Auto Restart MariaDB/MySQL If it Crashes on CentOS
Infrequently it happens on CentOS. MySQL/MariaDB stops working and our web application crashes. At this situation we have to login to SSH and restart MySQL/MariaDB server. It’s a painful thing.
To get rid of this issue, we can create a script to check database service is running or not. If database service stops, the script will restart the service automatically.
Have a look at the bash script to restart MySQL:
#!/bin/bash # Check if MySQL is running sudo systemctl status mysql > /dev/null 2>&1 # Restart the MySQL service if it's not running. if [ $? != 0 ]; then sudo systemctl restart mysql fi
Have a look at the bash script to restart MariaDB:
#!/bin/bash # Check if MariaDB is running sudo systemctl status mariadb > /dev/null 2>&1 # Restart the MariaDB service if it's not running. if [ $? != 0 ]; then sudo systemctl restart mariadb fi
After that add a cronjob like this:
That’s all. Using this method, you can restart any service. Thanks for reading. ?
* * * * * /home/user/scripts/restart_mariadb.sh > /dev/null 2>&1 #or * * * * * /bin/bash /home/user/scripts/restart_mariadb.sh > /dev/null 2>&1
Preview may take a few seconds to load.
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Bold **double asterisks**
Three back ticks and then enter your code blocks here.
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
> type a greater than sign and start typing your quote.
You can add links by adding text inside of  and the link inside of (), like so:
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of , and the image URL inside of (), like so:
To add a divider you can add three dashes or three asterisks:
--- or ***