June 14, 2011

Error establishing a database connection

wordpress

Error establishing a database connection

WordPress is a sophisticated framework and a lot of help available online. Please have a look in your wp-config.php and also check the followings to make sure that configuration is correct.

1-
/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);

:: Normally this constant remain as it is as localhost, make sure that your phpmyadmin referring to the same host.

2-
/** MySQL database username */
define(‘DB_USER’, ‘yourmysqluser’);

:: It has prefix of your domain name sometimes and mostly it has first eight letters of your domain with underscore. You can check it from you cpanel.

By clicking on MySql Databases, you can have a look on existing databases and users.

3-

/** MySQL database password */
define(‘DB_PASSWORD’, ‘yourmysqlpassword’);

:: You can not obviously reset your password, just remove exisitng user and create a new one. And don’t forget to add that user to the required database, it means that you are giving that user specific or all privillages for DQL, DML etc.

4-
Once you have created or checked the username, password and database etc. Now access phpmyadmin from your cpanel.

And verify that do you putting DB_HOST & DB_NAME correctly?
If everything is correct but still wordpress is displaying the same error, go to your wp-config.php and check do the followings:

1- Try to connect to your database by simple php mysql_connect() function.

2- Try to echo your constants which are DB_HOST, DB_NAME etc, are they displaying the correct information?

3- Hopefully you will get your problem resolved, if not yet then try a different copy of wordpress to install again. Or finally you can have a look on your hosting as well, sometimes we get PHP running fine but hostings do have separate databases servers. Hosting support will guide you regarding this. Error establishing a database connection is obviously a simple but can make trouble when it prevents our website to be displayed just after uploading it.

Last updated: March 19, 2014