查看主库运行状态

-- 查看主库运行状态
mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000012
         Position: 439767167
     Binlog_Do_DB: xxx_db
 Binlog_Ignore_DB: information_schema,mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)

查看从库运行状态

-- 查看从库运行状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.0.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000012
          Read_Master_Log_Pos: 439767167
               Relay_Log_File: xxxx-relay-bin.000018
                Relay_Log_Pos: 33321
        Relay_Master_Log_File: mysql-bin.000012
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: xxx_db
          Replicate_Ignore_DB: mysql,information_schema
           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: 439767167
              Relay_Log_Space: 34651
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 84cc6274-2241-11e6-86b1-00161e0c136d
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

其实就是主要看 Slave_IO_Running 和 Slave_SQL_Running 两个线程的状态。

-- 负责把主库bin日志(Master_Log)内容投递到从库的中继日志上(Relay_Log)
Slave_IO_Running: Yes
 
-- 负责把中继日志上的语句在从库上执行一遍
Slave_SQL_Running: Yes
 
-- Yes:表示正常, No:表示异常
Slave_IO线程相对比较简单,一般不容易出错。如果显示为No,则有可能以下原因导致:
    * 网络问题
    * 权限问题,例如在配置slave同步时因为slave访问master没有权限导致
    * master上的binlog文件误删或者其他问题导致的master库突然停止更新binlog日志。解决方案是找到同步的点和binlog文件,重新change master
 
相对的Slave_SQL线程就比较容易出错。例如人为的在从库插入一条数据,造成的主从库不一致。但此时两个线程的状态仍然是正常的,主库插入数据时,到从库同步时,从库会出现主键重复的错误。此时Slave_SQL_Running的状态变为No
 
而Last_SQL_Error, Last_SQL_Error_Timestamp会记录错误的原因和发生的时间
 
Slave_SQL_Running线程报错之后,会停止后续的SQL执行,因为向后执行会导致错误修复的难度增加

错误修复

-- 先停止slave
stop slave;
 
-- 跳过执行语句数量
-- 此时需要查看错误日志去修复报错信息
set global sql_slave_skip_counter=1;
 
-- 开始slave
start slave;
 
-- 然后再检查一下 slave status

如何判断完全同步

* Master_Log_File 和 Relay_Master_Log_File 所指向的文件必须一致
* Relay_Log_Pos 和 Exec_Master_Log_Pos 位置也要一致才行
* Slave_SQL_Running_State: 显示为wait 中继日志的sql语句已经全部执行完毕
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐