准备工作
用户
主,备节点都要创建 Replication
create user 'repl'@'192.168.2.%' identified by '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.2.%';
查询从库状态
show slave status\G;
查询主库状态
show slave hosts;
操作工作
从库slave停止 IO_THREAD 线程
slave
stop slave IO_THREAD;
Slave_IO_Running: No
Slave_SQL_Running: Yes
确保主从数据是否同步完毕
激活从库为主库
看到 Slave_SQL_Running_State: Slave has read all relay log;
waiting for the slave I/O thread to update it这个状态后
1. stop slave; # 完全停止 slave 复制
2. reset slave all; # 完全清空 slave 复制信息
3.reset master; # 清空本机上 master 的位置信息(给原主库同步此原从库做准备)
4.show binary logs;(GTID除外) # 查看当前数据库的 binlog 信息
将原主库变为从库(根据需要)
master
注意要保证主从数据一致的情况下,才进行reset
change master to master_host='192.168.66.129',master_user='repl',master_port=3306,
master_password='123456',master_auto_position=1;
其他从库:
stop slave;然后指向新主change master to;
检查
主库查看show slave hosts;
从库show slave status\G
如果主库下线
从库中不需要执行reset master了, 因为原主库(现从库)不要再找点啦(Position)