Error Establishing a Database Connection WordPress Fix

How to fix the “Error Establishing a Database Connection” error in WordPress. This is one of the most dreaded error in WordPress. In this WordPress tutorial, we will see about the causes of this WordPress error and how to fix it. This error is frustrating for both the users as well as the WordPress blogger. Site downtime is costly and it will give a bad reputation. “Error Establishing a Database Connection” is thrown at the face of the user and we should be well aware of this WordPress error to treat it instantly.

A word of caution. Before doing any experiment for learning purposes, do remember to backup your WordPress.

Why does this WordPress Error Occur?

The reason for this WordPress error to be infamous is the causes behind it. There are many reasons that can cause this error.

Incorrect WordPress database configuration.

WordPress database corrupted.

WordPress files corrupted.

Database connection overload.

Incorrect WordPress database configuration

The first and foremost reason for “Error Establishing a Database Connection” error is, we forget to update the database configuration parameters. We might have a local WordPress installation and when changes are uploaded, wp-config.php with the local connection settings can get uploaded. Changing the password in Database and forgetting to update it in wp-config.php file. All these are related to human errors. So the first place you got to verify if things are right is wp-config.php

I have heard instances that the database user’s access rights for the database have been revoked. So verify that the user is able to Read the database.

WordPress database corrupted

WordPress database can get corrupted. It has happened for me. When the DB gets corrupted, obviously the application will not be able to connect the database and do a query. When this occurs sometimes it is possible that wp-admin will work and only the blog/website may get the “Error Establishing …” error. WordPress provides a functionality to ‘repair the database’. We need to login to use this functionality and you need to make a change to the settings to enable it. Open the wp-config.php file and add the following line.

define('WP_ALLOW_REPAIR', true);

Now go to the URL http://www.site.com/wp-admin/maint/repair.php and repair the database. Remember to backup the WordPress before attempting this. Once you have completed the repair, first remove these setting change as it will allow anybody to access this WordPress feature.

‘siteurl’ value is inappropriate

There can be situations when we need to fix the WordPress by importing an old backup. When we are importing old backup we should verify that the ‘siteurl’ value is set appropriately. If you are in doubt run the following query against your WordPress database.

UPDATE wp_options SET option_value='your_siteurl' WHERE option_name='siteurl';

WordPress files corrupted

One popular solution to this issue is uploading the WordPress files again. Just delete your WordPress files, as always remember to do a backup. After deleting, upload a fresh set of WordPress files. For many people, this solution has worked. The reason behind this fix is, the WordPress files may have got corrupted. After uploading a fresh WordPress, you should retain the wp-config.php and wp-content folder. Importantly the uploads folder.

Database connection overload

Your WordPress database can go down primarily for two reasons. You have outgrown your plan bandwidth. Which is in a way happy news. Check if you have installed a cache plugin like WP Super Cache. If the cache is set and still you have got database issues, then most probably you need to acquire more DB bandwidth from your hosting provider.

The second reason could be because of your neighbor. In a shared hosting environment, a bad neighbor pulls a whole cluster down. In such a scenario, you can ask your hosting provider to move your database to a decent cluster.

Some parting tips,

Keep your WordPress slim by using a minimal number of third party plugins.

Always install only a popular plugin. Install a good cache plugin and keep its settings optimized.