1、主备设置只读

set global read_only=ON;
set global super_read_only=ON;

2、查看备库复制进程状态

show slave status\G

3、从库停掉复制进程并清空主从信息

stop slave;
reset slave all;

4、从库关闭只读开启读写,转为新主库

set global read_only=off;
set global super_read_only=off;

5、主库设置执行新主库的复制链路,转为新备库,完成主从切换

CHANGE MASTER TO MASTER_HOST='192.168.1.111',MASTER_USER='xxx',MASTER_PORT=3306,MASTER_PASSWORD='xxx',master_auto_position=1 ;

6、原主库上激活

start slave;
show slave status\G