Fixed: Error Establishing a Database Connection in WordPress

Table of Contents

Are you seeing this error on your WordPress site:

Error Establishing a Database Connection

It’s a disconcerting error that signifies your site doesn’t connect with your database — causing your site to crash.

Below are the common causes why your site stopped connecting with your database (which are also the ones we’re going to solve):

  1. Incorrect database login credentials
  2. Corrupted database
  3. Corrupted WordPress core file
  4. Web server problems

When you encounter this, you must act on it right away as visitors won’t be able to access your site at all.

Go through each method below since there’s really no way to pinpoint the exact cause until we fix it:

Method #1: Fix the WordPress Database Login Credentials (cPanel)

This is the most common cause of the database connection error in WordPress and usually happens after you migrate to another web host.

Now, it would be easy if you can simply check your database login credentials. Unfortunately, with recent updates from phpMyAdmin, this is impossible to do anymore.

Note: If your web hosting provider allows you to check your database login credentials in your host’s dashboard, there’s no need to hack the credentials anymore and change them. Check whether or not they’re the same as the ones in the wp-config.php file.

The first thing to do here is to access the wp-config.php file using an FTP tool like FileZilla or your cPanel’s file directory.

Once you have access to the file, check for the following code:

/ ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name' );

/** MySQL database username */
define( 'DB_USER', 'database_username' );

/** MySQL database password */
define( 'DB_PASSWORD', 'database_password' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

Now, if you have access to cPanel, click on “MySQL® Databases” in the databases app category.

Click on the MySQL databases app to see all the users

Scroll down to the bottom of the page until you see who the current users are.

Find the one you have access to (check the name in the “Users” column) and click on the “Change Password” button.

Click on the change password button to do so on that user

On the next page, simply enter a new password and save that password.

Now, go back to the wp-config.php file and change the password there as well. After that, reupload the file back to your server and overwrite the existing file.

Visit your website again and check if the error is now gone. If it’s not, then the problem is something else. The fact that you can access the database with the correct credentials is a sign that this isn’t it.

Method #2: Repair the Corrupted Database

Although it’s not that common for a database to get corrupted, it’s still possible especially when you have so many tables caused by constantly installing and uninstalling new plugins and themes.

If you see the following error in the WordPress dashboard:

One or more database tables are unavailable. The database may need to be repaired.

…then it’s likely that your database has been corrupted.

Fortunately, you can solve this easily by editing the wp-config.php file. Again, you can use an FTP tool like FileZilla to download-edit-reupload the file or use the cPanel’s file directory.

Note: You can also repair the database through the MySQL® Databases app in your cPanel. You should see a “Repair Database” button under the “Modify Databases” section on the page.

Add the following code snippet at the bottom of the wp-config.php file:

define('WP_ALLOW_REPAIR', true);

After reuploading the file back to the server, the next step is to navigate to the following page:

http://www.yourwebsite.com/wp-admin/maint/repair.php

Don’t forget to change the “yourwebsite” with your own. You should then see the following page:

Click on the repair database to start the process

Although there are two choices here, the “Repair Database” command is quicker. After that, try and visit your site again to see if this fix did it.

Note: Whether or not the issue has been fixed, remove the code snippet from the wp-config.php as soon as possible. Users don’t need to log in to see and access the “Repair Database” page.

Method #3: Manually Update the WordPress Installation

If you have corrupted files (could be due to hacking or from transferring files through FTP), it could also be causing your website to lose connection with your database.

Basically, what you need to do here is replace the WordPress core through fresh installation.

Download the updated version of WordPress from this site first:

https://wordpress.org/download/

Extract the file on your computer. Then, delete the following files:

  • wp-config-sample.php
  • wp-config.php
  • wp-content folder

After that, reupload the remaining files and folders to your server by using FTP.

Note: At this point, you may want to create a backup for your files in case something “not good” happens. Use whatever backup method you have at your disposal.

While uploading the files, you will be prompted and asked to overwrite the existing files and folders. Since we’re replacing the old corrupted files, confirm that you want to “Overwrite” the files.

Overwrite existing wp-activate.php

After doing this, make sure to clear your browser cache. Check our website again and see whether or not the issue has been fixed.

At this point, if the issue is still present, then the likely problem lies with your database server. Your only choice here is to contact your web host about the matter.

Anyway, if the methods earlier worked, do you mind sharing which one did the trick? Let us know too if you found another method that solved the problem so we can add it and give credits to you.

Is it time for a better WordPress host?

Related Posts

Leave a Comment

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

Receive the latest news

Subscribe To Our Newsletter