要解决数据库频繁停止运行的问题,我们可以创建一个宝塔计划任务,定期检测数据库状态并在其停止时自动重启。
使用方法:
shell命令如下
#!/bin/bash
# 数据库服务名称(根据你的系统确认是mysqld)
DB_SERVICE="mysqld"
# 日志文件路径
LOG_FILE="/var/log/db_auto_restart.log"
# 记录当前时间
current_time=$(date "+%Y-%m-%d %H:%M:%S")
# 检测数据库服务状态(使用service命令)
if service $DB_SERVICE status >/dev/null 2>&1; then
# 数据库运行正常,记录状态
echo "[$current_time] $DB_SERVICE is running normally." >> $LOG_FILE
else
# 数据库已停止,记录状态并尝试重启
echo "[$current_time] $DB_SERVICE is not running. Attempting to restart..." >> $LOG_FILE
# 尝试重启数据库(使用service命令)
if service $DB_SERVICE restart >/dev/null 2>&1; then
# 重启成功
echo "[$current_time] $DB_SERVICE restarted successfully." >> $LOG_FILE
else
# 重启失败
echo "[$current_time] Failed to restart $DB_SERVICE!" >> $LOG_FILE
fi
fi
© 版权声明
THE END
暂无评论内容