主库A、从库B
从库B执行 show processlsit; 如果出现 Slave has read all relay log; waiting for more updates 说明主从式同步的。
多次执行 show slave status\G
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 194
Relay_Log_File: mysql-relay.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 194
Relay_Log_Space: 783
Until_Condition: None
以上的 Master_Log_File 和 Read_Master_Log_Pos及 Relay_Log_File 和Relay_Log_Pos 不在发生变化,同时 Exec_Master_Log_Pos 等于 Read_Master_Log_Pos 说明主从是同步的。
确保 主库 A 没有业务在更新,可以通过停止业务(建议通过停止业务,show processlist,没有业务用户在连接操作),或者 flush tables with read lock 来阻止主库的写入。
-
在从库 B(在新主库上)执行stop slave。
stop slave;
-
在从库 B(在新主库上)执行reset slave all,使其断开与主库A(老主库)的连接。
reset slave all;
-
执行show master status记录新主库(从库 B)的二进制日志位点(File 和 Position)。
show master status; mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000007 | 194 | | | | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
-
关闭主库A(老主库),参数文件配置 只读
vi /etc/my.cnf read_only=on super_read_only=on
-
在新主库(从库 B) 设置参数取消只读
set global read_only=off; set global super_read_only=off
同时修改参数为配置数据库取消只读
vi /etc/my.cnf read_only=off super_read_only=off
-
建议重启新主库(从库B)
-
启动老主库(主库A也是即将变为新从库的),并配置新的主从
CHANGE MASTER TO MASTER_HOST='新主库的ip也就是从库B的ip', MASTER_USER='repl', MASTER_PASSWORD='replication', MASTER_PORT=3306, MASTER_LOG_FILE='master-bin.000007', ## 位点信息也就是第 3 步获取的 file MASTER_LOG_POS=194 ## ## 位点信息也就是第 3 步获取的 Position ;
-
新从库(主库A)启动 复制
start slave;
-
检查新的从库复制信息
show slave status\G