How to Fix The Error Establishing Database Connection on WordPress

Even though it’s a solvable problem, the “error establishing database connection” is indeed a bother to encounter.
Now, there are plenty of ways through which this error can occur. The problems range from corrupted website files, plugins, themes, to even issues within the WordPress core itself.
Armed with an understanding of the problem, however, you can solve this issue in no time. Pretty soon, you’ll have a site that shows these errors from a minimum to none.
In this tutorial, we’ll be discussing four ways in which you can fix these errors. So, let’s begin!
Database Connection Errors: Explained
Unlike the peskier, “under-the-hood” WordPress code errors, this one is easy to recognize.

Okay, so we know what the error is, what now?
Well, there are plenty of issues that can inhibit a secure website-database connection. Regardless of the issue that caused the problem, your site gets crippled quickly. If you’re ever faced with this error, you need to troubleshoot it fast.
Here’s how:
Fixing the Database Connection Error
At the very basic level, WordPress often fails to establish a connection due to a credential error. In such a situation, you should ensure that you log-in with the correct username and password.
If you’ve logged in with the right credentials and the error is still showing up, you need to dig deeper. The issue might arise due to errors in your hosting service and its external hosts. In that case, it’s best to take this issue up with your hosting provider.
Now, let’s talk about the four ways through which you can troubleshoot this error on WordPress.
1. Restoring Your Site Through Site and Database Backups
Having a backup plan in case of emergencies can prove beneficial for your site.
Make sure you have a working backup of your website available. Not only should it contain a copy of your website, but your database files as well. It helps you restore the entirety of your website in the event of an emergency.
But if you’re unable to access your website, how can you restore your site to a previous state? The backup plugin doesn’t work, so what now?
Well, for that, you need to depend on the backup functionality provided by your web host. With it, you can restore your site directly from the backend.

If you can access your website, you can back up from there. If not, the cPanel backend is your best friend.
2. Analyze Corrupted Files, Themes, and Plugins
Another major cause of this error is because of compatibility issues. Incompatible themes, plugins, and corrupted files inhibit successful connection to the database.
This incompatibility can occur even while you’re following the best practice of keeping your plugins and themes updated.
Similar to the case above, you’re still unable to access your WordPress dashboard. You’ll need to start up an FTP client like FileZilla and perform troubleshooting from there.
Have you connected your FTP? Good. Now, you need to folder hop and navigate to the wp-content folder. When you’re there, you need to find the plugins and themes folder.
When you’ve found the plugins folder, rename it. You could name it anything, but the best practice is usually renaming it to plugins.disabled. Now, all your plugins will get disabled.

Now try logging-in to your WordPress dashboard. If the error is now gone, it means that one of the plugins was causing the issue.
To remove the culprit, go to your FTP client again and revert the plugins.disabled folder to its original name and log into your website. Now, log back into your WordPress dashboard and navigate to the Plugins section. Enable each plugin, log-out, and then log back in again.
If, for example, you reactivate a plugin, log-out, and can’t log in again, you can be certain that the plugin that’s causing the problem. To solve it, you should revisit your FTP and delete that plugin from the backend.
If the issue persists, it’s time to troubleshoot whether the compatibility issue resides within a theme.
The process is similar to the plugins. From your FTP, visit the wp-content/themes folder and see the folder that contains your active theme.

Instead of changing the name of the entire theme folder, change the name of the active theme. It will disable that theme and cause WordPress to revert the site to a default version.
Now log-in to your WordPress dashboard. If you can’t log in without facing any errors, there might be a compatibility issue with your theme.
What about your theme? It would help if you ideally reinstalled it once your site loads well. If you’ve reinstalled the theme and it’s still showing such errors, it’s time you start looking for an alternative theme that doesn’t cause such issues.
3. Reinstall the WordPress Core While Keeping Your Content
If the preceding step doesn’t solve the issue, it’s time you look at the WordPress core itself. Often, the WordPress core files get corrupted and cause this problem.
The easiest way to solve this issue is to install a “clean” WordPress installation from the official WordPress website.
Once you’ve downloaded your fresh WordPress installation, extract the zip file. Now, remove the wp-content folder to prevent duplication or overwriting with your existing themes, media files, and installed plugins.
To upload the new files, you need to start up your FTP. From there, access the root folder. Next, upload the files from your computer to the root directory.
During this entire process, the FTP client will ask whether you want to overwrite the server’s files. Just say yes, each time. Don’t worry; the fresh installation will have your older content as well.
When you’re done, log-in to your website again. If it still fails, don’t fret. We still have one more trick up our sleeve.
4. Utilize WordPress’ Database Repair Functionality
You might be surprised to hear that WordPress comes with a database repair functionality. If your database is corrupted, and after everything you’ve tried fails, this is your last line of defense.
To access this functionality, you need to enable it from the backend. For that, you have to open up your FTP again and visit the folder titled “root” and look for the wp-config.php file. Open the file in a text editor.

Navigate to the bottom of the code and search for a line that reads, “That’s all; stop editing.” Alternatively, you can open up the search box and type in the text to find it faster. Once you’ve found it, add the following code before it:
define(‘WP_ALLOW_REPAIR’, true);
Save your changes and visit the following URL to start up WordPress’ database repair functionality:
yourwebsite.com/wp-admin/maint/repair.php
Instead of “yourwebsite.com,” you would want to write the name of your domain. Once you hit enter, you’ll be welcomed by the following screen:

Just click on Repair and let WordPress perform the fixes. When it’s done, it shows a success message. Now, log-in to your WordPress website and if it’s a success, congratulations!
If the problem doesn’t get fixed even after this, you should contact your hosting service. Getting them involved will help you solve the problem at an even more granular level.
Conclusion
A WordPress website can’t work without a database. When WordPress tried to fetch your website and can’t connect to the database, it breaks your site. The causes for this error can vary from website to website.
In this article, we talked about the four ways of troubleshooting the error establishing database connection. Namely, we talked about four ways you can troubleshoot:
- Restoring your site from a previous backup.
- Check for compatibility issues within themes and plugins.
- Reinstalling WordPress core.
- Repairing the database.
Is troubleshooting errors too much of a task? Contact us for expert WordPress development.
Read Also