Easily Fix the Error Establishing Database Connection in WordPress

If you are a WordPress developer or you regularly visit various websites, you must have encounter “Error Establishing Database Connection” issue. There are many reason for this error. And as a WordPress developer, this is an awfully frustrating. This can happen on its own even you are not changing anything. I have encounter this several times on my own website and also on other WordPress websites that I manage.

Error Establishing Database Connection Image

It takes very little time to detect and fix the error, if you can figure out the exact causes. In this article, I have tried to sum up few common solutions to detect and fix the error establishing database connection.

You may also like to read my other posts
Complete Step Wise Guide on How to Track Gmail Email.
Ultimate step by step guide to start WordPress blog.
Reset WordPress to default settings easily and quickly

What Causes Error Establishing Database Connection?

Perhaps, the answer is in question itself. This error occurs because WordPress is unable to establish a database connection. In a simple sense, WordPress is unable to connect with the database. But the exact reason why WordPress is unable to connect to a database may vary. Maybe your database login ID and password are wrong or changed. And This is very rare as any WordPress developer knows about it and won’t let this happen easily and if it does will fix it in sooner time.

Most of the time, I find the main reason is unresponsive database server. Database might be corrupted or there might be some sort of server error. But chances are always there having other factors as well.

You can find lot more tutorial on how to fix error establishing a database connection but I am trying to simplify the solution.

Troubleshooting Error Establishing Database Connection.

Most common warning and request: Before making any changes, please take a database backup or check that you have sufficient backup to recover if anything goes wrong.

Then let’s move to the troubleshooting process

Check if the problem occur for (wp-admin) also? Try to access your site as a logged in user. If you are not getting the same error on both end and have the different error for (wp-admin) you need to repair the database. The error for (wp-admin) seems like “One or more database tables are missing” / “One or more database tables are corrupted” etc.

To repair the database, you need to alter the wp-config.php file. Open the wp-config.php file to edit either using FTP or cPanel and add the following code just before last line ‘That’s all, stop editing! Happy blogging’

define(‘WP_ALLOW_REPAIR’, true);

Once added the code, you can visit the following URL


Repair Database Image

Click on Repair and Optimize Database.

This will try to repair and optimize the database, if that is repairable.

Always remember that the user does not need to be logged in in order to repair the database functionality when you alter the wp-config.php file as stated above. Maybe the users not able to log in as usual because of the corrupt database.

Also, do not forget to remove define(‘WP_ALLOW_REPAIR’, true); line once you finish repairing and optimizing the database. This is crucial because hackers may try to inject some malicious script using the database repair functionality. So this feature is disable in the beginning.

If the error continues even after repairing the database or sometime you may not be able to run the repair database functionality. Then you should check your wp-config.php file briefly.

Check wp-config.php in Brief

Probably, the wp-config.php is the most important file for the WordPress installation. And, also for the smooth running of the WordPress website. The detail information of your WordPress installation resides here in this file. Including the database connections. Host server connection etc. If anything is misconfigured or configured incorrectly you will get the error establishing database connection error. Sometime you may change the password for root, or for the database user password, then you also need to change the same in wp-config.php file. So, its better you check these settings and be sure it’s ok and correctly configured.

The wp-config.php file looks like as shown in below.

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

Where ‘database-name’ is your WordPress database, ‘database-username’ is the username for the database. ‘database-password’ is for the password to connect to the database. If you do not know these details, you may contact your hosting provider. And the DB_Host value is localhost in most of the hosting providers such as Godaddy, Bluehost, HostGator, and other popular hosting providers. But in some hosting providers it may not be the ‘localhost’

I have seen some people fixing the error by simply changing ‘localhost’ value to some other ip address.
define(‘DB_HOST’, ‘’);
IP address value is different depending on the hosting providers.

And, also remember that running on the local environment like MAMP XAMP or WAMP and running on the web server is always a different approach. While running on the local environment you may not consider the security issues. but while running on the web server you must consider it.

Now, you find everything ok in you wp-config.php file including the typos. I am sure the error is on the server side.

Error on the Hosting Server Side

The next process is to check your hosting server. So let’s check the MySQL server on your web hosting.

Most often the server-side error occurs because of the huge traffic that your site is getting. This happens more if you are on the shared hosting. When you get a lot of traffic, your shared hosting server is unable to allocate server resources properly as it is scaled for minimum while sharing with others. This often cause slow site performance and sometime error establishing database connection. And the best solution for such a problem is to increase you server allocation. You always need to seek help from your web hosting service provider for this kind of error.
Furthermore, you can also check the MySQL server yourself with the help of below tricks.

First step is to test another sites on that same server and check if the same issue occurs.If it does only your web hosting service provider can help you to fix this issue.

If you have only one site on this server then you can try to access your MySQL database using phpMyAdmin in the cPanel. If you are able to connect using phpMyAdmin, then go and verify that your database user has sufficient permission to access the database. You can check that using the phpMyAdmin or simply can create a new file. Let’s say connectiontest.php.

Check Database Permission Using connectiontest.php

Create connectiontest.php file and write the below code in it.

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

Where the ‘localhost’ is the server, ‘root’ is the username and ‘password’ is the password for user ‘root’ to access the database. Always make sure you enter the correct username and password.
Somewhere the ‘password’ is just blank so in place of ‘password’ you simply type ‘’ . This is for blank password. Then it might work. But leaving password blank is not a good choice. In fact, username ‘root’ and ‘password’ blank is pretty common so always a wise decision to keep it different.

If you are able to connect using the connectiontest.php that means, your user has sufficient permission. There might be something other wrong check your wp-config.php file once again and make sure username password is the same as in the connectiontest.php file.

Insufficient permission to access the database error seems like below.

Access denied for user 123 (using password: YES)

In such case you can simply access your database using phpMyAdmin. Sift back to root user and reset the permission for other users right from there. Or you can ask your hosting service provider to fix this issue. Even when you are unable to connect through connectiontest.php file you need to ask your hosting provider to fix the user permission issue.

Some other Common Fix to Error Establishing Database Connection

Sometime the installed plugins can cause the problem. In that case simply deactivate the plugins and the error might go away. You can always re activate the plugins later on.

Sometime the change of database table prefix can also cause the issue. Though this issue is likely to be fixed with database repair process.

Sometime all above process fail to solve the problem. Neither hosting service provider can solve your problem. You then have to freshly install new WordPress and import all your data to newly installed WordPress.

However if you are Godaddy user, you can also seek help from them simply mentioning error establishing a database connection Godaddy . Just give them a call and they will fix it.

Finally, This is really a frustrating error for each and everyone. I look forward to know the solutions that you tried and got the fix. And will further include that solution. Please do comment below or you can also reach me via my contact page. You can also mention @lvlysumn.

2 thoughts on “Easily Fix the Error Establishing Database Connection in WordPress”

  1. Joarden Hasctin

    Hello suman, Nice read this one. I have also encountered this problem couple of time. And some other fixes I got is the site URL. Sometime site URl can also create such error. Try to fix that by changing it in the wp-option database table. That will help.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.