Fixing a Broken MySQL Slave on Linux
Wednesday, November 4, 2009 11:41During a recent slave addition to our architechture I ran into a few problems getting the slave back up and running after it had stopped for a period of time. I ran into multiple errors which included Duplicate / Primary Keys, missing tables, corrupted binary log entries etc. The solution was to use a handy feature in a one liner that would loop through and restart the slave and increment the SQL_SLAVE_SKIP_COUNTER until there weren’t any errors being reported by MySQL. The handy one liner is as follows, hope this helps someone else out as much as it has helped me:
while [ 1 ]; do if [ `mysql -e "show slave status\G" | grep "Duplicate entry" | wc -l` -eq 1 ]; then mysqladmin stop-slave; mysql -e “set global sql_slave_skip_counter=1″; mysqladmin start-slave; fi; sleep 7; done
Your mileage may vary to get this to work with your system depending on the shell and the mysql settings you use.