我们有时候需要一些检查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" ]]
then
echo "It‘s dead - restart mysql"
# email subject
SUBJECT="[MYSQL ERROR] - Attempting to restart service"
# Email To ?
EMAIL="[email protected]"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "$result was received"> $EMAILMESSAGE
echo "when we were expected $expected" >>$EMAILMESSAGE
# send an email using /bin/mail
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
sudo /etc/init.d/mysql restart
fi
mysqladmin ping 如果mysql配置了有密码,就用 mysqladmin -u user -ppassword
然后定时执行这个脚本
*/5 * * * * /<path_to>/scripts/mysql.sh
时间: 2024-10-07 13:32:26