#教程# 监控MYSQL进程有故障则自动重启脚本

前言

图片[1] - #教程# 监控MYSQL进程有故障则自动重启脚本 - 云线路

监控 MYSQL 进程的脚本,故障则重启,如无法启动则 Email 通知;

功能

监控 MYSQL 进程,故障则重启,如无法启动则 Email 通知

#!/bin/bash
#/usr/bin/nmap localhost | grep 3306
#lsof -i:3306
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`
 
function checkMysqlStatus(){
	/usr/bin/mysql -uroot -p11111 --connect_timeout=5 -e "show databases;" &>/dev/null 2>&1
	if [ $? -ne 0 ]
	then
		restartMysqlService
		if [ "$MYSQLPORT" == "3306" ];then
			echo "mysql restart successful......" 
		else
			echo "mysql restart failure......"
			echo "Server: $MYSQLIP mysql is down, please try to restart mysql by manual!" > /var/log/mysqlerr
			#mail -s "WARN! server: $MYSQLIP  mysql is down" info@yunloc.com < /var/log/mysqlerr
		fi
	else
		echo "mysql is running..."
	fi
}
 
function restartMysqlService(){
	echo "try to restart the mysql service......"
	/bin/ps aux |grep mysql |grep -v grep | awk '{print $2}' | xargs kill -9
	service mysql start
}
 
if [ "$MYSQLPORT" == "3306" ]
then
	checkMysqlStatus
else
	restartMysqlService
fi

把脚本保存为 mysql_status.sh,这里建议每十分钟运行一次

*/10 * * * * /root/mysql_status.sh

结语

先检测 MYSQL 的 3306 端口是否正常;

使用帐号连接数据库并执行 show databases 命令;

如以上两点都能正常工作则表示数据库运行正常。

© 本站文章随意转载,但请注明出处!
THE END
点赞9 分享
评论 抢沙发
头像
务必使用真实的邮箱地址评论,虚假邮箱的评论将不通过审核及无回复。
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容