Question

MySQL stops working from time to time

I have a droplet with Ubuntu 22.04 LTS and Apache, PHP, MySQL and Wordpress set up. Everything works flawlessly for a few days or weeks. But then, I check my sites and the browser says: “Error establishing a database connection”. Then I log in to the droplet, restart the system via console and everything goes back to normal. Everything works fine for a few days and then, out of nowhere, the same error happens: “Error establishing a database connection”, when trying to access the sites. I guess it’s something related to MySQL. Why does it happen? And how can I fix it and prevent it from happening for good, so the sites will work properly without the need of manually reseting the server from time to time?


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.

alexdo
Site Moderator
Site Moderator badge
February 12, 2024

I will definitely recommend you to add a swap file if you haven’t done this yet and also use the MySQL tuner script and see if the MySQL configuration needs an improvement.

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

  1. #!/bin/bash
  2. # Check if MySQL is running
  3. sudo service mysql status > /dev/null 2>&1
  4. # Restart the MySQL service if it's not running.
  5. if [ $? != 0 ]; then
  6. sudo service mysql restart
  7. fi

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

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

Hope that this helps!

KFSys
Site Moderator
Site Moderator badge
February 7, 2024

Heya @renatov,

It sounds like you MySQL gets killed due to insufficient RAM. Apart from what has been mentioned, I would suggest for you to check/add SWAP

https://www.digitalocean.com/community/tutorial-collections/how-to-add-swap-space

Swap is a portion of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in RAM. This lets you increase the amount of information that your server can keep in its working memory, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.

While you tune your MySQL as well.

If you are still experiencing the same issue, then upgrading your Droplet will be your next step.

Bobby Iliev
Site Moderator
Site Moderator badge
February 6, 2024

Hey!

It sounds like that your server might be running low on RAM and the MySQL service might be getting killed.

I could suggest a few things here:

  • You could do is use the MySQLTuner script. Here is a quick tutorial on how to use it:

https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

  • You can also add a Swap file so that you could have some extra memory buffer in case that your server runs out of RAM:

https://www.digitalocean.com/community/tutorial-collections/how-to-add-swap-space

  • And finally you should also consider adding more RAM to the server as well.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel