I have faced this error umpteen times and have always struggled to get rid of it. Hence I present you the solutions through this article. There is no necessity to panic!
This error message “Error establishing a database connection” looks pretty serious.
Understanding of the problem
WordPress uses two main technologies: PHP and MySQL.
- PHP is a programming language. WordPress core files are written on it.
- MySQL is a database management system (DBMS). WordPress uses a MySQL database to store site content: posts, pages, and site title, widget layout, etc.
The error discussed in this article occurs when WordPress cannot access information in the database using PHP commands. When this happens, WordPress displays the message “Error connecting to database”.
Several reasons this error occurs:
- Invalid login credentials. If the password has changed, WordPress will not be able to receive information from the database.
- Corrupted WordPress files. This can occur when updating plugins, themes, and WordPress itself.
- Corrupted database. For example, if a malicious plugin damaged the database from the inside. And also due to a hacker attack, a failure in the topic, etc.
- The database server is down.
- A large amount of traffic. Because of this, the database cannot respond to queries. For example, if one of your posts has become “viral”, and at the same time, many users are trying to access your site.
How to fix the “error establishing a database connection” error
First I recommend a way that will quickly solve the problem. Then I will dig deeper.
Step 1: Contact Your Hosting Provider
If your site worked a minute ago, and now it is unavailable, then an error may occur on the hosting side. Quality hosting offers quick chat support.
In it, tell us that you had the error “Error establishing a database connection” and ask:
- whether the database server is running;
- whether there are jumps inactivity or traffic on the server now.
Hosting support will inform you whether your database is in order and whether there are suspicious actions on the server.
If the problem is not on the server-side, go to step 2.
Step 2: Check if plugin files have been corrupted
For example, maybe you updated the plugin or theme, and everything crashed. Or connected the site to an external service, edited the files manually. If you did not, skip this step.
What can be done:
- Connect to the hosting via FTP. You can do this with the free FileZilla tool.
To connect, you must enter the username and password for your FTP account. The easiest way to get them is to look for the first emails you received from the provider. And also request them from the host’s support service.
Then connect to the server and go to the root folder of WordPress. Usually it is called “public_html” or “www”.
If you see in front of you subfolders like “wp-content” and “wp-admin”, then you are at the root of WordPress. Example: Go to the wp-content folder and rename the plugins folder. Set any other name to the plugins folder.
Return to the site and check if the problem is resolved.
If the error is resolved, now you know that it was caused by some kind of plugin. Go back to the server, rename the folder back to “plugins”. Go into it and change the folder names of individual plugins. The easiest way to do this is to add an underscore at the end of the name: “_”
Continue until you find out when the error occurs and when not. Once you narrow your search to one specific plugin, delete it or find a solution to the problem on the Internet.
- If the error persists after renaming the plugins folder, perform the same process for the themes folder.
- If this does not help, rename both folders back to “plugins” and “themes”. The error is related to something else.
Step 3: Check if the database has been corrupted
- Go to your WordPress dashboard – YOURSITE.com/wp-admin
If you see the same error: “Error establishing a database connection”, skip the step.
- Pay attention to what WordPress offers you to do. One of the options:
If you see a message starting with “ One or more database tables are unavailable […]”. This means that you need to “repair” the database.
Open it in a text editor and a line at the end (right before “ That’s all, stop editing” ):
Now upload the file back to the site (via FTP). Go to YOURSITE.com/wp-admin/maint/repair.php
Here is what you should see:
Press any of the buttons. After the automatic recovery is complete, return to the site and see if the error is fixed.
If there is no error, great! But you still need to return the “wp-config.php” file to its previous state. Delete the line you added and reload the file.
If the error persists, continue to the next step.
Step 4. Verify the database connection credentials.
If the password and login from the database have changed, the error “Error establishing a database connection” is raised! Its correction is performed in the wp-config.php file. I am talking about these lines:
They contain all the information WordPress needs to connect to the database. If something in it is incorrect, the connection will not be established.
Copy the above lines, paste into the chat window and ask the hosting support service if the data for connecting to your database is correct. If not, the specialist will provide the correct values for authorization.
Copy the new credentials and put them in the wp-config.php file.
Step 5: Recover WordPress Files
WordPress core files can be corrupted for a number of reasons. But you can restore the default WordPress files. The easiest way to do this is to download a clean version of the engine from WordPress.org and install it on top of the one you have.
Follow this link and download the latest version of WordPress. Unzip the archive and delete the wp-content folder from it.
Next, connect to your site via FTP, go to the root of WordPress, upload new CMS files. Overwrite existing files.
When the download is complete, return to your site and check the result. If this does not help, contact the hosting support service again. Tell them about everything you have done and ask for help.
How to avoid the Error establishing a database connection in the future?
There is no ideal way to protect the site from database connection errors. But the likelihood of these problems can be minimized by adhering to some rules:
- Only work with hosts that have optimized their servers for working with WordPress. Thanks to this, you can always count on qualified help support.
- Install a simple backup plugin such as UpdraftPlus. Thanks to it, you will always have a working backup of WordPress.
I hope this guide helps you.