How to solve Error establishing a database connection in WordPress

Error establishing a database connection is probably the most common MYSQL database error, which bloggers see on their WordPress websites. In the past, I’ve also faced this problem so many times on my WordPress websites. If you are new to WordPress , then this can be really frustrating as there are manycauses  for this problem. Usually you will see this error on your website when you install, upgrade, downgrade or migrate WordPress from one server to another server.

In this post, I’m going to explain to you reasons for “Error establishing a database connection” and how you can easily resolve this small, but frustrating WordPress database error in less than two-three minutes.

how to solve error establishing a database connection in wordpress,wp error establishing a database connection,error establishing a database connection wordpress fix
How to solve error establishing a database connection in wordpress

Main Causes Of  “Error Establishing a Database Connection in WordPress” are as follows:

  •  Wrong MYSQL database login details (Most common cause)
  •  WordPress database is corrupted
  •  MYSQL Server is down
  •  MYSQL Database User Has Limited Privileges

Wrong MYSQL database login details

Most of the time you will see “Error Establishing a Database Connection” due to “wrong MySQL database login details”. You will have to open your wp-config.php file(located on your root folder) through FTP and file manager and check whether database details i.e. (Database name, database user, database password and database host )are correct or not. There should not be any typing mistake. You can find your database details from your web hosting cpanel. Reset password in case you don’t know your database password. It should look like this.

define('DB_NAME', 'Enter your database name here');
define('DB_USER', 'Enter your database username here');
define('DB_PASSWORD', 'Enter your database password here');
define('DB_HOST', 'Enter either localhost or 127.0.0.1 here');
Note: In Database Host field enter either “localhost” or 127.0.0.1.

How To Repair Corrupted Database In WordPress

If you are getting ‘Error Establishing a Database Connection” only in back end i.e. on WordPress dashboard(wp-admin) then your WordPress database is corrupted.  For repairing database you need to add the function given below on your wp-config.php file.

define('WP_ALLOW_REPAIR, true);

 After you have added above code you need to visit the link given below in snapshot for completing repairing process. Click “Repair” or “Repair and Optimize” and in a few minutes your database will be repaired.
Repair Corrupted WordPress Database,WordPress Error Establishing a Database Connection

Note:  Please remove the above function from wp-config.php file when your database is repaired. 

MYSQL Server is Unresponsive

If your database is not corrupted nor you have entered wrong login details in wp-config.php file then you  seriously need to contact your web hosting provider whether your MYSQLserver is responsive or not? Most of the time due to high traffic, mysql server becomes unresponsive and as a result you see this error on your website or your website will become slow.

You may also like to read: How to add Google Analytics In WordPress In Less Than 5 minutes


MYSQL Database User Has Limited Privileges

Sometimes MySQL database user privileges can become corrupted or limited. So in order to fix  this you will need to delete the current user that is connected to your database. After deleting current user, create new user with full privileges to your WordPress MYSQL Database. Now open your wp-config.php file and replace old database username with new username. That’s it.

Note: Try this method only if none of the methods given above work.

Do let me know via comments if you faced “Error establishing a database connection error” and if the solutions provided in this post solved your problem. If you know any other method by which we can fix this issue then do share it with us. 

  • SANTOSH KUMAR says:

    Nice pal.Thanks for sharing.Is there any trick to make mysql server responsive?

    • Deepak Singh says:

      Hello Santosh,
      For making MYSQL responsive,you should optimize your database two-three times in a month. You can use Free WP- Optimize plugin for optimizing and repairing databases.

      In addition I would highly recommend you to enable MySQL slow query log for confirming that the problem in fact lies with MySQL.

  • Indeed the “Error establishing a database connection ” is one of the many frustrations affecting newbie WordPress users. However, its not all bad since there are ways to deal with it. Luckily, with the tips shared here, a blogger can bid farewell to these errors.

    I have applied the repair tips shared in correcting corrupted database error to solve error problems in the past. Yes, these tips work, I recommend them for newbies!

    This post was found in kingged where it was shared and upvoted.

  • Prince Ramgarhia says:

    I already face this problem one time then I have to call my friend to fix my problem but I have found this solution now for next time Thanx a lot

  • This article solve my database error. Thanks..

  • Well, quite good explanation dude. However, there may be another cause for the error. As, if a person forgets to renew his hosting, then also he can suffer this situation. Though, it’s quite rare but it may also be the reason.
    Thanks for sharing such a nice information.
    -Nitin

  • This is one of the major problem that use to occur on wordpress blog, i always face this on one of my client blog due to hosting issue and i am glad that this article help in getting it solved.

    Thanks a lot and my first time here, am going to keep on re visiting and subscribe to your mail list for more.

  • >

    Pin It on Pinterest

    Share This