计划内 MySQL 传统主从切换,相当于 ORACLE 的switch over,就是主从切换。

主库A、从库B

从库B执行show processlsit;如果出现 Slave has read all relay log; waiting for more updates 说明主从的状态是正常。

从库B多次执行 show slave status\G;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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_PosRelay_Log_FileRelay_Log_Pos 不再发生变化,同时 Exec_Master_Log_Pos 等于 Read_Master_Log_Pos 说明主从是同步的。

确保 主库 A 没有业务在更新,可以通过停止业务(建议通过停止业务,show processlist,没有业务用户在连接操作),或者 flush tables with read lock 来阻止主库的写入。

  1. 在从库 B(在新主库上)执行 stop slave

    1
    stop slave;
  2. 在从库 B(在新主库上)执行reset slave all,使其断开与主库A(老主库)的连接。

    1
    reset slave all
  3. 执行show master status记录新主库(从库 B)的二进制日志位点(FilePosition)。

    1
    2
    3
    4
    5
    6
    7
    8
    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)
  4. 关闭主库A(老主库),参数文件配置 只读

    1
    2
    3
    vi /etc/my.cnf
    read_only=on
    super_read_only=on
  5. 在新主库(从库 B) 设置参数取消只读

    1
    2
    set global read_only=off;
    set global super_read_only=off;

    同时修改参数为配置数据库取消只读

    1
    2
    3
    vi /etc/my.cnf
    read_only=off
    super_read_only=off
  6. 建议重启新主库(从库B)

  7. 启动老主库(主库A也是即将变为新从库的),并配置新的主从

    1
    2
    3
    4
    5
    6
    7
    8
    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
    ;
  8. 新从库(主库A)启动 复制

    1
    start slave;
  9. 检查新的从库复制信息

    1
    show slave status\G

原文作者: liups.com

原文链接: http://liups.com/posts/d5d58030/

许可协议: 知识共享署名-非商业性使用 4.0 国际许可协议