Você está na página 1de 4

How to fix MySQL Duplicate Record

Replication Error
By Will Mayall http://www.nwstartups.com 16-June-2011
Problem: Sometimes, the Master sends the record twice, the first update works,
the second attempt BREAKS Replication. Then you have to Manually Fix Replication
on the Slave.

Check the mysql replication slave status:


mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: clnxcctdb01svnew.hilton.com
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 235837065
Relay_Log_File: clnxcctdb02svnew-relay-bin.000015
Relay_Log_Pos: 836877
Relay_Master_Log_File: mysql-bin.000001
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: Error 'Duplicate entry 'BOOKING_ROOM-1-GLOBAL-2010-04-23-2099-12-31-1188' for key 1' on query.
Default database: 'bre'. Query: 'insert into TRIGGER_DATA (TYPE, SEQUENCE, CRO, BRAND, SRP, CITY,
PROPERTY_CODE, HONORS_TIER, START_DATE, END_DATE, TRIGGER_ID) values ('BOOKING_ROOM', 1,
'GLOBAL', NULL, 'GHRCC', NULL, 'LARLA', NULL, '2010-04-23', '2099-12-31', 1188)'
Skip_Counter: 0
Exec_Master_Log_Pos: 836740
Relay_Log_Space: 235841037
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
1 row in set (0.00 sec)

Last_Errorno: 1062, the 1062 is a Duplicate Record.

Get the Table DDL:


mysql> show create table TRIGGER_DATA\G
*************************** 1. row ***************************
Table: TRIGGER_DATA
Create Table: CREATE TABLE `TRIGGER_DATA` (
`TYPE` varchar(32) NOT NULL,
`SEQUENCE` int(11) NOT NULL default '1',
`CRO` varchar(32) NOT NULL,
`BRAND` varchar(32) default '"not_available"',
`SRP` varchar(32) default '"not_available"',
`CITY` varchar(32) default '"not_available"',
`PROPERTY_CODE` varchar(32) default '"not_available"',
`HONORS_TIER` varchar(32) default '"not_available"',
`START_DATE` date NOT NULL,
`END_DATE` date NOT NULL,
`TRIGGER_ID` int(11) NOT NULL,
PRIMARY KEY (`TYPE`,`SEQUENCE`,`CRO`,`START_DATE`,`END_DATE`,`TRIGGER_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

The duplication occurs when the Primary Key is violated:

mysql> select TYPE,SEQUENCE,CRO,START_DATE,END_DATE,TRIGGER_ID from TRIGGER_DATA


where TYPE='BOOKING_ROOM' and SEQUENCE=1 and CRO='GLOBAL' and START_DATE='2010-04-23' and
TRIGGER_ID=1188;

+--------------+----------+--------+------------+------------+------------+
| TYPE | SEQUENCE | CRO | START_DATE | END_DATE | TRIGGER_ID |
+--------------+----------+--------+------------+------------+------------+
| BOOKING_ROOM | 1 | GLOBAL | 2010-04-23 | 2099-12-31 | 1188 |
+--------------+----------+--------+------------+------------+------------+
1 row in set (0.00 sec)

Only returned one row, of course, so do a select * against the Primary


Key:

mysql> select * from TRIGGER_DATA where TYPE='BOOKING_ROOM' and SEQUENCE=1 and CRO='GLOBAL'
and START_DATE='2010-04-23' and TRIGGER_ID=1188\G

*************************** 1. row ***************************


TYPE: BOOKING_ROOM
SEQUENCE: 1
CRO: GLOBAL
BRAND: NULL
SRP: GHRCC
CITY: NULL
PROPERTY_CODE: LARLA
HONORS_TIER: NULL
START_DATE: 2010-04-23
END_DATE: 2099-12-31
TRIGGER_ID: 1188
1 row in set (0.00 sec)
mysql>

Looks like a duplicate to me.


Verify Replication is not running, Highlighted in RED above in show
slave status.
From mysql skip the error and start the slave:
mysql>set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;start slave;

Show the Slave status.


mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: clnxcctdb01svnew.hilton.com
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 235837065
Relay_Log_File: clnxcctdb02svnew-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:

Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 235837065
Relay_Log_Space: 235
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: 0
1 row in set (0.00 sec)
mysql>

Replication is now running on the slave.

Você também pode gostar