当前位置:网站首页 > SQL数据库编程 > 正文

mysql主键重复报错1062(mysql重置主键)



一、基础信息

1. Centos7.4

2.MySQL 5.7.21

3.基于gtid的复制

二、异常描述

误把从节点当成主节点插入一条数据,同一条数据在主、从节点插入都进行了一次插入操作,导致主键冲突,slave下的SQL线程异常。

建议设置从库为只读:

my.cnf 文件设置永久只读

三、处理过程

1.查看报错信息-- 报错大致如下

mysql> show slave status G
* 1. row *
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: repuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_binlog.000023
Read_Master_Log_Pos:
Relay_Log_File: devmidsrv01kf-relay-bin.000003
Relay_Log_Pos: 423
Relay_Master_Log_File: mysql_binlog.000016
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: 1062
Last_Error: Could not execute Write_rows event on table testdb1.students; Duplicate entry '8' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql_binlog.000016, end_log_pos
Skip_Counter: 0
Exec_Master_Log_Pos:
Relay_Log_Space:
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 testdb1.students; Duplicate entry '8' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql_binlog.000016, end_log_pos
Replicate_Ignore_Server_Ids:
Master_Server_Id: 757
Master_UUID: 7bd36140-a92d-11e9-ba1f-005056a4099b
Master_Info_File: /data/mysql/3306/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: 16:19:02
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 7bd36140-a92d-11e9-ba1f-005056a4099b:-
Executed_Gtid_Set: 7bd36140-a92d-11e9-ba1f-005056a4099b:1-,
bcad977d-a934-11e9-a589-005056a483f4:1-29
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

2.确认需要跳过的gtid是关键

找到这个已经执行的gtid,加1

7bd36140-a92d-11e9-ba1f-005056a4099b:1-

3.跳过gtid

stop slave;

SET @@SESSION.GTID_NEXT= '7bd36140-a92d-11e9-ba1f-005056a4099b:1-';

BEGIN; COMMIT;

SET SESSION GTID_NEXT = AUTOMATIC;

START SLAVE;

4、检查

mysql> show slave status G
* 1. row *
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: repuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_binlog.000023
Read_Master_Log_Pos:
Relay_Log_File: devmidsrv01kf-relay-bin.000003
Relay_Log_Pos: 423
Relay_Master_Log_File: mysql_binlog.000016
Slave_IO_Running: Yes
  Slave_SQL_Running: Yes

到此这篇mysql主键重复报错1062(mysql重置主键)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在 编程的领域有一番成就!

版权声明


相关文章:

  • sql文件转换成csv显示不全(sql csv文件)2025-04-24 22:45:04
  • sql for循环(sql for循环到5000)2025-04-24 22:45:04
  • mysql导入sql文件过大自动断开(mysql 运行sql文件导入到数据库 失败)2025-04-24 22:45:04
  • oracle sql 最大长度(oracle sql最长长度)2025-04-24 22:45:04
  • sql文件转换成csv显示不全(sqlserver导出csv文件)2025-04-24 22:45:04
  • sql文件是什么数据库(sql是什么文件格式)2025-04-24 22:45:04
  • sql文件如何打开数据库(sql怎么打开数据库文件)2025-04-24 22:45:04
  • pymysql(pymysql怎么安装)2025-04-24 22:45:04
  • sqluldr2 字符集(sqlloader 字符集)2025-04-24 22:45:04
  • Pymysql 连接池(pymysql连接池多线程)2025-04-24 22:45:04
  • 全屏图片