签到成功

知道了

CNDBA社区CNDBA社区

解决Mysql 主从或主主报1032错误

2018-08-19 19:30 5847 0 原创 故障处理 mysql
作者: leo

1032错误的主要原因是主库更新或者是删除的记录在从库上不存在引起的。
处理此种错误一般有两种思路:
1、直接跳过错误执行语句
2、找到错误执行语句,修复从库数据
第一种解决方案会有造成主从不一致的隐患(delete语句可以跳过),第二种是从根本上解决问题比较推荐 http://www.cndba.cn/leo1990/article/2956http://www.cndba.cn/leo1990/article/2956http://www.cndba.cn/leo1990/article/2956

http://www.cndba.cn/leo1990/article/2956

语句跳过操作方法如下:

1032 错误提示如下:

http://www.cndba.cn/leo1990/article/2956

Replicate_Wild_Ignore_Table: 
                 Last_Errno: 1032
                 Last_Error: Could not execute Update_rows event on table test.test; Can't find record in 'test', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql1-bin.000001, end_log_pos 2579
               Skip_Counter: 0

—传统模式

mysql> stop slave;
#表示跳过一步错误,后面的数字可变,(或者N条event,一条一条跳过)
mysql> set global sql_slave_skip_counter =1;
mysql> start slave;

之后再用mysql> show slave status/G 查看:

mysql> show slave status/G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.20
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql1-bin.000001
          Read_Master_Log_Pos: 2610
               Relay_Log_File: cndba-relay-bin.000005
                Relay_Log_Pos: 321
        Relay_Master_Log_File: mysql1-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
#还有一种方法跳过所有1032错误
更改my.cnf文件,在Replication settings下添加:
  slave-skip-errors = 1032
并重启数据库,然后start salve。
注意:因为要重启数据库,不推荐,除非错误事件太多。

—GTID模式

mysql> stop slave;
通过show slave status/G;找到Retrieved_Gtid_Set:7800a22c-95ae-11e4-983d-080027de205a:12
mysql> set GTID_NEXT='7800a22c-95ae-11e4-983d-080027de205a:12'
mysql> begin;commit;
mysql> set GTID_NEXT='AUTOMATIC';
mysql> start slave;

模拟1032场景:

主库创建表

mysql> create table test(id int PRIMARY KEY ,name varchar(32));
Query OK, 0 rows affected (0.06 sec)
修改参数sql_log_bin 使主库操作不同步到从库
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values (1,'aa');
Query OK, 1 row affected (0.02 sec)
mysql> insert into test values (2,'bb');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test values (3,'dd');
Query OK, 1 row affected (0.00 sec)
修改参数sql_log_bin 使主库操作说同步到从库
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values (4,'cc');
Query OK, 1 row affected (0.08 sec)
mysql> select * from test; 
+----+------+
| id | name |
+----+------+
|  1 | aa   |
|  2 | bb   |
|  3 | dd   |
|  4 | cc   |
+----+------+
4 rows in set (0.00 sec)

从库查看数据同步情况

可以看到从库数据只同步了一条数据
mysql> select * from test; 
+----+------+
| id | name |
+----+------+
|  4 | cc   |
+----+------+
1 row in set (0.00 sec)

在主库更新表test

mysql> update test set name = 'abc' where id=1;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0
此时在插入一条数据:
mysql> insert into test values (5,'dd');
Query OK, 1 row affected (0.01 sec)

在备库查看主从状态

发现报1032错误,发生这个错误的原因是因为从库没有id=1的数据,导致更新主库,从库没有相应的数据进行修改。http://www.cndba.cn/leo1990/article/2956

http://www.cndba.cn/leo1990/article/2956
http://www.cndba.cn/leo1990/article/2956http://www.cndba.cn/leo1990/article/2956

mysql> show slave status/G;
......
                   Last_Errno: 1032
                   Last_Error: Could not execute Update_rows event on table test.test; Can't find record in 'test', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql1-bin.000001, end_log_pos 2037
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1792
              Relay_Log_Space: 2264
              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.test; Can't find record in 'test', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql1-bin.000001, end_log_pos 2037
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 20
......

解决办法:

根据Last_Error中提示的master log和end_log_pos的位置查找这条从库上缺失的数据
主库操作:
[root@cndba binlog]# mysqlbinlog --no-defaults -v --base64-output=decode-rows  --stop-position=2037 /data/mysql/binlog/mysql1-bin.000001 | tail -20
SET TIMESTAMP=1534626999/*!*/;
BEGIN
/*!*/;
# at 1934
#180819  5:16:39 server id 20  end_log_pos 1984 CRC32 0x666a3738     Table_map: `test`.`test` mapped to number 220
# at 1984
#180819  5:16:39 server id 20  end_log_pos 2037 CRC32 0x74a99c1f     Update_rows: table id 220 flags: STMT_END_F
### UPDATE `test`.`test`
### WHERE
###   @1=1
###   @2='aa'
### SET
###   @1=1
###   @2='abc'
ROLLBACK /* added by mysqlbinlog */ /*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

找到之后,手动转变为insert 语句
从库执行以下语句: 
mysql> insert into `test`.`test` values (1,'aa');
Query OK, 1 row affected (0.03 sec)
启动复制
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
查看从库状态:
mysql> show slave status/G;
......
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
......
mysql> select * from test;
+----+------+
| id | name |
+----+------+
|  1 | abc  |
|  4 | cc   |
|  5 | dd   |
+----+------+

通过以上结果可以看到状态正常了,并且在错误发生之后插入的数据也通过到从库,可以得出结论,只要修复1032错误不会对错误发生之后的数据产生影响。http://www.cndba.cn/leo1990/article/2956

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
leo

leo

关注

坚持你的坚持

  • 202
    原创
  • 0
    翻译
  • 39
    转载
  • 16
    评论
  • 访问:674834次
  • 积分:1270
  • 等级:核心会员
  • 排名:第8名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ