Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Learn how to automatically restart a crashed MySQL service on Ubuntu using Cron. Ensure uninterrupted database performance with this simple guide.
MySQL may crash when it runs out of memory or for some other issue. In most cases, you can resolve this by simply restarting the MySQL service.
Also Read: Setup VueJS on Ubuntu
sudo service mysql statussudo service mysql restartBut, what if you’re away on holidays while your client’s website goes down? Surely there’s a way to automate this process In Ubuntu with the following easy steps.
You can configure cron to automatically check the status of the MySQL server and restart it if it crashes. Of course, this is not a permanent fix for your leaky MySQL server, but it can buy you time until you are able to investigate further.
We first need to create a simple BASH script and store it in the home folder (or anywhere you want), and then instruct cron to run this BASH script once a minute.
You can put this script anywhere, but in this example, we will put it in the home folder.
Change directory to the home folder.
cd /home/sudo mkdir scriptsChange to this directory.
cd scriptsCreate a new file in this directory called mysqlmon.sh using the nano text editor.
sudo nano mysqlmon.shPaste in the following script.
/home/scripts/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"
fiSave and exit.
Also Read: How database effects the website Design
sudo chmod +x mysqlmon.shTest the script by running:
Also Read: Setup Ubuntu Server for Laravel
sudo ./mysqlmon.shIf MySQL is up and running, you should see:
MySQL Service is running already. Nothing to do here.If you want to see what happens when the script detects if MySQL is down, stop the MySQL service, but only do this if your web server isn’t live! If your server is live but not busy, stopping and starting the MySQL server should only take a few seconds.
sudo service mysql stopNow test the script again by running:
sudo ./mysqlmon.shOutput:
MySQL Service was down. Restarting now..sudo service mysql statusBy adding this script to crontab, the server will check the MySQL service once a minute, and if it isn’t running, it will restart it.
Open crontab (if asked to select a text editor, choose nano).
sudo crontab -eIn crontab, add the following line to the bottom of the file.
* * * * * /home/scripts/mysqlmon.sh > /dev/null 2>&1Save and exit (press CTRL + X, press Y and then press ENTER)
To test if crontab is running the script once a minute, you will need to stop the MySQL service temporarily.
sudo service mysql stopYou can now test if MySQL is running with the following command.
sudo service mysql statusIf MySQL is down, wait for at least a minute until the crontab runs. If after two minutes the MySQL service is still down, something has gone wrong with your cron script. Start MySQL again with sudo service mysql start.
Also Read: Install PHPStorm in Ubuntu