MySQL主从1032错误详解

Posted by 道行尚浅 on August 9, 2018

错误模拟

查询主库从库数据

  • 主库

mysql> select * from test.t1 ;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    2 |
|  2 |    3 |
+----+------+
2 rows in set (0.01 sec)
  • 从库

mysql> select * from test.t1 ;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    2 |
|  2 |    3 |
+----+------+
2 rows in set (0.01 sec)

从库删除数据


mysql> delete from test.t1 where c1=1 ;
Query OK, 1 row affected (0.01 sec)

主库修改主键相同的数据


mysql> update test.t1 set c2=6 where c1=1 ;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

查看从库同步状态 发现1032错误

mysql> show slave status \G ;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.61
                  Master_User: dao
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 155470
               Relay_Log_File: node2-relay-bin.000012
                Relay_Log_Pos: 153471
        Relay_Master_Log_File: mysql-bin.000004
             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: 1032
                   Last_Error: Could not execute Update_rows event on table test.t1; Can't find record in 't1', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000004, end_log_pos 155439
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 155206
              Relay_Log_Space: 153942
              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: 1032
               Last_SQL_Error: Could not execute Update_rows event on table test.t1; Can't find record in 't1', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000004, end_log_pos 155439
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 03fde241-9a55-11e8-aa12-000c291f013a
             Master_Info_File: /mysql1/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp: 180810 04:22:37
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

手工修复数据并跳过错误

手工添加缺失数据


mysql> insert into test.t1 values (1,6);
Query OK, 1 row affected (0.01 sec)

跳过此错误继续同步


mysql> stop slave ;
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1 ;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave ;
Query OK, 0 rows affected (0.00 sec)

查看同步状态


mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.61
                  Master_User: dao
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 155470
               Relay_Log_File: node2-relay-bin.000013
                Relay_Log_Pos: 320
        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: 155470
              Relay_Log_Space: 154108
              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: 03fde241-9a55-11e8-aa12-000c291f013a
             Master_Info_File: /mysql1/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)


使用参数直接跳过错误

在从库配置文件中加入slave-skip-errors


[mysql@node2 ~]$ cat /mysql1/mysql1.cnf
[client]
port = 3306
socket = /tmp/mysql1.sock
[mysqld]
port = 3306
socket = /tmp/mysql1.sock
#skip-grant-tables
basedir = /usr/local/mysql
datadir = /mysql1/data
pid-file = /mysql1/mysql1.pid
log-error = /mysql1/mysql1.log
log-bin=/mysql1/data/mysql-bin
server_id = 2
slave-skip-errors=1062,1032
log-slave-updates=1

启动数据库


[mysql@node2 ~]$ mysqld --defaults-file=/mysql1/mysql1.cnf &
[1] 3557


从库删除主键为1 的数据


mysql> delete from test.t1 where c1=1 ;
Query OK, 1 row affected (0.03 sec)


主库修改ID为1 的数据


mysql> update test.t1 set c2=8  where c1=1 ;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

查看同步状态

  • 主库



mysql> show master status ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |   155734 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)



  • 从库


mysql> delete from test.t1 where c1=1 ;
Query OK, 1 row affected (0.03 sec)

mysql> show slave status \G ;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.61
                  Master_User: dao
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 155734
               Relay_Log_File: node2-relay-bin.000017
                Relay_Log_Pos: 584
        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: 155734
              Relay_Log_Space: 791
              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: 03fde241-9a55-11e8-aa12-000c291f013a
             Master_Info_File: /mysql1/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

发现日志已同步,但是没有报错,虽然没有报错,但现在主库和从库数据并不一致.

查看主库从库数据

主库



mysql> select  * from test.t1 ;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    8 |
|  2 |    3 |
+----+------+
2 rows in set (0.00 sec)

从库



mysql> select * from test.t1
    -> ;
+----+------+
| c1 | c2   |
+----+------+
|  2 |    3 |
+----+------+
1 row in set (0.00 sec)


使用工具修复此错误

查看错误


[mysql@node1 ~]$ pt-table-checksum --host='192.168.2.61' --user=dao --password=dao --create-replicate-table  --nocheck-replication-filters --replicate=test.checksums   --no-check-binlog-format --databases=test
            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
08-10T04:40:24      0      1        2       1       0   0.149 test.t1



修复错误



[mysql@node1 ~]$ pt-table-sync --execute --sync-to-master 192.168.2.62 --user=dao --password=dao



验证

  • 主库

mysql> select * from test.t1 ;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    8 |
|  2 |    3 |
+----+------+
2 rows in set (0.00 sec)

  • 从库

mysql>  select * from test.t1 ;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    8 |
|  2 |    3 |
+----+------+
2 rows in set (0.00 sec)