Troubleshooting Error Establishing Database Connection

Last updated on October 18th, 2014 by Robert Abela. Filed under WordPress Security Tutorials & Tips

WordPress database connection problems are common when installing, upgrading or even migrating a WordPress blog or website. There are several other scenarios were MySQL database connections can fail, and they can all be solved in the same easy way. In this article we will explain in step by step format how to easily troubleshoot and solve WordPress database connection problems.

How and where WordPress stores information

WordPress is a PHP web application that stores all the information in files and in a MySQL database. Images and other media related content are saved as files in /wp-content/uploads/ directory, while all other content and information, such as blog posts, links, usernames and passwords are stored in the MySQL database.

When installing WordPress you have to specify the database name, database username and password, and the hostname of the MySQL database server in the wp-config.php file, as per the example below.

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');
/** MySQL hostname */
define('DB_HOST', 'localhost');

When you encounter the “Error establishing database connection” error message, most of the times the issue is related to one or more of to the above mentioned entries, such as a wrong host, wrong username or password or wrong database name. It could also be a user rights issue, i.e. the configured user does not have access to read from or write to the WordPress MySQL database.

WP White Security.com security tip: When creating  a MySQL database for your WordPress, create a dedicated user with read and write permissions to the WordPress MySQL database only. Never use the MySQL root account for such operations.

Troubleshooting WordPress Database Connection

If the error ‘Error establishing database connection’ is showing up instead of your WordPress blog or website, first check that the host, i.e. the MySQL Server address is correct. Once you confirm that the host is correct, confirm that the MySQL username and password specified in the wp-config.php file are correct and that the user has access to read from and write to the WordPress MySQL database. Below are the two test procedures explained in detail.

Testing tool for WordPress MySQL database

  1. Download the WordPress MySQL database connection test tool from here.
  2. Replace the entries mentioned below from the code with the same entries you’ve entered in the WordPress configuration file wp-config.php:
  • #HOST: IP address or hostname of the MySQL database server
  • #DB_USER: the username used to access the WodPress MySQL database
  • #DB_PASSWORD: the password of the  MySQL user
  • #DATABASE: the name of he database

Once you change the details, save the changes and upload the file to your WordPress using an FTP client. Navigate to the file URL uploaded on your website to start the test. E.g. if you uploaded the file dbtest.php to the root of your website, the URL is http://www.yourwebsite.com/dbtest.php.

Once you run the test, if everything is configured correctly you should see the below success test messages:

  1. Successfully connected to MySQL database server.
  2. WordPress MySQL database selected.

This means that the host, the credentials to access the WordPress MySQL database and permissions are correct. If you get any other error message, refer to the below list of error messages for more information on how to solve the issue.

MySQL Host and database credentials error messages

The results in the first line are related to the MySQL Host and the database credentials. Below are the possible errors:

 [2002] A connection attempt failed because the connected party did not properly respond after a period of time

This means that the Host you specified is not correct, or the MySQL database server is down and unreachable. If you are not sure about which host you should use for the MySQL server, contact your hosting provider.

No such host is known

Same as above; MySQL Server is unreachable.

Access denied for user ‘db_user’@’localhost’ (using password: YES)

This means that the username or password you are using are incorrect. To solve this issue, login to your hosting provider control panel and make sure you copy the correct username. If you are not sure what the password is, from the hosting provider control panel or from the PhpMyAdmin web interface, change the password for the database user.

Note: Before troubleshooting WordPress MySQL database user rights, first you must be able to successfully connect to the MySQL Server. So before troubleshooting any errors in the second line, the first line should be  should always be ‘1. Successfully connected to MySQL database server’.

WordPress MySQL database access

The result ofthe second line of the test is related to the MySQL database user access, i.e. it checks if the user can at least read from the WordPress MySQL database. If the second line is ‘2. WordPress MySQL database selected’, it means that you are using the correct database and the user has access to read from it. If you get any other error, refer to the below list for more information:

Unknown database ‘db_name’

This means that the database name you specified does not exist on the MySQL server. In this case the database name is wrong. Refer to the hosting provider cpanel to get the correct database name.

Access denied for user ‘localusr’@’localhost’ to database ‘localdotcom’

The above error means that the database exists but the user you specified in the WordPress configuration file wp-config.php does not have access to read from the MySQL database. In this case you have to give permissions to the user to be able to access the WordPress MySQL database. You can do so from the hosting provider control panel or from the pypMyAdmin web interface.

WordPress Hosting, Firewall and Backup

This Website is:

30 comments

agus 28/12/2012

T.Q. for information and tips…
My WEB was going to normal again….

Once again..
Thank’s very much…..

wilson 23/01/2013

this is what i got

Host ‘rrcs-98-102-204-203.central.biz.rr.com’ is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts’

WP White Security Administrator 23/01/2013

Hi Wilson,

Do you have your own server where MySQL is installed or are you using shared hosting? If you are using shared hosting, then contact your hosting provider. If you have your own server, connect to the server using SSH, login to MySQL command line tool and run the command mysqladmin flush-hosts to flush any open connections.

Hope the above helps. If you are unable to solve it, drop us an email on help@wpwhitesecurity.com and we will assist you.

kelly 28/01/2013

Thank you this really helped, seems I have my database on another server from when i swapped it, so now all i need to do is export it to my own myphpadmin server

Robert Abela 28/01/2013

Hi Kelly,

Good luck with the move. Just drop me a mail on robert@wpwhitesecurity.com if you need any assistance or get stuck.

karthik 07/02/2013

How to install wordpress by using SQL Server.. i need Guide to install?

Robert Abela 08/02/2013

Hi Karthik,

Do you mean installing WordPress on Microsoft SQL Server? If yes, you can find more reference below:

http://wordpress.visitmix.com/development/installing-wordpress-on-sql-server
http://www.microsoft.com/web/wordpress

If you encounter any problems drop me a mail on robert@wpwhitesecurity.com. We have extensive experience on Windows servers as well.

Engr Kabir 16/04/2013

U are Great!
Thanx for your help and support.

Floyd 18/12/2013

I’m a graphic artist & photographer – NOT a programmer.

I’ve created a WP site and uploaded the files to the server but keep getting the ERROR Establishing a Database Connection.

I’ve updated all config files to no avail.

I installed the Test php above and get this error:

Not Found

The requested URL /dbtest.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I obviously have no clue what’s going on..

Robert Abela 21/12/2013

Hi Floyd,

Sorry for the delayed response.

Most probably you are not specifying the correct database, username, password or server IP in wp-config.php file. That is the most common problem especially while setting up WordPress the first time. Confirm the database connection details with the hosting provider if need be.

As regards the test file dbtest.php it seems you are uploading it to a different location from where the website is.

Hope the above helps. Should you have further queries or need assistance just drop us a mail on support@wpwhitesecurity.com.

Good luck 🙂

Check out the

Roman Inflianskas 29/03/2014

Hi.

I tested my credentials with your dbtest.php. It shows that all is OK but I still getting the same error. I enabled debug https://codex.wordpress.org/Debugging_in_WordPress but saw nothing new in web browser, and logs wasn’t created.
What should I do?

Robert Abela 30/03/2014

Hi Roman,

Thank you for following our blog. Unfortunately it is very difficult to determine the problem remotely. If you’d like us to help you with solving your problem contact us on support@wpwhitesecurity.com.

Looking forward to hearing from you.

Alex 30/04/2014

Thanks but I got the error “Warning: mysql_connect() [function.mysql-connect]: Can’t connect to MySQL server on ‘localhost’ (10061) in D:\Programme_WEB\xampp\htdocs\invensys\scripts\wp\Invensys\dbtest.php on line 5
Can’t connect to MySQL server on ‘localhost’ (10061)”

Please assist.

Robert Abela 02/05/2014

It seems that the MySQL server is not listening, i.e. it is switch off. The credentials are not a problem since else you would get a credentials related error. Can you check if the MySQL server is listening on localhost?

Manggale Web 16/05/2014

My website is also facing the same problem “Error establishing database connection”
But that makes me confused, the problem occurs only a few minutes, and then restored, after some time and it’s down again multiple times

Robert Abela 17/05/2014

In your case it seems to be a problem with the MySQL server uptime, i.e. in some cases it seems it is not available. It could be a network problem or load problem. I would recommend you to check with your hosting provider.

Shafar 10/08/2014

Creating a new db user with full rights worked for me! Thanks!

tom 25/10/2014

Thanks but I got the error:

Warning: mysql_connect(): Unknown MySQL server host ‘#LOCALHOST’ (74)

Robert Abela 25/10/2014

Hi Tom,

Such error means that or the MYSQL server is not responding or it is listening on something else rather than Localhost. You might need to check that with your hosting provider.

tom 27/10/2014

thanks Robert 🙂

Thapainfo 25/10/2014

Thank you for sharing great guideline. it is really helpful for me. My site has also faces same problem and able to solve through your guideline. i appreciate for you.

Alex 29/10/2014

H Robert,

Thank you for the DB testing tool. I did as you said and my site displayed:
1. Successfully connected to MySQL database server.
2. WordPress database selected.

However, all other pages show the “Error establishing a database connection” message.

How to fix it?

Many thanks,
Alex

Robert Abela 30/10/2014

Hi Alex,

Thank you for following our blog and welcome for your comments.

If the tool works the WordPress installation should work. To start off with can you confirm that the connection details you’ve configured in the tool are the same as those in the wp-config.php file?

Alex 30/10/2014

Robert, thank you very much for keeping this post alive and answering our questions.

Yes, the DB credentials are identical in the wp-config.php and the test tool. I also uploaded the brand-new wp-config.php with correct DB details; no avail.

It is not a new installation. The site was perfectly fine for years, when suddenly at no actions from my end it gave the “Error establishing a database connection”.

I’m lost for causes. Do you have any advise?

Many thanks,
Alex

Robert Abela 04/11/2014

Hi Alex,

Technically speaking it should work and offhand I do not have any recommendations. If you want drop me an email at robert@wpwhitesecurity.com so we can look further into this, i.e. give me remote access etc.

Looking forward to hearing from you.

Dinesh Kumar 27/02/2015

Recently, I started a new website and install wordpress on it. But, now it is showing “Error establishing Database connection ” and it appears and after 1-2 minutes site works normal again. It occurred 3-4 times, I don’t know it happened by mistake or there is any need to do something.

Robert Abela 09/03/2015

Hi Dinesh,

If the problem is intermittent, then the database details in the wp-config file are correct. The issue seems that your MySQL server is not available all the time, hence I would recommend you to speak to the hosting provider.

Sometimes bigger memory_limit in php.ini or users.ini helps too in this case

Bastien 25/06/2016

This can also be caused by a DOS attack !

Robert Abela 26/06/2016

If the database server (MySQL) stops responding before the web server, yes it can. Well said.

Leave a Reply

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