mysql主从复制2

时间:2022-09-15 11:40:00

服务器A:

编辑配置文件添加如下:

server_id = 1

log-bin=binlog

log-bin-index=binlog.index

重启服务

创建用户:

GRANT replication slave ON *.* TO 'hello'@'%' identified by '123';

flush privileges;

服务器B:

server_id = 2

relay_log = /var/lib/mysql/mysql-relay-bin

relay_log_index=/var/lib/mysql/mysql-relay-bin.index

log-bin=binlog

log-slave-updates

将/var/lib/mysql下的所有文件及文件夹全部删除,并重启服务

进入mysql输入

change master to master_host='192.168.1.144', master_port=3306, master_user='hello', master_password='123', master_log_file='binlog.000003', master_log_pos=4;

start slave;

show slave status \G

查看IO和SQL的状态是否为yes

服务器C

server_id = 3

relay_log = /var/lib/mysql/mysql-relay-bin

relay_log_index=/var/lib/mysql/mysql-relay-bin.index

将/var/lib/mysql下的所有文件及文件夹全部删除,并重启服务

进入mysql输入

change master to master_host='192.168.1.144', master_port=3306, master_user='hello', master_password='123', master_log_file='binlog.000003', master_log_pos=4;

start slave;

show slave status \G

查看IO和SQL的状态是否为yes

服务器D:

server_id = 3

relay_log = /var/lib/mysql/mysql-relay-bin

relay_log_index=/var/lib/mysql/mysql-relay-bin.index

将/var/lib/mysql下的所有文件及文件夹全部删除,并重启服务

在服务器B上:

mysqldump -A -x > /tmp/bak.sql

scp /tmp/bak.sql root@192.168.1.108:/tmp/

在服务器D上:

mysql < /tmp/full.sql

进入mysql输入

change master to master_host='192.168.1.162', master_port=3306, master_user='hello', master_password='123', master_log_file='binlog.000003', master_log_pos=4;

start slave;


show slave status \G

查看IO和SQL的状态是否为yes

这样就实现了服务器A是服务器B和C的主,而服务器B是D的主了

本文出自 “海纳百川” 博客,谢绝转载!