【转】三台MySQL数据库做三主环形同步
系统:centos7
数据库:mysql
三台MySQL数据库做三主环形同步:A主B从,B主C从,C主A从
A:192.168.3.104
B:192.168.3.101
C:192.168.3.102
方法
一、修改数据库配置文件
gedit etc/my.cnf
#----------------------------------添加的部分------------------------------------
server_id = 3
log_bin = mysql-bin
binlog-do-db=link #需要同步的数据库
relay-log=relay-bin
relay-log-index=relay-bin.index
replicate-do-db = link
replicate-do-table=link.sync
auto_increment_offset=3
auto_increment_increment=3
sync_binlog=1
slave_net_timeout=30
innodb_flush_log_at_trx_commit=2
log-slave-updates=true
slave-skip-errors=all
#--------------------------------------------------------------------------------------
server_id 要不一样 三台数据库可以设为1、2、3
auto_increment_offset 同理 设为1、2、3
auto_increment_increment表示几台同步
二、重启mysql数据库
systemctl restart mariadb.service
三、在三台数据库里面设置权限账户
进入到各台数据库的shelll界面
A:create user ‘slave’@‘192.168.3.101’ identified by ‘root’;
grant replication slave on . to ‘slave’@‘192.168.3.101’;
flush privileges;
B:create user ‘slave’@‘192.168.3.102’ identified by ‘root’;
grant replication slave on . to ‘slave’@‘192.168.3.102’;
flush privileges;
C:create user ‘slave’@‘192.168.3.104’ identified by ‘root’;
grant replication slave on . to ‘slave’@‘192.168.3.104’;
flush privileges;
账户为”slave“,密码是”root"
四、查看主机log_file:show master status;
创建完账户后,就输入:show master status;来查看主机log_file。
记下master_log_file和master_log_pos的值,从机连接主机时要用到。
五、从机连接主机
进入到各台数据库的shell界面
A:(A的主机为C机,所以要连接C机)
change master to master_host=‘192.168.3.102’,master_user=‘slave’,master_password=‘root’,master_log_file=‘mysql-bin.000003’,master_log_pos=770;#此处的,master_log_file和master_log_pos的值为上一步查询到的,为C机上查询到的值,下面两台同理
change master to MASTER_HEARTBEAT_PERIOD=10;
B:(B的主机为A机,所以连接A机)
change master to master_host=‘192.168.3.104’,master_user=‘slave’,master_password=‘root’,master_log_file=‘mysql-bin.000004’,master_log_pos=1113;
change master to MASTER_HEARTBEAT_PERIOD=10;
C:(C的主机为B机,所以连接B机)
change master to master_host=‘192.168.3.101’,master_user=‘slave’,master_password=‘root’,master_log_file=‘mysql-bin.000001’,master_log_pos=662;
change master to MASTER_HEARTBEAT_PERIOD=10;
“change master to MASTER_HEARTBEAT_PERIOD=10;”为从机心跳信号,目的是为了让主机知道从机还在线。
六、开启同步
从机连接完主机后,在从机开启同步:start slave;
然后查看连接状态:show slave status \G;
如果看到:
Slave_io_running:yes
Slave_sql_running:yes
即为同步成功
————————————————
原文链接:https://blog.csdn.net/tyz666/article/details/100094983
默认分类 2021-02-26 10:52:00 通过 网页 浏览(1009)
共有1条评论!
我还没有验证哈,行不行自己看。