Linux Shell script detects MySQL status in real time and automatically restarts immediately if it fails

888u

Last update at :2024-04-05,Edit by888u

Why does a database connection error occur

I believe many friends have encountered the WordPress prompt "Error establishing a database connection" (English: "Error establishing a database connection"). Generally, there are two situations when encountering this error: One is that the database connection information in the wp-config.php file in the WordPress root directory is incorrect; the other is that the Mysql database has stopped.

The first situation rarely occurs. What everyone encounters is basically the second situation, when the Mysql database stops. Let’s talk about the reasons why this second situation occurs and how to solve it?

Reasons for Mysql database stopping

Generally speaking, the reasons why MySQL that is running normally suddenly stops unexpectedly are mostly the following: the server space is full, the server is attacked, the database reading and writing performance of multiple servers is poor...

In the third case, it is recommended that Baidu install and enable wordpress's Memcached or Redis database caching function. If it doesn't work, you can contact me for a fee.

Solution to Mysql database stop

In most cases, it is impossible for us to know whether the MySQL database has stopped, so how to ensure that MySQL is started in time after the MySQL database is stopped? The solution is introduced below.

1. Pagoda panel

If you use the Pagoda panel, it is very simple. Go to the "Scheduled Tasks" menu, select "Shell Script" for "Task Type", select "N minutes" for "Execution Period", and then fill in 15 minutes. Fill in the following code for the "script content" content (thank you group friend @chuanjianguo for providing the script):

pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ]; then
bash /www/server/panel/script/rememory.sh
/etc/init.d/mysqld start
fi

This way, it can automatically detect whether MySQL is stopped every 15 minutes. If it stops, it will automatically start. PS: The time interval 15 can be modified by yourself.

2.Other general

If you use other panel programs, you can use the following general method to solve it.

2.1 Production script

Save the following code as a file named dr_check_mysql.sh, then upload it to the server's root directory, and then execute the command chmod + x /root/dr_check_mysql.shGive execution permissions to the script file:

#!/bin/bash
# author: Long Xiaotian
# website: https://www.ilxtx.com/linux-shell-auto-restart-mysql.html

pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ];then
/etc/init.d/mysql start
fi

2.2 Add scheduled tasks

Use the crontab -e command to add a scheduled task, and modify the time interval 15 yourself:

#auto restart mysql
*/15 * * * * /bin/bash /root/dr_check_mysql.sh

Recommended site search: apply for free space and domain name, high-defense server rental qy, server proxy IP, expired registered domain name query, US server rental, domain name resolution query, Hong Kong high-defense server, .cn domain name registration, Hong Kong virtual host, How to check the domain name registration number?

Linux Shell script detects MySQL status in real time and automatically restarts immediately if it fails

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码