mysql 双主同步
文章目录
ip地址
A节点IP地址:192.168.100.67 node1
B节点IP地址:192.168.100.60 node2
keepalive的VIP:192.168.100.145 (相关部署见下一章)
配置服务器的防火墙
#停止防火墙:systemctl stop firewalld
#关闭防火墙:systemctl disable firewalld
修改配置文件:vi /etc/my.cnf
server-id = 1 #节点2修改为2
auto-increment-increment = 2
auto-increment-offset = 1 #节点2修改为2
添加同步账户
账户名为account,密码为password123
两台服务器分别操作
节点1mysql> grant replication slave on . to ‘account’@‘192.168.100.67’ identified by ‘password123’;
节点2mysql> grant replication slave on . to ‘account’@‘192.168.100.60’ identified by ‘password123’;
#两台服务器分别操作,使配置立即生效
mysql> flush privileges;
查看的主库状态
节点1(192.168.100.67)show master status;
节点2(192.168.100.60)show master status;
开始主主同步
节点1(192.168.100.67)操作,change master 语句中填写节点2的信息
#第一台服务器操作
mysql> unlock tables;
mysql> stop slave;
mysql> change master to master_host='192.168.100.60',master_user='repl',master_password='password123',master_log_file='mysql-bin.000003',master_log_pos=150;
mysql> start slave;
mysql> show slave status \G
#执行show slave status \G之后看到以下内容为正常!
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
节点2(192.168.100.60)操作,change master 语句中填写节点1的信息
#第二台服务器操作
mysql> unlock tables;
mysql> stop slave;
mysql> change master to master_host='192.168.100.67',master_user='repl',master_password='password123',master_log_file='mysql-bin.000002',master_log_pos=2848;
mysql> start slave;
mysql> show slave status \G
#执行show slave status \G之后看到以下内容为正常!
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主主同步测试
在节点1登陆数据库,创建数据库,创建表,插入数据,然后在节点2上查看。