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.

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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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

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

Hello, @app260866

The Woocommerce plugin is likely to consume more memory and if you’re using a smaller droplet you might run into “out of memory” issues. You can check the logs for memory exhaustion.

You can execute the following commands:

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

I’d suggest that you add a swap file to give yourself a bit more of a buffer. Check out this tutorial:

https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-18-04

The other option will be to upgrade the droplet memory as well.

Hope that this helps! Regards, Alex