Oct 16, 2011

Error establishing a database connection – WordPress

When I imported production database and configured code on my local dev machine, I got this error even I had updated wp-config.php with correct information. I fixed the problem after some changes. This article explains troubleshooting steps to fix this error.

1. First, Make sure, MySql is running. Open PhpMyAdmin and connect the database.
2. Check User has sufficient permission to connect database. For this, Create a test page say testconnection.php, put following code and run it.

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

3. If above code is working fine then Open wp-config.php, Make sure you have defined correct information, No additional spaces/tabs in following parameters:

4. For WordPress Multisite:

Suppose you have to configure wordpress in Xampp> htdocs > techbrij folder. The final URL would be

then check DOMAIN_CURRENT_SITE, PATH_CURRENT_SITE and $base parameters in wp-config file.

DOMAIN_CURRENT_SITE: localhost(your dev server)
PATH_CURRENT_SITE: /techbrij/(your dev folder)

If It’s okay then open database and check wp_blogs and wp_site tables have correct domain name and path.

5. Open wp_options table and check HOME and SITEURL parameters, See here to configure production URL to dev URL.

6. Still getting error, turn off Multisite functionality in wp-config if exists and test.

define( ‘MULTISITE’, false);

7. Open admin side of WordPress and check whether it is connected or not. If it is connected means problem is related to plugin. Disable cache plugin or other one by one and test it.

Hope, It helps. If you have any other step or opinion to fix this, share it in below comment box.

  • James Luff

    I’ve followed your instructions, everything checks out fine but I still get the “Error establishing database connection”. I have a clean WordPress install in another directory which works just fine. I’m stumped :-(

  • Century21


  • Brian Fegter

    I found that my wp_options table was corrupted. Just run a REPAIR TABLE table_name and it should fix it if your connections are good.

  • Sometimes, I get this error on my WordPress site front-end, I haven’t faced such issue in the back end. This article will certainly help the WordPress users who are facing this database error.

    Thanks for the tip !

  • Sorry for being eventually dumb, I get a “Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘root’@’localhost’ (using password: YES) in /home/triod039/public_html/testconnection.php on line 2

    Could not connect: Access denied for user ‘root’@’localhost’ (using password: YES)” message launching the php script, do I have to change the three values (‘localhost’, ‘root’ and ‘password’) using the ones in my configuration? I miss the point :(

  • Anonymous

    any new thoughts about this?
    I can’t get any connections, or anything to work for that matter. I’m stuck with the db connection step.

  • Tom Clarke

    It may sound silly… but check your cookies too! I just spent ages trying to fix this but foolishly had not checked in another browser (a cardinal rule!). Cleared cookies and the error is gone.

  • Domiziano Galia

    After half a day spent uselessly I found out that I had the same problem. A Repair Table on wp_options worked it out. But I still don’t get what happened (last time I managed the site was working ok) and if there’s a fast way to understand that a table is corrupted. Thanks.

  • Anonymous

    I think that step 7, “problem is related to plugin” is my main issue.

    My site was working couple minutes ago, then suddenly I got the “Error Establishing a database connection…”

    Thanks for these tips, will disable plugins one by one and test it again. Hopefully it will works

    Really appreciate it:)

  • Kdermendzhiev

    I need my multisite working, but it doesn’t work.

  • Claude S

    Thank You I just add to disable multisite . to find the bug . I wish you blog was in the first page of google .It would i save me precious sleeping time !!

  • If you’re faced with the excruciating Database Connection Error on your WordPress website, there are a number of factors which could be causing it. Perhaps you overlooked something in trying to fix it. Also check out this blog post which shares some good tips to overcome the Database Error. Details: http://www.cloudways.com/blog/how-to-fix-the-error-establishing-a-database-connection-wordpress/