Question

How to automatically restart MYSQL when it crashes on Ubuntu Wordpress 18.04?

It happens infrequently, but whenever my wordpress crashes it causes a downtime. If I am away for home I have to wait anywhere from a few hours to a few days till I’m back home and log in to my console and restart my server using

sudo service mysql restart

I am aware that SSH can be installed on my smartphone and I can log in through it and restart my server.

But shouldn’t mysql restart by itself when it crashes? If not, is there any other easy way I can make it restart automatically whenever it goes down?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello, @SamN

You can create a simple bash script to check if MySQL is running and if not to restart it.

#!/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
    sudo service mysql restart
fi

Run this script every 5 minutes using a cron job like this one:

 */5 * * * * /home/user/scripts/monitor.sh > /dev/null 2>&1

Hope that this helps! Regards, Alex

You might want to check into “Monit”. It’s a free application that you can add to your server that will monitor the services you specify in the config file. So you tell it to look at specific services such as NGINX or MySQL and if they go down, monit will restart them based on the rules you put in the config. Basically just follow this tutorial and you should be good to go: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-monit

Hey, thank you for the detailed reply

Lastly, you’ll need to create a script which checks if MySQL is actually down and start it up again. This can be executed every minute by a cron job on your server.

Not to seem rude but can I get more on this first?

Firstly, you’ll need to check why the service was killed. To do so, you’ll have to check your /var/log/messages for the kill or oom command from your server. Run the following

I am getting a no such file or directory error on this