Question

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

Posted November 28, 2019 1.2k views
WordPress

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?

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.

×
3 answers

Hi @SamN,

There are a couple of ways going about it. First however, you’ll need to see why MySQL is crashing in order to be fix the issue rather than just cover it up.

Most probably the issue comes out of High Load caused by something, being either a service like MySQL,Apache or Nginx or just too much traffic. When this happens and your service runs out of memory it kills random services to keep itself from crashing completely. How to be able to pinpoint this?

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

grep -i kill /var/log/messages
grep -i oom /var/log/messages

This will show you why and if your server killed a service and why.

If there is no output, most probably the service itself, in this case MySQL crashed for a different reason. We’ll get on to that a bit later.

Now, let’s assume you did get some output stating your server went out of memory and had to kill a service to not crash. In this case, you’ll need to check if your service was taking too much memory and why.

This will most probably be related to traffic. This can be checked in the logs of your Web Service and Mail Service. Depending on your service the logs will be in a different location. You’ll need to google on for that.

Now, let’s continue if you didn’t get an output from the above commands. It will mean the service, in this case MySQL broke down because of a different reason.

If it was MySQL the problem, then it will be displayed in the error log of MySQL. You can find where this log is stored by opening the file

/etc/my.cnf

There the path to your log file would be presented. In the error log file, the reason behind the crash will be displayed. You’ll need to act upon that.

Alternatively, your server just doesn’t have enough RAM and you’ll need to upgrade it to keep it from crashing again.

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.

Regards,
KDSys

  • <^>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

    grep -i kill /var/log/messages
    grep -i oom /var/log/messages<^>

    I tried this but I am getting an error saying

    grep /var/log/messages: No such file or directory
    
    • Depending on the OS, the information in /var/log/messages can be stored in /var/log/syslog .

      Please try to check if the errors are presented there.

      • Thank you. I checked both

        grep -i kill /var/log/syslog
        grep -i oom /var/log/syslog
        

        and got no output.

        I also checked /etc/mysql/my.cnf and /etc/mysql/mysql.cnf and neither give me the path to my log file

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

  • What I meant was to actually have a bash script set as a cron or create a service that checks if your SQL instance is actually up.

    If you don’t any scripting knowledge however it’s gonna be quite hard for you to write it from scratch. As such I’ll recommend leaving this step as a last resort and focusing on the other steps provided.

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

by Etel Sverdlov
Monit is a helpful program that automatically monitors and manages server programs to ensure that they not only stay online consistently, but that the file size, checksum, or permissions are always correct. This tutorial covers monit installation and provides an example of how to configure it for some common programs.
Submit an Answer