Error Establishing a Database Connection WordPress Fix

How to fix the “Error Establishing a Database Connection” error in WordPress. This is one of the top 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 do this WordPress Error Occurs?

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

/** The name of the database for WordPress */
define('DB_NAME', 'database_name');

/** MySQL database username */
define('DB_USER', 'root');

/** MySQL database password */
define('DB_PASSWORD', 'password');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Following is a PHP code snippet to verify if your database connection setting are right. Create a new PHP file and put it inside the WordPress installation with the following content and invoke it.

$db = mysql_connect('localhost', 'root', 'password');
if (!$db) {
die('Error connection database: ' . mysql_error());
echo 'Database connected successfully!';

I have heard instances that the database user’s access rights for the database have been revoked. So verity 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 sometime 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 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 and repair the database. Remember to backup the WordPress before attempting this. Once you have completed the repair, first remove this settings 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 a 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 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. 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.

Second reason could be because of your neighbour. In a shared hosting environment, a bad neighbour pull 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 minimal number of third party plugins.
  • Always install only a popular plugin. Install a good cache plugin and keep its settings optimized.
  • Keep a strong password.
  • Update the WordPress regularly.
  • Take frequent regular backups.

This Wordpress code tutorial was published on October 14, 2015.

↑ Back to Top