我们有时候需要一些检查MySQL是否宕机,如果宕机了应自动重新启动应用并通知运维人员!
此脚本用来简单的实现MySQL宕机后自动重启并邮件通知运维,此为SHELL脚本,当然也有一些朋友喜欢用Python之类的实现,其原理是一样的!这儿主要用到的是命令是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="my@lvtao.net"# 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 如果mysql配置了有密码,就用 mysqladmin -u user -ppassword
然后定时执行这个脚本*/5 * * * * //scripts/mysql.sh