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

1062错误——主键冲突,出现这种情况就是从库出现插入操作,主库又插入相同的数据,iothread没问题,sqlthread出错
处理此种错误一般有两种思路:
1、直接跳过错误执行语句
2、找到错误执行语句,修复主库2数据

语句跳过操作方法如下:
1062 错误提示如下:

         Last_Errno: 1062
         Last_Error: Could not execute Write_rows event on table test.test; Duplicate entry '2' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql2-bin.000001, end_log_pos 2799
         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.21
                      Master_User: rep1
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql2-bin.000001
              Read_Master_Log_Pos: 3087
                   Relay_Log_File: cndba-relay-bin.000006
                    Relay_Log_Pos: 587
            Relay_Master_Log_File: mysql2-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: test
    #还有一种方法跳过所有1062错误
    更改my.cnf文件,在Replication settings下添加:
    slave-skip-errors = 1062
    并重启数据库,然后start salve。
    注意:因为要重启数据库,不推荐,除非错误事件太多。

模拟1062场景:
主库1创建表

    mysql> create table test(id int PRIMARY KEY ,name varchar(32));
    Query OK, 0 rows affected (0.06 sec)
    修改参数sql_log_bin 使主库1操作不同步到主库2
    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 使主库1操作说同步到主库2
    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)

主库2查看数据同步情况主库2查看数据同步情况

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

在主库2插入表test

    mysql> insert into test values (2,'bb');
    Query OK, 1 row affected (0.01 sec)

在主库1查看主从状态

    发现报1062错误,发生这个错误的原因是因为主库1已经存在id=2的数据,导致主键冲突。
    mysql> show slave status/G;
    ......
                       Last_Errno: 1062
                       Last_Error: Could not execute Write_rows event on table test.test; Duplicate entry '2' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql2-bin.000001, end_log_pos 2799
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 2564
                  Relay_Log_Space: 1541
                  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: 1062
                   Last_SQL_Error: Could not execute Write_rows event on table test.test; Duplicate entry '2' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql2-bin.000001, end_log_pos 2799
    ......

根本解决办法:

    根据Last_Error中提示的master log和end_log_pos的位置查找这条主库2上缺失的数据
    主库2操作:
    [root@cndba data]# mysqlbinlog --no-defaults -v --base64-output=decode-rows  --stop-position=2799 /data/mysql/binlog/mysql2-bin.000001 | tail -20
    #180819 10:59:56 server id 21  end_log_pos 2706 CRC32 0x5a43437b     Query    thread_id=6    exec_time=0    error_code=0
    SET TIMESTAMP=1534647596/*!*/;
    SET @@session.sql_mode=1436549152/*!*/;
    SET @@session.auto_increment_increment=2, @@session.auto_increment_offset=2/*!*/;
    BEGIN
    /*!*/;
    # at 2706
    #180819 10:59:56 server id 21  end_log_pos 2756 CRC32 0x0d6a3132     Table_map: `test`.`test` mapped to number 229
    # at 2756
    #180819 10:59:56 server id 21  end_log_pos 2799 CRC32 0xc6a8a0b0     Write_rows: table id 229 flags: STMT_END_F
    ### INSERT INTO `test`.`test`
    ### SET
    ###   @1=2
    ###   @2='bb'
    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*/;
    找到之后,手动转变为delete 语句
    主库1执行以下语句:
    mysql> delete from test where id=2;
    Query OK, 1 row affected (0.02 sec)
    启动复制
    mysql> start slave;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    查看主库2状态:
    mysql> show slave status/G;
    ......
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    ......

确定插入的数据和库里的数据是不是相同,如果相同可以删除主库1的数据来解决,否则可以考虑跳过这个事件来解决
 

你可能感兴趣的:(MYSQL主从)