MySQL复制常见错误--主键重复
主从数据不一致时,slave上已经有该条记录,但我们又在master上插入了同一条记录,此时就会报错,报错信息如下:
Last_SQL_Error:Could not execute Write_rows event on table ...;
Duplicater entry '2' for key 'PRIMARY',
Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY;
the event's master log bin-log.000006, end_log_pos 924
解决方法:在slave上使用命令“desc abc.student;”先查看一下表结构,如下所示:
mysql> desc abc.student;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | | |
| class | int(10) | YES | | NULL | |
| score | varchar(10) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
得到主键的字段名id,接着删除重复的主键,命令如下:
delete from student where id=2;
开启主从复制:
start slave;
show slave status\G;