How To Fix Error Establishing Database Connection Error

| November 1, 2012 | 0 Comments

error establishing a database connection
Are you getting this error when you try to visit your site? The reason behind this is WordPress is not able to coonect into the database for various reasons. It could happen if your database login credentials are wrong or have been changed, or your database got corrupted or if your web hosting server is having some problems. In this article, we will show you how to fix the error establishing a database connection in WordPress .We will show you best solutions all in one place.

First make sure you have sufficient backups before you make any database changes.

Why do you get this error?

We already mentioned some reasons why WordPress is unable to establish a database connection. It could be that your database login details are wrong or have been changed. It could be that your database has been corrupted or your database server is unresponsive.In most of the times this error happens because of some sort of server error but not always.Let’s find out how to fix this issue.

Can you access /wp-admin/ ?

Are you getting the same error on both front-end (homepage) and the back-end (dashboard) of the site?If you are seeing error message “Error establishing a database connection” on both pages, then skip onto the next step. If you are getting something like “One or more database tables are unavailable” or “One or more database tables are unavailable” error on the wp-admin, then your database needs to repaired.

WordPress Database Repair

You can fix this error by adding the following code in your wp-config.php file:

	define('WP_ALLOW_REPAIR', true);

Once you have done that, visit this page to see the settings: http://www.yoursite.com/wp-admin/maint/repair.php

database error
When you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.

If this repair did not fix the issue or you are still having trouble repairing your database then try following solution to make it work.

Checking the WP-Config file

WP-Config.php is probably the main and most important file in your entire WordPress installation. This file handles the details for WordPress to connect your database. If your root password changed, or the database user password, then you will have to edit this file as well. Check wp-config.php file to make sure everything in your is the same.

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

DB_Host value for popular hosts like HostGator, BlueHost, it is localhost. If you are using any other hosting provider than make sure DB_Host value is correct.
When running WordPress on a local server environment, for example on MAMP, you could replace localhost with the following IP address.

	define('DB_HOST', '127.0.0.1:8889');

IP’s will vary for online web hosting services so you will have to ask for host for it.

If all details in this file are correct (check for typos), then there is probably something wrong on the server end.

Check your Web Host (MySQL Server)

Often you will notice this Error establishing database connection when your site gets a lot of traffic and your host server cannot handle the load (specially on shared hosting). The site will usually get very slow and for some users even show the error. If this is the case you should get on the phone or livechat with your hosting provider support and ask them if your MySQL server is responsive.

If you want to test if MySQL server is running yourself, you can do a few things. If you have other sites on the same server , and they are also getting the same error, then most likely there is something wrong with your MySQL server. Another option is to simply go to your cPanel and try to access phpMyAdmin and connect the database. If you can connect, then we need to verify if your database user has sufficient permission.
Create a new file called testdb.php and paste the following code in it:

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

Make sure to replace the username and password. If you can see “’Connection ok”, then it means that your user has sufficient permission, and there is something else wrong. Go back to your wp-config file and double check everything to make sure all details there are correct (re-check for typos).

If you cannot connect to the database by going to phpMyAdmin, It does not necessarily mean that your MySQL server is down.It could also be that your user does not have sufficient permission.

So if you get the access denied error in either connecting to your phpMyAdmin or through testdb.php results, then you should contact your host right away to get them to fix it. Your host will be able to get the site back live. Also many webmasters claim to fix these and similar errors by simply uploading a fresh copy of WordPress.
Before you try any of our tips mentioned above, make sure that you have everything backedup in case if anything goes wrong.
If none of our tips worked for you, and you are still getting “unable to establish a database connection” error message, than use our contact page to let us know.We will fix it for you for free.

Do leave a comment below if you have any questions or tips how to fix these kind of errors.

Tags:

Category: TUTORIALS

Leave a Reply

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