Error establishing a database connection

Date: April 8th, 2010
Author: Stefan Batanov

Error establishing a database connection in WordPress is one of the worst errors you may get and in my opinion the hardest to troubleshoot as we all know that the developers of WordPress made it really hard for non experienced users to get some useful stuff from the logs. In this tutorial I will try to guide you how to troubleshoot this error and get your wordpress blog up and running once again.

I want to explain that what follows are general instructions how to proceed if you get this error. I will try to generalize the approach to find the root cause of the issue.

The prerequisites are you run the latest available version of WordPress, you use Apache webserver , and you use mysql database. That is pretty much 95% of all WordPress users so we hope to be helpful to majority of the users.

First and most important step is to find out if the username and password are working. All hosting companies offer phpmyadmin to test the connection to the database server. I will not cover instructions of how to do that as all hosting companies offer different control panels. The best advice if you do not know how to test the username and password is to call your hosting provider. The configuration settings about your database login credentials are stored in wp-config.php file. They look like this

  1. // ** MySQL settings – You can get this info from your web host ** //
  2. /** The name of the database for WordPress */
  3. define('DB_NAME', 'site2');
  4.  
  5. /** MySQL database username */
  6. define('DB_USER', 'site2');
  7.  
  8. /** MySQL database password */
  9. define('DB_PASSWORD', 'site2');
  10.  
  11. /** MySQL hostname */
  12. define('DB_HOST', 'localhost');

Once you have verified that these work in mysql server or directly you test it form the command line(mysql -h localhost -usite2 -psite2) next step is to try to find out if apache can give you more detailed log info for the error it encountered. Upload a file called info.php with the following contents in your document root directory and access it from a web browser : http://www.mysite.com/info.php

  1. <?php
  2.  
  3. // Print all information.
  4. phpinfo();
  5.  
  6. ?>

What we need to check in this file is the display_errors and probably log_errors if you are on a Virtual Server (VPS) or even on a dedicated server. If these are ON and you do not see any errors besides Error establishing a database connection then we get to the last step

Error establishing a database connection

Last step is to test the real communication between apache, php and mysql. This is done with a test mysql connection script written in php. We have dedicated a whole tutorial about it and this script will reveal really of php and mysql get along. MySQL connection using PHP

VN:F [1.9.18_1163]
Rating: 4.0/10 (1 vote cast)
Error establishing a database connection, 4.0 out of 10 based on 1 rating

2 Responses to “Error establishing a database connection”

  • August Jiau says:

    Thank you for the write up! Also, I noticed that your RSS feeds aren’t working. Could you take a look at that?

  • Jean Hobell says:

    I am impressed, I must say. Really rarely do I see a blog that’s both informative and entertaining, and let me tell you, you’ve hit the nail on the head. Your blog is important; the issue is something that not many people are speaking intelligently about. I’m really happy that I stumbled across this in my search for something relating to this.

Leave a Reply