如何解决宝塔面板莫名其妙的总是MYSQL自动停止的问题?
在使用宝塔的过程中,经常会遇到了 MySQL 自动停止的问题,尤其是配置低一些的服务器。导致 MySQL 停止的情况有很多种,
这里不再多述。
下面教大家怎么解决这一问题。
方法一:
在宝塔的计划任务里添加一段 shell 命令,从而达到定时自动执行检测 MySQL 是否停止,如果是停止状态,那么就执行启动命令:
代码如下:
pgrep -x mysqld &> /dev/null if [ $? -ne 0 ];then bash /www/server/panel/script/rememory.sh /etc/init.d/mysqld start echo "监控到MySQL已停止,已执行重启计划,时间:`date "+%Y-%m-%d %H:%M:%S"`" >> /www/mysql_error.log fi
如果 MySQL 自动停止,代码运行的日志会记录到这里: /www/mysql_error.log 请自行查看。
方法二:
创建一个检测和重启MySQL的脚本:
#!/bin/bash # 检查MySQL服务状态 mysql_status=$(systemctl status mysqld | grep "active (running)") if [ -z "$mysql_status" ]; then # 记录重启日志 echo "$(date '+%Y-%m-%d %H:%M:%S') MySQL is not running, restarting..." >> /var/log/mysql_autorestart.log # 重启MySQL服务 systemctl restart mysqld # 再次检查状态 sleep 5 new_status=$(systemctl status mysqld | grep "active (running)") if [ -n "$new_status" ]; then echo "$(date '+%Y-%m-%d %H:%M:%S') MySQL restarted successfully" >> /var/log/mysql_autorestart.log else echo "$(date '+%Y-%m-%d %H:%M:%S') MySQL restart failed" >> /var/log/mysql_autorestart.log fi fi
将脚本保存为
/root/mysql_monitor.sh
,并赋予执行权限:
chmod +x /root/mysql_monitor.sh
设置crontab定时任务,每分钟检查一次:
(crontab -l ; echo "* * * * * /root/mysql_monitor.sh") | crontab -
日志文件位置:
/var/log/mysql_autorestart.log