How to Fix “Error Establishing a Database Connection” in WordPress

One fine day, as you’re happily visiting your website, you suddenly realize that it isn’t displaying what it’s supposed to.

Instead you are greeted by a baffling error message that simply states, “Error Establishing a Database Connection” on virtually every page you attempt to visit.

Although not very common, the error is notorious for showing up seemingly out of nowhere, as you have not knowingly made any drastic changes on the back-end to your knowledge. What could this WordPress database error mean, and how can you fix it?

Needless to say, letting this issue go unresolved for several days could impact your overall active or passive income.

Potential Causes:

It’s important to understand the potential causes for this error message before attempting to fix it. One of the following is typically responsible for generating this issue:

Plugin configuration: A plugin could be incompatible with your current WordPress blog due to conflicts with other existing files. Otherwise, said plugin could be poorly developed and thus lead to a database connection error.

Corrupted database or incorrect credentials: WordPress will not be able to establish a database connection if you had recently changed your username, password and/or database name. Believe it or not, this can occasionally go undetected if you recently switched hosting companies or performed other sensitive changes on the back-end.

Hosting server error: Another possibility might be a problem with your webhost. For example, your server may have crashed temporarily due to technical issues or simply because of a sudden spike in traffic to your website, especially if you’re on a shared server.

How to Fix “Error Establishing a Database Connection” on WordPress:

The following techniques are written in order of easiest to hardest, the latter which has a greater potential of fixing the error if it’s indeed a core database issue.

Disable the latest plugin: First let’s try some basic steps, which could quickly get your website up and running without causing any headaches. This usually involves retracing your steps and undoing your last action from the back-end of your website.

Had you installed or modified a plugin or its settings recently? Was such plugin recently updated? If so, go ahead and visit the back-end by typing, “Yourwebsite.com/wp-admin” into your browser.

But wait, are you even able to log into your WP admin panel in the first place? Are you receiving the same or a similar error such as, “One or more database tables are unavailable. The database may need to be repaired”?

If this is the case, you must access your website’s core files via FTP or through your cPanel’s main file manager tool.

Navigate to your website’s Plugins folder, typically located here: “Yourwebsite.com/public_html/wp-content/plugins.” The exact path may change slightly depending on your existing configuration.

Next, disable the last plugin you installed by renaming its folder. If your last plugin’s directory is called “SEO Plugin,” for example, simply rename it to something like “SEO Plugin-disabled.”

Now go back and visit your website to see if the problem is resolved. If you’re still seeing “Error Establishing a Database Connection” display, then optionally rename the entire “Plugins” directory that contains all of your website’s plugins. This will disable every single one of them, and it’s a good step to try in order to cover all your bases.

Move on to the following step if you are still experiencing the same problem.

Repair your database: Thankfully, WordPress provides a user-friendly method of repairing your database if the issue was caused by known bugs and occurrences.

Simply browse to your website’s main WordPress installation using your cPanel’s file manager tool or FTP program again, as described earlier.

Then, locate and open a file called “Wp-config.php” which is typically found at the root of your website’s WordPress installation.

Enter the following line at the bottom of the Wp-config.php file:

define( ‘WP_ALLOW_REPAIR’, true );

Save/update the file, then visit the following URL from your browser:

“Yourwebsite.com/wp-admin/maint/repair.php” while replacing “Yourwebsite.com” with your actual URL.

You will see a screen like the following:

wpdb

As we are specifically trying to fix a database problem, click “Repair Database” and allow a few minutes for the procedure to finish.

Note: It is crucial that you delete the above line in your Wp-config.php file to prevent others from accessing the database-repair screen, as this is publicly accessible without the need for user credentials.

If this does not fix the “Error Establishing a Database Connection” message, you will need to dig deeper into the actual database. At this point you should back up your website if possible, or at least make a database backup directly through your cPanel.

Confirm your database credentials: As mentioned earlier, your user credentials may unknowingly change if you had recently acquired a different webhost package and/or altered any sensitive information. So let’s look at your current information to ensure they are correct:

Re-open your website’s Wp-config.php file, which is found at the root of your website’s WP installation. You should see the following lines toward the beginning of the file, with slight variations on your end:

/** The name of the database for WordPress */
define( ‘DB_NAME’, ‘INSERT DB NAME’ );

/** MySQL database username */
define( ‘DB_USER’, ‘INSERT USERNAME’ );

/** MySQL database password */
define( ‘DB_PASSWORD’, ‘INSERT PASSWORD’ );

/** MySQL hostname */
define( ‘DB_HOST’, ‘INSERT DB_HOST’ );

You will receive the ominous error message if any of these are incorrect. To compare the above details against your actual WordPress database, you will need to launch a program called “PHPMyAdmin” from your webhost’s main cPanel.

Once inside PHPMyAdmin, browse for your website’s database name from the left pane. Its name should be similar to the value you see next to “DB_NAME” on your Wp-config.php file.

Now click on the item called “WP_Options” and see if the details inside pertain to your website. If you see the correct URL listed, for example, now you know the database name is correct inside your Wp-config.php file. If not, replace the existing name with the one on PHPMyAdmin.

Check your username and password: If the error persists, let’s make sure that your username and password are also intact. The easiest way is to simply create a new user ID and password, then updating your Wp-config.php file with those new details.

Browse for a program called “MySQL Databases” from your webhost’s cPanel dashboard. On the following screen, click “Add New User” and enter a new username and a strong password. Once done, enter this information into your Wp-config.php file, specifically after “DB_USER” and “DB_PASSWORD” respectively.

Finally, click “Add User to Database” and select the username and your website’s database from the list shown. Now everything should match (username, password and database) thus allowing WordPress to connect to your website’s database.

Checking your DB_HOST: If the above still haven’t fixed the error message, check your Wp-config.php file to make sure that “DB_HOST” matches the one used by your webhost.

Most companies use “localhost” in this field, but you should check this list to make sure you are using the correct one:

Possible DB_Host Values:
http://codex.wordpress.org/Editing_wp-config.php#Possible_DB_HOST_values

If you cannot find your company on this list, contact your webhost and ask them what value should go under the DB_HOST field.

Still having problems?

If all else fails, it’s quite possible that the issue might be on the webhost’s end, as a server-related issue may indeed lead to the “Error Establishing a Database Connection” message.

Contact a representative and let them know everything you have tried so far, while suggesting that they check for your user permissions. In some cases, user permissions may reset depending on the severity of changes performed by you or the server team.

Have you found a solution for fixing your WordPress database not mentioned here?