Question

Error establishing a database connection

Posted November 13, 2020 322 views
WordPress

I have hosted a website on DigitalOcean.
I deleted unused media through a plugin “Media cleaner” (approximately 2K images).
But it deleted some used images.
Before doing this I have made a backup of only uploads through the Updraftplus plugin.
So I restored the backup.
Reassigned shortcodes in oxygen.
But something happened and now it is showing “Error establishing a database connection”
How to solve this? It’s urgent. Thank You!

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
2 answers

Hello,

The “Error establishing a database connection” occurs when WordPress is unable to make a connection to the database. A number of things can affect your WordPress database connection.

This issue can be caused by incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server. You can refer to our community article for more information

https://www.digitalocean.com/community/tutorials/how-to-debug-the-wordpress-error-establishing-database-connection

One of the most common reason is MySQL stopped running. It stopped or failed to start as a result of not enough memory.

A useful program to review the logs one page at a time is less, for example:

less /var/log/mysql/error.log

If you see memory issues such as mmap can’t allocate then you can be sure that you are having memory issues. You can choose to optimize your MySQL configuration, or use caching mechanisms to reduce the application’s load on MySQL. If nothing works, you can try resizing your server to a one with higher CPU and RAM.

Hope this helps!

Cheers,
Lalitha

by Brian Boucheron
WordPress is one of the most popular open source content management systems in the world. Although it started out focused on blogging, over the years it has developed into a more flexible platform for websites in general. After almost fifteen years of development it is quite polished and robust, yet issues can still come up. If you've recently attempted to load your WordPress-powered website and instead saw a message stating "Error Establishing Database Connection", the cause is most often...
  • Thank you. The issue is solved. I followed the same guide you mentioned.
    Now whenever I try to use the media library I am getting “Error establishing a database connection” then when I restart database site is up.

Hello, @sushpawar2500

Have you checked the MySQL error_log in order to see if the server is running out of memory or if the issue is with to many MySQL connections? As per the tutorial you can run this command in order to check if the server have recently run out of memory:

zgrep -a "allocate memory" /var/log/mysql/error.log*

zgrep will search through log files, including older log files that have been archived as compressed .tar.gz files. We’re searching for lines that contain allocate memory, in any error.log* file in the /var/log/mysql/ directory.

You can always login and check the MySQL error_log when the issue occurs using tail to either check the last 100 entries in the log or monitor the log in real time:

tail -n 100 /var/log/mysql/error.log

and monitor it in real time:

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

Hope that this helps!
Regards,
Alex