谷姐:我们都是谷歌迷
We are all Google fans

利用 cron 自动启动崩溃的 MySQL 服务

Rate this post

Error Establishing a Database Connection
最近网站的MySQL服务一直奔溃,换了内存大的服务器还是不行。博主不太会找这个问题,只好尽可能地第一时间重启,但是大多时候是不及时的。这里尝试利用定时执行工具cron来第一时间自动启动崩溃的MySQL服务。

一、新建脚本目录

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cd /home/ && sudo mkdir scripts && cd scripts
cd /home/ && sudo mkdir scripts && cd scripts
cd /home/ && sudo mkdir scripts && cd scripts

二、将bash脚本写入mysqlmon.sh

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat <<EOT >> mysqlmon.sh
#!/bin/bash
# Check if MySQL is running
sudo service mysql 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"
sudo service mysql restart
else
echo -e "MySQL Service is running already. Nothing to do here.\n"
fi
EOT
cat <<EOT >> mysqlmon.sh #!/bin/bash # Check if MySQL is running sudo service mysql 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" sudo service mysql restart else echo -e "MySQL Service is running already. Nothing to do here.\n" fi EOT
cat <<EOT >> mysqlmon.sh
#!/bin/bash

# Check if MySQL is running
sudo service mysql 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"
    sudo service mysql restart
else
    echo -e "MySQL Service is running already. Nothing to do here.\n"
fi
EOT

或者手动录入,输入

sudo crontab -e
sudo crontab -e后,写入

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
* * * * * /home/scripts/mysqlmon.sh > /dev/null 2>&1
* * * * * /home/scripts/mysqlmon.sh > /dev/null 2>&1
* * * * * /home/scripts/mysqlmon.sh > /dev/null 2>&1

三、赋予mysqlmon.sh可执行权限

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo chmod +x mysqlmon.sh
sudo chmod +x mysqlmon.sh
sudo chmod +x mysqlmon.sh

四、定时运行mysqlmon.sh脚本

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(crontab -l 2>/dev/null; echo "* * * * * /home/scripts/mysqlmon.sh > /dev/null 2>&1") | crontab -
(crontab -l 2>/dev/null; echo "* * * * * /home/scripts/mysqlmon.sh > /dev/null 2>&1") | crontab -
(crontab -l 2>/dev/null; echo "* * * * * /home/scripts/mysqlmon.sh > /dev/null 2>&1") | crontab -

五、测试效果

尝试手动关闭MySQL,看看脚本能否发现MySQL被关闭和能否启动它
手动关闭MySQL

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo service mysql stop
sudo service mysql stop
sudo service mysql stop

等待1分钟后,查看MySQL的运行状态

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo service mysql status
sudo service mysql status
sudo service mysql status

如果发现没起来,那就是上面的脚本内容有问题或者脚本的可执行权限没设置好,再或者是cron任务没跑起来。可手动启动MySQL后,对以上的操作挨个检查。
手动启动MySQL命令

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo service mysql start
sudo service mysql start
sudo service mysql start
未经谷姐允许不得转载:谷姐靓号网 » 利用 cron 自动启动崩溃的 MySQL 服务
分享到: 生成海报

热门文章

评论 抢沙发

评论前必须登录!

立即登录   注册

买Google Voice认准【谷姐靓号网】

Google Voice靓号列表Google Voice自助购买
切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活