How To Fix Error Establishing A Database Connection In WordPress

Fix Error Establishing a Database Connection In WordPress: Getting error on our WordPress blog is quite frustrating. Many times and many types of errors we come across in our blogging career. Error establishing a database connection in WordPress is most common error that most users get.

Honestly, I have faced this error many times and by doing research every time I faced this issue, I found out its possible causes and solutions to fix them.

So in this article, I will be showing you how to fix error establishing a database connection in WordPress. You can also check out my article on how to fix to increase PHP memory limit to fix memory exhausted error in WordPress.

The Reason For Getting Database Connection Error

Getting this error means WordPress is unable to establish a database connection. There can be lots of reasons if you are getting this error varying from your unresponsive database and wrong or changed database login credentials to a corrupted database. Sometimes you can get “error establishing a database connection” error due to server error.

Fix Error Establishing Database Connection In WordPress

Now we shall look at various ways for fixing this error.  But before making any changes make sure to backup your database. Try one solution first and if it doesn’t work then you can move on to another solution. Now let us proceed with database fixing:

Fix Database Connection Error by Repairing Database

If you are getting the same error message that is “Error Establishing Database Connection” on wp-admin as well on your site . Then your database needs to be repaired. Add these following lines to your site wp-config.php file:

define("’WP_ALLOW-REPAIR’, true);

repair database connection using wp-config

After you have added these lines in your wp-config.php, you need to visit this page:

Note: Replace with your site name.

fix database connection error by database repair

You must remove above lines from wp-config.php after you have successfully repaired your corrupted database as to access this particular functionality user doesn’t need to be logged in.  So removing define("’WP_ALLOW-REPAIR’, true); from wp-config.php is must.

If your issue is still not resolved then proceed to next solution.

Fix Database Connection Error by Changing Database Details And DB_Host Value

If you have changed your database user password or root password, then you must make changes to these changes to your wp-Config.php

file also. Make sure everything is same.

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

If this issues still persists then try to replace localhost with IP in DB_Host.

define('DB_HOST', '');

Note: IP varies for different hosting services.

fix database connection error by MySQL

If the error is still not fixed move on to next solution.

Fix Database Connection Error by Checking MySQL Server

You can also get this error establishing a database connection when your site get loaded with a lot of traffic. This mostly happens if your site hosted on shared web hosting as their servers can not handle the load. So, in this case, you should check if your MySQL server is responsive or not.

If you have multiple WordPress site running on same host server you should check if you are getting the same error on all the sites. If you are getting this error on all the sites then there must be due to MySQL server.

But if you don't have any site on the same hosting account means if you have only one site.  Then to check MySQL responsiveness Go to cPanel > phpMyAdmin and connect the database. If you are able to connect then you need to check if your database has sufficient permissions. To check create file testconnection.php and paste the following code:

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

You should replace the password. and if it is connected without any issue then it means you have sufficient permissions and it means MySQL is fine. Now it means something else is wrong. Check your wp-config for typos.

If you are unable to connect to database then it means you don't have sufficient permissions or your MySQL server is down. Then you must follow the next solution. (Try this last solution and it will better assist you)

Contact Your Hosting Provider 

If you had tried all the fixes and still you are not able to fix database connection error, then you must try contacting your hosting provider.

You can phone or live chat with your hosting provider and ask for a possible issue with your site. They will probably tell you the main issue and also assist you in solving the error. This is what I recommended to the newbies.

Remember it is better to ask for help instead of complicating the issue. All top hosting companies like Hostgator, BluehostSiteground provide quality customer support.

Before trying any of the fixes remember to backup your wp-config.php file and database. I hope this guide will help you to solve database connection error and get your site live.

If you enjoyed the article please share it over the internet. For any questions and feedback let me know via comments. For updates please Subscribe.


Add a Comment

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