mysql 1062 场景_解决Mysql 主从或主主报1062错误[通俗易懂]

mysql 1062 场景_解决Mysql 主从或主主报1062错误[通俗易懂]1062错误——主键冲突,出现这种情况就是从库出现插入操作,主库又插入相同的数据,iothread没问题,sqlthread出错处理此种错误一般有两种思路:1、直接跳过错误执行语句2、找到错误执行语句,修复主库2数据https://www.cndba.cn/leo1990/article/2957https://www.cndba.cn/leo1990/article/2957https://ww…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

1062错误——主键冲突,出现这种情况就是从库出现插入操作,主库又插入相同的数据,iothread没问题,sqlthread出错

处理此种错误一般有两种思路:

1、直接跳过错误执行语句

2、找到错误执行语句,修复主库2数据https://www.cndba.cn/leo1990/article/2957https://www.cndba.cn/leo1990/article/2957

https://www.cndba.cn/leo1990/article/2957

https://www.cndba.cn/leo1990/article/2957https://www.cndba.cn/leo1990/article/2957

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的数据来解决,否则可以考虑跳过这个事件来解决。

https://www.cndba.cn/leo1990/article/2957

https://www.cndba.cn/leo1990/article/2957

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189545.html原文链接:https://javaforall.net

(0)
上一篇 2026年4月13日 下午1:46
下一篇 2026年4月13日 下午1:46


相关推荐

  • 关于敏捷软件开发

    关于敏捷软件开发最近在看 对其中的原则和实践非常感兴趣 顺便记录下来 1 敏捷软件开发宣言 nbsp 个体和交互 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 胜过 nbsp nbsp 过程和工具 nbsp 可以工作的软件 nbsp 胜过 nbsp nbsp 面面俱到的文档 nbsp 客户合作 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 胜过 nbsp nbsp 合同谈判 nbsp 响应变化 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 胜过 nbsp nbsp 遵循计划 2 12 个原则 nbsp 2 1 我们最优先要做的是通过尽早的 持续的交付有价值的软件来使客户满意 nbsp 2

    2026年3月17日
    2
  • 数组求和方法汇总_用函数的方法对输入的数组求和

    数组求和方法汇总_用函数的方法对输入的数组求和vararr=[1,2,3,4,5,6];测试时我不想过度使用全局变量影响命名空间,所以没使用未声明变量。而是直接通过私有作用域设置静态私有变量,也可以用其他设计模式来限定变量作用域。因为数组对象的迭代方法也是一种遍历,所以也可以借助用来实现求和。一、利用数组对象的各迭代方法:1.array.every()查询是否有所有项都匹配的方法:1(function(){…

    2026年4月17日
    5
  • SpringCloud Greenwich.M3 版本组件汇总

    SpringCloud Greenwich.M3 版本组件汇总因为之前用的 springcloud nbsp D 版本的 版本比较低了 打算对 springcloud 做下升级决定用 Greenwich M3 下文会列出常用的组件和升级过程中遇到的问题 以下模块已作为 Greenwich M3 的一部分进行了更新 MODULE VERSION ISSUES SpringCloud Greenwich M3 nbsp SpringClo

    2026年3月19日
    2
  • iOS 13越狱:越狱后如何安装AppSync和afc2越狱补丁

    iOS 13越狱:越狱后如何安装AppSync和afc2越狱补丁iOS13越狱:越狱后如何安装AppSync和afc2越狱补丁?本文件转载自:https://news.tongbu.com/98644.html文章概要:越狱后必须安装的补丁:AppSync和afc2。越狱后如何安装AppSync和afc2越狱补丁?越狱虽然成功了,但如果不安装Appsync和afc2这两个重要的补丁,就无法享受越狱的功能哦。请按照如下教程提示,分别安装着这2个重要补丁。越狱教程:《iOS13越狱教程:unc0ver5.2.0更新发布,支持iOS13.5..

    2022年4月27日
    77
  • 大黑AI速报

    大黑AI速报

    2026年3月18日
    2
  • 百度智能云发布DuClaw

    百度智能云发布DuClaw

    2026年3月18日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号