error establishing a database connection

How to Resolve the Error Establishing a Database Connection in WordPress

October 06, 2020

Are you exploring to get resolved from the error establishing a database connection in WordPress? WordPress is one of the most common open-source content management systems in the world. Although it started focusing 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. Let’s walk through these issues one at a time to determine if they affect you, and how to fix them.

What is the Error Establishing a Database Connection?

All of the information on your WordPress website, such as post data, page data, Meta information, plugin settings, login information, etc. is stored in your MySQL database. The only data that isn’t saved there is media content such as images and your theme/plugin/core files such as index.php, wp-login.php, etc. When someone visits your website, PHP produces the code on the page and queries the information from the database, which then displays it to the visitor in their browser.

If for some reason this isn’t working correctly, you are left with the Error establishing a database connection message, as seen below. The entire page is blank because no data can be retrieved to render the page, as the connection is not working correctly. Not only does this break the frontend of your website, but it will also prevent you from accessing your WordPress dashboard. However, visitors might not see this Error on the frontend right away. That is because your site is most likely still serving from cache until it expires.

What Causes This Error?

While the meaning of the error message is exact, the reasons behind its occurrence often aren’t. There are many speculations why your website could be having trouble connecting to your database, but they generally fall into three categories:

  1. Your login credentials for your database are wrong. Your database works separate login credentials (username and password). If those credentials have recently been changed, your website won’t be able to access the database with the old certificates.
  2. Your database is corrupted. Any number of things could cause your WordPress database to become corrupted, from uploading a lousy plugin to lightning strikes to the server (seriously — though luckily that isn’t too common).
  3. The server that hosts your database is down. Your database’s server may be down due to a query or error on the part of your web hosting company, or you could be experiencing a big traffic spike that your server couldn’t handle.

So with all these possibilities, how can you narrow down the problem and fix it?

wordpress error establishing a database connection
wordpress error establishing a database connection

How to Fix Error Establishing a Database Connection

Fix #1: Restore a recent backup that includes your database

In short, you should have a recent backup of your website available at all times. This backup should also include a copy of your database so that you can restore the entire website to a working state before the connection error.

Restoring a backup without access to the WordPress admin can be tricky, though. If you depend on a plugin solution, you might be out of luck altogether.

Ideally, you’d have already set up automatic backups with your web host – and you can usually restore them from your control panel.

If this applies to you, fixing the Error establishing a database connection in WordPress is a matter of simply choosing which backup to use.

On the other hand, if you only have access to manual backup files, you may need to upload them manually. This means both are uploading your WordPress files and using a tool such as phpMyAdmin to restore a database backup.

Fix #2: Reinstall WordPress core files

So far, we’ve ruled out plugins and themes as from being behind the Error establishing a database connection in WordPress. Now, let’s check if some of your WordPress core files are the ones causing problems. The simplest way to fix this is to replace all your core files with a ‘clean’ version of WordPress. To do this, download the latest version of the core WordPress software from WordPress.org.

Once you have the file, unpack it and delete the wp-content the folder in the downloaded files, so you don’t overwrite your plugins, themes, or media files. To upload the clean files, access your website via FTP, and navigate to your site’s root folder. Next, upload all the WordPress files from your computer.

This process might take a while, and you will need to say yes when the FTP client asks if you want to overwrite the files on your server. Also, bear in mind that if you’ve made any modifications to core files, such as your site’s .htaccess file, this process will overwrite them. When the upload is done, go ahead and try to access your website once more. If the error persists, it’s time to bring out the big guns.

Fix #3: Use WordPress’ database repair functionality.

One thing you may not know about WordPress is that it comes with a database repair function, which can help you fix corrupted tables. Before you can use this feature, you need to enable it. To do so, access your website via FTP, head to your root folder, and look for the wp-config.php file. Next, open it using a text editor.

Scroll to the bottom of the file and see for the line reading that’s all, stop editing. Now add the following code before this line:

define(‘WP_ALLOW_REPAIR’, true);

Once you save the changes to wp-config.php, you can access WordPress’ database repair feature by visiting the following URL:

mywebsite.com/wp-admin/maint/repair.php

Replace the mywebsite.com placeholder with your domain, and you’ll see the following screen. When you’re ready, click on the Repair Database button and let WordPress do its thing.

When the repair is over, you’ll see a success message, which is your cue to check if the Error establishing a database connection in WordPress message is gone. If you still see the error, it’s a sign that your host should get involved. We’d recommend reaching out to them for a fix to the Error.

Leave a Reply

Your email address will not be published. Required fields are marked *

X

Get a Free

SEO Consultation