How to Fix “Error Establishing a Database Connection” in WordPress

This is one of the worst fears of an amateur webmaster. You try to access your WordPress website but it gives an error saying “Error Establishing a Database Connection”. Personally, I have encountered this error few times myself. If you have encountered this error, you must take immediate action to fix it. You will lose all your traffic during this period. Lost traffic equals to lost revenue. If you looking how to fix “Error Establishing a Database Connection” in WordPress, follow one of the methods mentioned below.

How to Fix “Error Establishing a Database Connection” in WordPress

Before we talk about solutions, have you ever wondered what caused your WordPress website to display “Error Establishing a Database Connection” error? WordPress software itself written using PHP and MySQL. So when you load a post or a page in WordPress, PHP code access MySQL database to display the information. When this process gets interrupted, your WordPress website display the error message “Error Establishing a Database Connection”.

There are many causes for this error.

1. Your website database is corrupted.

2. Your website database login credentials missing/wrong.

3. Your website server not responding.

Now let’s talk about how to fix “Error Establishing a Database Connection” in WordPress. First step to determine what cause this error is to check few pages and posts of the website. If your website posts/pages loading normally and you only get the error trying to access WordPress admin dashboard, this could be a sign of corrupted WordPress Database. If you can’t access your website at all, this is due to failed connection to your WordPress Database.


Solution 1 – Repair WordPress Database.

WordPress has its own built in way to repair the database. But you must enable this option first to use its perks. Log in to your Website cPanel and load the “File Manager“. Now find the WordPress folder and open the “wp-config” file through editor. Enter the following code to the bottom of the file and save.

define( 'WP_ALLOW_REPAIR', true );

This will enable WordPress to try repair it’s database itself. Simply go to (replace with your website URL).

How to Fix Error Establishing a Database Connection

Now select either of the two options present. Note that if you choose the “Repair and Optimize Database” option, it could take longer. Please do not close the browser or hit the back button till the process complete. Then check your website to see if its functioning properly and exit.

Now navigate back to cPanel File Manager and remove the code you entered earlier in “wp-config” file and hit save. If you forget to do this, it will leave your website vulnerable to anybody to try to access the WordPress Database Repair options.

If you still experiencing the error “Error Establishing a Database Connection” in WordPress, follow the solution 2.


Solution 2 – Check your WordPress Database Settings.

If WordPress can’t access the MySQL database, it can’t display the information which could ultimately lead to the “Error Establishing a Database Connection” error. Did you recently changed hosting services? or installed any shady WordPress Plugins? Either way, lets check if your Database log in settings are correct.

Log in to your website cPanel. Open up the “File Manager” and find the “wp-config” file. Open it through the Editor and check the following lines.

// ** MySQL settings - You can get this info from your web host ** //
/** 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' );

These information must be accurate for your website to function properly. Now let’s go through each one and check the accuracy.

DB-NAME‘ is your website MySQL Database name and ‘DB-USER‘ is the Authorized User to that database . Go to your website cPanel and click on “MySQL Databases” under “Databases“. There you can see the “Database” name and “Privileged Users“. Check these two fields with the “wp-config” file fields for the accuracy.

How to Fix Error Establishing a Database Connection

DB-PASSWORD‘ should be the MySQL Database password you chose when you were creating the database. If you can’t remember the password, you can simply create a new user with a new password and add it to the database. In most servers, ‘DB-Host‘ is ‘Localhost‘.

Check all the fields mentioned above for accuracy. If any of those fields are incorrect, your WordPress website will not function properly and may give the error message “Error Establishing a Database Connection“. If you have tried both of the solutions above and still can’t get rid of the error, it’s most likely caused by your website server. Contact your website hosting company for further assistance.

Are you a WordPress guru and know any other way to fix this error? Put it down in comments, We would love to hear them and inform our audience in the future.

Also read: Updated WordPress Ping List


Related Articles

3 Responses

  1. I tries both ways. It said database was damaged. So I ended up restoring a back up from last week. 🙁

Leave a Reply

Your email address will not be published.