Sometimes MySQL Replication may run out of sync – because of its own buts or operational limitations or because of application mistake, such as writing to the slave when you should be only writing to the master. In any case you need slave to be synced with Master.
To discover the difference between Master and Slave you can use excellent Maatkit tool though if you you just need to resync small single table it may be easy to do it other way:
Assuming your table schema does not change you can do something like this on the master:
LOCK TABLE tbl WRITE;
SELECT * FROM table INTO OUTFILE ‘/tmp/tbl.txt’;
DELETE FROM tbl;
LOAD DATA INFILE ‘tmp/tbl.txt’ INTO TABLE tbl;
This will dump master’s table content to the file clean the table on master and slave and refresh it on both of them.
This obviously makes table inaccessible on master for duration of operation but for small tables it can take just few seconds.
This trick is of course quite dirty, especially if you’re dealing with sensitive data – what if you run out of space or SELECT INFO OUTFILE fails for other reason and you proceed with deletion you may kiss your data goodbye.
Alternative would be not to drop the table but rather raname them. Unfortunately you can’t rename locked tables (ERROR 1192 (HY000): Can’t execute the given command because you have active locked tables or an active transaction) – this is also I used DELETE instead of TRUNCATE to clean out the table.
In the applications which are error tolerant (like handle errors properly) you may rename table instead of locking – for many applications this works better anyway as having table locked causes connections to pile up and either MySQL runs out of connections or Apache out of children allowed to process request;
So you can do following instead:
mysql> rename table rep to rep_maint; Query OK, 0 rows affected (0.01 sec) mysql> select * from rep_maint into outfile '/tmp/rep.txt'; Query OK, 0 rows affected (0.01 sec) mysql> create table rep_new like rep_maint; Query OK, 0 rows affected (0.00 sec) mysql> load data infile '/tmp/rep.txt' into table rep_new; Query OK, 0 rows affected (0.01 sec) Records: 0 Deleted: 0 Skipped: 0 Warnings: 0 mysql> rename table rep_maint to rep_old, rep_new to rep; Query OK, 0 rows affected (0.00 sec)
This way you never actually delete any data and besides same data loaded to “rep” table you will get the copy of old table in “rep_old” – this can be very handy if you would like to later investigate why data got out of sync and may be do some manual touch ups after you resync the data.
Note – this process works well for small tables when you do not want to shut down the master and slave. If you need quick resync of large amount of data it is much better to use LVM snapshots or similar techniques – these are much faster.