Mysql – thesql replication stop/start causes duplicate rows

MySQLreplicationstart-stop-daemon

We're using pretty simple MASTER->SLAVE MySQL query-based replication.

Every midnight a cron script on the slave stops the replication SQL_THREAD, backs up the slaves databases with mysqldump and then restarts the replication SQL_THREAD.

After replication restarts though, it immediately stops, with this errornous output in SHOW SLAVE STATUS\G

Last_Errno: 1062
Last_Error: Error 'Duplicate entry '54149' for key 1' on query. Default database: 'dbname'. Query: 'insert into emails (emailsubject,emailmessage,html) values (blah','blah', false);'

It seems like the problem is that the replication log on the slave is replaying statements it has already processed.

MySQL bug tracker seems to agree with me: http://bugs.mysql.com/bug.php?id=38205#c200483

Because replication seems like such a basic and standard procedure, my question is: how do we get around replication trying to replay statements?


Addendum: I wanted to use the tags 'start-stop' and 'replaying' and 'statements' to create this post, but my reputation isnt high enough (seems silly?)

Best Answer

Do you need to stop replication to run your dump, mysqldump can get a global read lock with the --lock-all-tables option.