Skip to content

Instantly share code, notes, and snippets.

@tonyvu2014
Created April 17, 2015 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyvu2014/83cdc9a5cbf49a7a2c9e to your computer and use it in GitHub Desktop.
Save tonyvu2014/83cdc9a5cbf49a7a2c9e to your computer and use it in GitHub Desktop.
Mysql replication monitor and recovery. Set up the cronjob on mysql server (slave side) to run this script daily or weekly to monitor and recover from mysql replication failure. If there is any error an notification email will be sent to the email of your choice.
#!/bin/bash
USER=<your mysql username>
PASSWORD=<your mysql password>
SUBJECT="Mysql Replication Problem"
EMAIL=<your monitor email>
RESULT=`mysql -u$USER -p$PASSWORD -e 'show slave status\G' | grep Last_SQL_Error | sed -e 's/ *Last_SQL_Error: //'`
if [ -n "$RESULT" ]; then
echo "$RESULT" | mail -s "$SUBJECT" $EMAIL
mysql -u$USER -p$PASSWORD -e "STOP SLAVE;"
mysql -u$USER -p$PASSWORD -e "SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;"
mysql -u$USER -p$PASSWORD -e "START SLAVE;"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment