We sometimes need to check whether MySQL is down or not, and if it goes down, you should automatically restart the app and notify the OPS staff!
This script is simple to implement the MySQL after the downtime of automatic restart and mail notification operations, this is the shell script, of course, some friends like to use the implementation of Python, the principle is the same!
The main use here is that the command is mysqladmin ping .
#!/bin/bash# result=`/usr/bin/mysqladmin -u user -ppassword ping`result=`/usr/bin/mysqladmin ping`expected=‘mysqld is alive‘if [[ "$result" != "$expected" ]]thenecho "It‘s dead - restart mysql"# email subjectSUBJECT="[MYSQL ERROR] - Attempting to restart service"# Email To ?EMAIL="[email protected]"# Email text/messageEMAILMESSAGE="/tmp/emailmessage.txt"echo "$result was received"> $EMAILMESSAGEecho "when we were expected $expected" >>$EMAILMESSAGE# send an email using /bin/mailmail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGEsudo /etc/init.d/mysql restartfi
Mysqladmin Ping If MySQL is configured with a password, use mysqladmin-u user -pPassword
and execute the script on a regular basis.
*/5 * * * * /<path_to>/scripts/mysql.sh
MySQL timed check for downtime and email notifications