云计算2主从数据库

设置主从数据库的目的是将数据库1和数据库2分别建在两个虚拟机上,并实现数据互通访问

首先准备两个虚拟机,这里示例ip分别为:

192.168.200.10;192.168.200.20

修改主机名,一个是mysql1,一个是mysql2,(可改可不改,方便区别而已,我后续一个是mysql1一个是samba),

然后关闭SELinux和防火墙,并修改hosts的配置文件,下图增加的最后两行是自己虚拟机的ip和主机名,无需和我一样

配置yum源,安装数据库服务,注意如何配置yum源 ,不再重复细节,可以参考云计算1中ftp安装篇,现在设置里检查DVD光盘文件是否连接,再进行以下操作

root@localhost\~#mount /dev/cdrom /opt/centos 将cd设备挂载到/opt/centos,不存在的话可以先创建

root@localhost\~#mkdir -p /opt/centos

root@localhost\~#cd /opt

root@localhost\~#ll 查看是否创建成功 total 636则挂载成功

root@localhost\~#mv /etc/yum.repos.d/* /media/ 移除repos.d下的文件

root@localhost\~#vi /etc/yum.repos.d/local.repo 创建local文件

centos7

name=cantos7

baseurl=file:///opt/centos

gpgcheck=0

enabled=1

root@localhost\~#cat /etc/yum.repos.d/local.repo 查看

进行数据库服务的安装

root@mysql1 \~# yum install -y mariadb mariadb-server

两台虚拟机一样操作到这一步,然后同样启动数据库服务

root@mysql1 /# systemctl start mariadb

root@mysql1 /# systemctl enable mariadb

初始化数据库并进行配置

root@mysql1 /# mysql_secure_installation

接下来按照提示,set root password?,y,root,000000,remove anonymous users?,y,Disallow root login remotely? ,n

remove test database and access to it?,y,Reload privilege tables now? y

配置mysql1主节点,注意server_id是ip的结束数字

进入数据库,使用数据库命令语句创建一个叫test的数据库,在test下创建叫company的表格

root@mysql1 /# systemctl restart mariadb

root@mysql1 /# mysql -uroot -p000000

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright © 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB (none)> grant all privileges on . to root@'%' identified by "000000";

Query OK, 0 rows affected (0.00 sec)

MariaDB (none)> grant replication slave on . to 'user'@'samba' identified by'000000';

Query OK, 0 rows affected (0.00 sec)

MariaDB (none)> create database test;

Query OK, 1 row affected (0.00 sec)

MariaDB (none)> use test;

Database changed

MariaDB test> create table company(id int not null primary key,name varchar(50),addr varchar(255));

Query OK, 0 rows affected (0.01 sec)

MariaDB test> insert into company values(1,'alibaba','china');

Query OK, 1 row affected (0.00 sec)

MariaDB test> select*from company;

±---±--------±------+

| id | name | addr |

±---±--------±------+

| 1 | alibaba | china |

±---±--------±------+

1 row in set (0.00 sec)

这样主数据库就配置好了

配置从数据库的节点

root@samba \~# systemctl restart mariadb

root@samba \~# mysql -uroot -p000000

MariaDB (none)> change master to master_host='mysql1',master_user='user',master_password='000000';

Query OK, 0 rows affected (0.00 sec)

MariaDB (none)> start slave;

Query OK, 0 rows affected (0.00 sec)

MariaDB (none)> show slave status \G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: mysql1

Master_User: user

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 527

Relay_Log_File: mariadb-relay-bin.000002

Relay_Log_Pos: 811

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

配置成功,开始验证主从数据库的关联功能,试试能否在虚拟机2也就是从数据库访问到刚才在主数据库建立的表格信息

MariaDB (none)> show databases;

±-------------------+

| Database |

±-------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

±-------------------+

4 rows in set (0.00 sec)

MariaDB (none)> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB test> show tables;

±---------------+

| Tables_in_test |

±---------------+

| company |

±---------------+

1 row in set (0.00 sec)

MariaDB test> select*from company;

±---±--------±------+

| id | name | addr |

±---±--------±------+

| 1 | alibaba | china |

相关推荐
闲云自留地2 小时前
主库倒下 30 秒自动扶正:MySQL MHA 高可用实战
数据库·mysql
许彰午10 小时前
01-Oracle数据库简介与版本选择
数据库·oracle
菜鸟~noob23311 小时前
【电子战】第三季预告—波武器与电子系统强力毁伤
开发语言·数据库·matlab·电子战
Seraphina3612 小时前
SQL注入(二)盲注
网络·数据库·经验分享·笔记·sql
盟接之桥14 小时前
AI助手:你的7×24小时智能管家——让异常预警无处不在
大数据·网络·数据库·人工智能·制造·ai编程
waoooqwe14 小时前
问卷样本真实吗
大数据·数据库·算法·数据分析
可乐ea14 小时前
Agent 评测换判据:用终态数据库状态判定任务是否完成
数据库·ui·oracle·mcp工具·agent评测·终态断言·智能体可靠性
l1t14 小时前
DeepSeek总结的一个不含数据的 DuckDB 数据库
服务器·数据库·duckdb
PaperData15 小时前
2007-2020年税收调查面板数据
数据库
程序员清风16 小时前
CSV、Excel 与数据库数据读取实践
数据库·oracle·excel