环境准备:
master:192.168.0.106:3306
slave:192.168.0.105:3306
主和从都必须配置有唯一的ID(server_id:建议ip最后一组+MySQL端口号,例如:1063306,1053306)
采用gtid模式复制,需要配置mysqld开启以下两个参数
1 gtid_mode=ON 2 enforce-gtid-consistency=ON
操作步骤:
1、在master创建用于复制的账号
1 mysql> CREATE USER 'repl'@'192.168.0.%' IDENTIFIED BY 'repl'; 2 mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%';
2、mysqldump备份master数据库
1 mysqldump -S /tmp/mysql3306.sock -p --master-data=2 --single-transaction -A |gzip >3306-`date +%F`.tar.gz
3、slave上还原master的备份
1 gunzip < 3306-2019-10-13.sql.gz | mysql -S /tmp/mysql3306.sock -p
提示:ERROR 3546 (HY000) at line 26: @@GLOBAL.GTID_PURGED cannot be changed: the added gtid set must not overlap with @@GLOBAL.GTID_EXECUTED
登录slave数据库,清除从站已有GTID_EXECUTED值,然后再执行还原
1 mysql> reset master; 2 mysql> exit; 3 gunzip < 3306-2019-10-13.sql.gz | mysql -S /tmp/mysql3306.sock -p
5、 登录slave数据库,执行主从配置连接命令
1 CHANGE MASTER TO 2 MASTER_HOST='192.168.0.106', 3 MASTER_USER='repl', 4 MASTER_PASSWORD='repl', 5 MASTER_PORT=3306, 6 MASTER_AUTO_POSITION=1;
MASTER_HOST: master主机ip
MASTER_USER:master复制连接账号
MASTER_PASSWORD:master复制连接密码
MASTER_PORT:master端口
MASTER_AUTO_POSITION:使用GTID模式
6、登录slave数据库,启动复制
1 start slave;
7、查看复制状态信息
1 show slave status\G
查看以下两个参数都为Yes代表复制启动成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes