Linux中利用shell脚本定时检测mysql状态实现自动重启

1、背景:最近网站的MySQL数据库,晚上经常无故宕机,然后起不来了。(具体原因分析中...)

2、目的:临时解决MySQL宕机后,利用shell脚本+定时器cron,检查MySQL是否宕机,宕机就重启

3、shell脚本:【mysqlRestartXzz.sh脚本文件】 【mysqlRestartXzz.log脚本日志文件】

[root@iZ23****bZ cron]# cat /www/shell_files/mysqlRestartXzz.sh
#!/bin/bash
pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ]
then
echo “At time: `date +%Y%m%d-%H:%M:%S` MySQL is stop .”>> /www/shell_files/mysqlRestartXzz.log
/etc/init.d/mysqld start
else
echo “At time: `date +%Y%m%d-%H:%M:%S` MySQL server is running .”>> /www/shell_files/mysqlRestartXzz.log
fi

 

4、测试shell脚本是否可用:手动停止MySQL,执行脚本命令,测试如下:【测试成功,将脚本写入定时器 略】

 

 

5、最终是暂时解决了这个问题,不过MySQL宕机原因还是要找到并解决的。
posted @ 2022-06-09 11:34  傻白甜++  阅读(682)  评论(0编辑  收藏  举报
TOP