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?
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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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.
Run this script every 5 minutes using a cron job like this one:
Hope that this helps!
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.
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:
Let me know how it goes!
Best,
Bobby