MySQL案列之主从复制出错问题以及pt-slave-restart工具的使用

今天主从复制遇到一个问题,主库上插入了几百行万数据,后来又删除了这些数据,原因就是主库删除的表从库中不存在,导致从库在遇到删除不存在表的错误无法继续同步。

MySQL [(none)]> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.31.22.29
                  Master_User: chaofeng
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 997651368
               Relay_Log_File: ip-172-31-26-133-relay-bin.000004
                Relay_Log_Pos: 35415834
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1146
                   Last_Error: Error executing row event: 'Table 'sbtest2.sbtest1' doesn't exist'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 35415621
              Relay_Log_Space: 7442202963
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1146
               Last_SQL_Error: Error executing row event: 'Table 'sbtest2.sbtest1' doesn't exist'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: ce15f67e-0ccc-11e9-9e0a-0af74ce261dc
             Master_Info_File: /data/data_mysql/master.info

解决的步骤就是:我们通过设置参数来跳过这个错误。你需要从上面来确定last error这个值,

1)在/etc/my.cnf的[mysqld]下面添加slave_skip_errors=1146。

2)接下来重启mysql服务器即可。

注意:这个参数是不能在mysql命令行内直接设置,因为他是个只读参数。

MySQL [(none)]> set global slave_skip_errors=1146;
ERROR 1238 (HY000): Variable 'slave_skip_errors' is a read only variable

在遇到这个问题是参考这个老哥的文章解决的:https://blog.csdn.net/gua___gua/article/details/52869614

 注意事项:跳过错误这个命令,解决从库多数据的现象(错误代码1062)。如果从库少数据,还跳过错误,就不能从根儿上解决主从同步的问题了(错误代码1032),就需要先找到缺少的数据是什么了,如果缺少的特别多,建议重新搭建主从环境。

如果说觉得这样子做比较麻烦的话,那我们还可以使用pt-slae-restart这个工具来跳过错误:

这个操作要在从库上执行

[root@node4 bin]# ./pt-slave-restart -uroot -proot123 --error-numbers=1062
2017-08-10T16:28:12 p=...,u=root node4-relay-bin.000002      751437 1062
原文地址:https://www.cnblogs.com/FengGeBlog/p/10250825.html