Why did MySQL suddenly stopped on my server?

Posted September 18, 2016 11.6k views

I am running a ready-made Wordpress droplet. Today my website displayed the dreaded “failed to establish database connection” message. I logged in to the server and noticed that the mysqld daemon doesn’t run in the background, so restarted it and all was fine again.

Question is, why would mysqld randomly stop by itself? I don’t want to find out that the WP website just doesn’t work when a potential client wants to look at it.

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.

Submit an Answer
3 answers

What is your Droplet size? Are you using 512MB Droplet?

What comes to my mind is that if MySQL could got killed because it left without available RAM.

As of Ubuntu 16.04, default MySQL is 5.7, which requires at least 1GB of RAM on Droplet.

If you are really using 512MB Droplet I could recommend you upgrading to higher or setting-up swap

by Justin Ellingwood
One of the easiest way of increasing the responsiveness of your server and guarding against out of memory errors in your applications is to add some swap space. In this guide, we will cover how to add a swap file to an Ubuntu 16.04 server. <$>[warning] [label...
  • I agree. This is most likely a memory issue, but barring that, OP might want to take a look at their log to find out more details.

  • As @eris suggesting, you should look for logs, this is usually best way to find details.
    You have two places where you look for logs.

    You should look at /var/log/syslog. This is system log, try to find is there any kernel Out of Memory (OOM) entries. If there any OOM entries about MySQL, that means it crashes because it run out of memory, so you should try fixing it with setting-up swap.
    More details also can give logs at /var/log/mysql/.
    If you need help analyzing you can post it here but please be sure to hide sensitive informations is there are any.

Every droplet I have ever created with an install of Wordpress will eventually crash MySQL, unless I take certain steps.

Mostly, what happens is that Wordpress is barraged with bots trying to login or gain access to your Wordpress account.

A few steps will diminish these attacks to rarely if ever crash MySQL:

  • Install the plugin Login-lockdown
  • Change your Wordpress Login Page (you will need to Google for that, as there is several ways to go about it) basically you are moving your Wordpress login page from wp-login.php to something else (for example mypersonalogin.php)
  • set up swap (as mentioned in previous answers
  • use 6G firewall in your .htaccess

Lastly, I created a script that will check on MySQL and Apache and restart them if needed:

Those things will not completely stop all bots or attacks on your Wordpress site, but they will stop the mindless bots that are searching for basic/common installs of Wordpress, which will make MySQL crash.

For fun, once you install Login-Lockdown, wait for a bit and then view the logs (under settings in Wordpress) and you will see all of the attempts that have been made to access your site. You can see all of the common names and passwords that the bots use to try and login with.

Yes, you should look at system logs before come to a conclusion. But there is a 95% chance that this happened due to lack of memory. You’ll have to upgrade your droplet if that’s the case. And while you’re at it you can create a more optimized environment for WordPress using a LEMP setup.