检测mysql是否运行,如崩溃自动重启

最好的办法,当然是从根源上避免程序崩溃。
但是在一个512M内存的小服务器上,数据库崩溃还是无法完全避免。
方法一
做个脚本。不过这个方法有点绕远了。
#!/bin/bash
if (( $(ps -ef | grep -v grep | grep mysqld | wc -l) <= 0 ))
then
   echo “MySQL is currently not running and will be restarted!” | mail -s “MySQL may have crashed” -r from@dingxuan.info to@dingxuan.info
   service mysqld restart
else
   echo “Running”
fi
加入crontab
*/5 * * * * /root/mysql_monitor.sh
方法二
比前一种更简洁。首先,可以测试一下数据库运行状态。mysqld 或者mariadb
/sbin/service mysqld status
echo $?
如果返回值不是0,说明运行状态异常。所以可以用这个作为判断依据,来控制是否执行启动命令。
/sbin/service mysqld status || service mysqld start
加到crontab里,每分钟运行一次。
* * * * * /sbin/service mysqld status || service mysqld start
延伸:测试apache运行状态
同理,要测试如无法访问某网址,则重启apache:
*/2     *       *       *       *       wget -q dingxuan.info || service httpd restart

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Time limit is exhausted. Please reload CAPTCHA.