当 mysql 服务因内存不足被系统强制退出后,自动再启动

问题:有个VPS,站点在某些时间段可能被爬虫频繁访问导致mysql服务自动退出,隔天就要人工启动mysql服务,恢复站点可访问状态。

尝试:可以写个定时执行的任务,检测mysql服务是否运行,如没有运行,就启动之。

如脚本 /root/mysqlmon.sh 的内容:

#!/bin/bash

# Check if MySQL is running
/sbin/service mysqld status > /dev/null 2>&1

# Restart the MySQL service if it's not running.
if [ $? != 0 ]; then
    echo -e "MySQL Service was down. Restarting now...\n"
    /sbin/service mysqld restart
else
    echo -e "MySQL Service is running already. Nothing to do here.\n"
fi

定时任务内容(每隔15分钟检查服务状态):
*/15 * * * * /root/mysqlmon.sh > /dev/null 2>&1

在centos7中,可查看 /var/log/cron 日志内容,查看任务执行的历史记录。

参考:https://devanswers.co/how-to-auto-restart-a-crashed-mysql-service-with-cron/

标签: mysql, cron

添加新评论