«

»

Nov 01 2014

Print this Post

How to fix WP Error Establishing a Database Connection

The Error Establishing a Database Connection error is a very common issue on WordPress. This issue is caused by a connection failure between the WordPress CMS and the SQL database. Here is a simple guide to fix the problem.

A typical error message displayed on site.

A typical error message displayed on site.

Finding the cause

This can be caused by several issues. If you are getting the same error, “Error Establishing a Database Connection”, on the main front page as well as the wp-admin section, then the problem is most likely the credentials. This is the most common reason for this issue. However, if the error message on the wp-admin section is different from that of the front page, then it is most likely caused by a database corruption. I will explain several methods to fix the problem.

Fix wp-config file

From the back-end of your server, open the wp-config.php file in an editor. If you are using cPanel, then you can do this from file manager. If you have your own server, you can access the file either using FTP or remote desktop connection. Check if the following parameters are setup properly in wp-config.php.

define('DB_NAME', 'database_name');
define('DB_USER', 'database_username');
define('DB_PASSWORD', 'database_password');
define('DB_HOST', 'localhost');

The information in wp-config.php file should match the corresponding current database user account. These accounts should be listed under under MySQL Databases section of your server. On cPanel it is located under Databases sub section.

MySQL in cPanel

MySQL in cPanel

Typically the database name is composed of your username_databaseName. All the current databases and users will be listed within the MySQL admin page (example shown below).

List of current databases and users.

List of current databases and users.

If the exact same username appears in the MySQL current list as it is in the wp-config, then it may be the password. You cannot check what password is used for each user account. But you can change the current password. To do that, under MySQL Users –> Add New User, enter the current username, Then enter a password you prefer. This will update the existing account.

Updating the password for a user.

Updating the password for a user.


Fix corrupted database

As mention above, if the error message on the wp-admin section is different from “Error Establishing a Database Connection”, then it is caused by a database corruption. WordPress comes with a built in repair method in repair.php. First add the following to the top of wp-config.php file.

define('WP_ALLOW_REPAIR', true);

Then run the following file located at, http://domain.com/wp-admin/maint/repair.php. It will provide you with “Repair Database” and “Repair and Optimize Database” options. Choose either one to fix the issue. Once the repair is successful (or not), please delete the WP_ALLOW_REPAIR from the wp-config.php file. With the code in place, anyone one access the repair page without administrative account.

Connection to database

This can be tested by running a simple PHP script. Create a PHP file with the following code such as test.php.

<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

If it provide a feedback with a successful connection, then your database is working. If not, either the database is corrupted or the sever files are damaged. Contact your hosting company for help. If it is your own server, then your best hope is to replace the MySQL files with available backup.

Permanent link to this article: http://sanuja.com/blog/how-to-fix-wp-error-establishing-a-database-connection