Question

Repeated Error establishing a database connection

I recently set up a wordpress website and occasionally I get the above error message. That is initially I get connected without any problem and after a few minutes the server is not responding returning the above error. When I’m trying to log in again there is no response but after a few minutes everything works ok again. This"loop"process keep happening.

Can you please provide me with a solution to this problem?


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.

KFSys
Site Moderator
Site Moderator badge
May 18, 2021
Pinned Answer

Hi @app260866,

This crash is most likely due to your system running out of memory. I’d suggest that you add a swap file to give yourself a bit more of a buffer. Check out this tutorial:

How To Add Swap on Ubuntu 18.04

What you can also do is to use the MySQLTuner script.

The MySQLTuner is a script written in Perl and allows you to quickly test your MySQL configuration and it gives you suggestions for adjustments to increase performance and stability.

According to the official GitHub page, it supports 300 indicators for MySQL/MariaDB/Percona Server in this last version.

To run the script you could do the following:

  • SSH to your Droplet
  • Download the script:
wget http://mysqltuner.pl/ -O mysqltuner.pl
  • Then execute it:
perl mysqltuner.pl

The script would run multiple checks against your MySQL instance, all checks done by MySQLTuner are documented here.

Also as stated in the official documentation, it is still extremely important for you to fully understand each change you make to a MySQL database server. If you don’t understand portions of the script’s output, or if you don’t understand the recommendations, you should consult a knowledgeable DBA or system administrator that you trust.

As a good practice make sure to always test your changes on staging environments before implementing them on your production database.

On the same note, if you want to have worry-free MySQL hosting and focus on your application, I would recommend trying out the DigitalOcean Managed Databases:

https://www.digitalocean.com/products/managed-databases-mysql/

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

Additionally, answer taken from @bobbyiliev from this question :

https://www.digitalocean.com/community/questions/help-mysql-keeps-crashing-error-establishing-a-database-connection

alexdo
Site Moderator
Site Moderator badge
May 16, 2021

Hello, @app260866

If you’re using Ubuntu the path to the MySQL errog_log should be /var/log/mysql/error.log so if you want to check the last 100 entries of the log you can use the following command

  1. tail -n 200 /var/log/mysql/error.log

and monitor it in real-time:

  1. tail -f /var/log/mysql/error.log

Hope that this helps! Regards, Alex

KFSys
Site Moderator
Site Moderator badge
May 14, 2021

Hi @app260866,

This looks like your Database server is crashing and restarting itself.

I’ll recommend checking your MySQL/MariaDB error logs to see what’s going on. Basically, SSH to your droplet and tail the log:

tail -f /path/to/mysql/log

And when your Website displays an error establishing a database connection, you’ll see what was the error once it’s logged.

Reasons behind these failures might be memory or CPU shortage, you might need to upgrade if your Website is too heavy or maybe there is an issue with some of the Databases inside your MySQL/MariaDB instance but again this will be logged in your error_log.

Regards, KFSys

Try DigitalOcean for free

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

Sign up

Featured on Community

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