WordPress database connection problems are very common, especially when installing, upgrading and migrating a site. However, they can also occur on other occasions. If you have a WordPress site you’ve surely seen the error establishing database connection:
WordPress database connectivity problems can be solved very easily. This article explains how to use our database connection tool to troubleshoot and solve WordPress <> My SQL database connectivity problems.
Common Causes of WordPress Database Connection Problems
In short, the Error establishing a database connection problem occurs when WordPress cannot connect to the database on the MySQL database server. This is caused by any of the below:
- The database configuration details in WordPress are incorrect
- The MySQL database server is unavailable (it is down, very busy or unreachable)
- Incorrectly configured WordPress database privileges on the database server
Troubleshooting the WordPress Database Connection Error
If the error ‘Error establishing database connection’ is showing up instead of your WordPress blog or website, first check that the database configuration in WordPress is correct.
Where are the Database Connection Details Configured in WordPress?
WordPress is a PHP web application that stores data in a MySQL database. It uses the database connection details configured in the file wp-config.php to connect to the database, which can be found in the site’s root directory. Below is an abstract from the file where the database connection details are configured:
/** 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');
Most of the times database connection issues happen because one of the above entries are not configured correctly. So check that all of the above are correct:
- database server’s name or IP address
- database name
- username and password of database user.
WP White Security.com security tip: Create a dedicated user with read and write permissions for the WordPress MySQL database only. Never use the MySQL root account for such operations.
If the database connection details in the wp-config.php file are correct but the connection still fails, then you either have incorrect details, or there are problems with the database server or privileges. In such case use our simple tool to test the database connection and find out exactly what the issue is.
Testing tool for WordPress Database Connectivity Tests
Follow the below procedure to test the WordPress database connection and find out the cause of the problem:
- Download the WordPress MySQL database connection test tool.
- Replace the entries mentioned below from the code with the same entries you’ve entered in the wp-config.php WordPress configuration file:
- #HOST: IP address or hostname of the MySQL database server
- #DATABASE: the name of he database
- #DB_USER: the username used to access the WodPress MySQL database
- #DB_PASSWORD: the password of the MySQL user
Once you update the database connection details save the changes and upload the file to your WordPress. Navigate to the file URL uploaded on your website to start the test. E.g. if you uploaded the file wpdbtest.php to the root directory of your website, the URL is http://www.yourwebsite.com/wpdbtest.php. If the configuration is correct, you should see the below success message:
It there are database connectivity problem the tool will display an error message and error code highlighting the problem, as shown in the below screenshot. Refer to the below list of error codes and explanation to find out what is wrong with the connection and solve the issue.
MySQL & WordPress Database Connection Errors
The below is a list of the most common MySQL server database connection error codes and descriptions.
|Error Code||Cause & Resolution|
|1040||Too many connections. Try to reduce the number of processes / applications using the database server or distribute the connections to other servers.|
|1041||Out of memory. You can restart the server to fix this issue though it might happen again. If so, increase the memory allocated to your database server.|
|1043||Bad handshake. This means the database connector you are using is using a different protocal than the one the server uses, so they cannot communicate. This is a server configuration issue. Speak to your web host.|
|1044||Access denied for user to database. This means that the database user credentials are correct. However the user does not have acess to the database.|
|1045||Access denied for user using password. This means that the credentials are incorrect so you cannot connect to the MySQL server. Check that the username and password are correct.|
|1049||Unknown database. The database you specified in the configuration file does not exist on the server.|
|2002||Connection timed out. This means that the database server is unreachable. So it is either down or you are using the wrong IP address. Check with your web host to confirm the IP address.|
Summing It Up
It is very easy to fix the Error establishing database connection issue in WordPress, especially with our database connectivity test tool. It pin points the problem so you do not have to troubleshoot and do any guesswork. Should the issue be database privileges or server configuration contact your web host’s support if you are not familiar with MySQL.
T.Q. for information and tips…
My WEB was going to normal again….
Thank’s very much…..
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’
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 email@example.com and we will assist you.
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
Good luck with the move. Just drop me a mail on firstname.lastname@example.org if you need any assistance or get stuck.
How to install wordpress by using SQL Server.. i need Guide to install?
Do you mean installing WordPress on Microsoft SQL Server? If yes, you can find more reference below:
If you encounter any problems drop me a mail on email@example.com. We have extensive experience on Windows servers as well.
U are Great!
Thanx for your help and support.
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:
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..
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 firstname.lastname@example.org.
Good luck 🙂
Check out the
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?
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 email@example.com.
Looking forward to hearing from you.
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)”
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?
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
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.
Creating a new db user with full rights worked for me! Thanks!
Thanks but I got the error:
Warning: mysql_connect(): Unknown MySQL server host ‘#LOCALHOST’ (74)
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.
thanks Robert 🙂
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.
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?
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?
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?
Technically speaking it should work and offhand I do not have any recommendations. If you want drop me an email at firstname.lastname@example.org so we can look further into this, i.e. give me remote access etc.
Looking forward to hearing from you.
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.
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
This can also be caused by a DOS attack !
If the database server (MySQL) stops responding before the web server, yes it can. Well said.