Home Linux Server Create a script to restart MySQL when it stops on the Linux server

Create a script to restart MySQL when it stops on the Linux server

by Thạch Phạm
Published: Last Updated on

Create a script to restart MySQL when it stops on a Linux server with 3 steps.


In the process of using or administering a Linux server, surely you are no stranger to the Mysql service being suspended or stopped at some point for some reason. I often see Mysql being stopped when the space is full or due to excessive use of RAM resources on the server. When the RAM resources on the server are overloaded, the OOM service will have to kill the Mysql process to reduce this resource load.

If Mysql is stopped due to full capacity, you need to check and delete unnecessary data (logs,…) then you can restart Mysql. And if we fall into the case of being stopped due to overloading Ram resources or for some unknown reason, we can apply the way to create Cron like this article.

How to create a Script

Step 1: SSH into your Linux server

To make Cron start Mysql on Linux, we first need to SSH or access your VPS/server as root. If you don’t know how to SSH into your VPS/Server, you can refer to the following tutorial:

After successfully SSH, we continue with step 2 to continue the configuration.

Step 2: Create a script to check the status and start Mysql

Please follow the commands below to create the path and add the script content (you can replace the path, the file/folder name as you like).

  • Create the path containing the script file
AZDIGI Tutorial
mkdir /root/auto
vi /root/auto/autostart-mysql.sh

Explanation of commands:

Command 1: Create an auto folder at /root

Command 2: Create file autostart-mysql.sh at /root/auto

  • Add the below content to the created file

After creating the autostart-mysql.sh file, proceed to edit and paste the script content below into the file.

To determine if you are using mysqld or mariadb, you can use the command below:

AZDIGI Tutorial
netstat -nlp | grep 3306

If you are using MySQL , use the following script:

AZDIGI Tutorial
if [ ! "$(systemctl status mysql.service | awk 'NR==3 {print $2}')" == "active" ]; then
/bin/systemctl start mysql.service

If you are using MariaDB , use the following script:

AZDIGI Tutorial
if [ ! "$(/bin/systemctl status mariadb.service | awk 'NR==3 {print $2}')" == "active" ]; then
systemctl start mariadb.service

Here is an example of when I add script content to the autostart-mysql.sh file. Once added, please save it.

CleanShot 2022 05 10 at 14.14.09
  • Permissions for the file autostart-mysql.sh
AZDIGI Tutorial
chmod +x /root/auto/autostart-mysql.sh

Step 3: Create Cron to set script runtime

Note: You should not set the Cron runtime too short (less than 5 minutes) because it will easily overload resources and cause system errors.

For example, I will set the time every 10 minutes, and the script will be run once to check the status of Mysql, if the status is stopped, it will automatically restart. Next, you set up fast Cron using the command:

AZDIGI Tutorial
(crontab -u root -l ; echo "*/10 * * * * /bin/bash /root/auto/autostart-mysql.sh") | crontab -u root -
systemctl restart crond

Explanation of the above command:

Command 1: Set up Cron to run the script every 10 minutes and check once.

Command 2: Restart the Crond service

So AZDIGI has shown you in detail how to create a Cron that automatically checks the status and restarts Mysql if it is stopped. This helps your website not be interrupted for too long if you cannot monitor your server 24/24. Hope this article is helpful to you, wish you success!

You can refer to other instructions at the link below:

If you need assistance, you can contact support in the ways below:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign