Skip to content

Instantly share code, notes, and snippets.

@tonyvu2014
Created April 17, 2015 01:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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