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

Is your website experiencing the “Error Establishing a Database Connection” error? Don’t panic just yet — it is actually pretty common. Read on to find out what this error means, what are the possible causes, and how to solve it.

What Does Error Establishing a Database Connection Mean?

Simply put, this error message indicates that your site failed to retrieve the information needed to render the page.

WordPress uses PHP as the main language and MySQL as the database. When someone visits your site, certain PHP commands will fetch your site’s information from the database. If it fails to do so, your site cannot load, and the error message will appear.

As a result, not only does your site becomes inaccessible and loses traffic, but it can also create a negative reputation.

What Causes This Error?

There are three possible reasons that can cause the “Error Establishing a Database Connection” error message, namely:

  • Incorrect database credentials — if there is any change to your database’s username and password, the old login credentials stored in your WordPress files will be invalid.
  • Corrupted database — can be a result of a hacking attempt, plugin failure, or an incompatible theme.
  • Traffic spike — a sudden surge in demand can make your database unresponsive.

Before making any attempts to fix the error, check whether or not the error message appears on your admin dashboard as well.

Here’s how your WordPress admin area will look like:

error establishing a database connection that appears on the admin dashboard

If that’s the case, the issue lies with invalid login credentials. However, if a different error message appears, then the cause might be a corrupted database or a traffic spike.

Once you’ve identified the possible cause, follow our tutorial below.

How to Fix “Error Establishing a Database Connection”?

Here are the three most common solutions you can try to fix this error:

Method 1: Fix the Login Credentials

If both the frontend and backend of your site show the same error warning, this method might solve the problem.

First things first, you need to check if the database login information matches what your site stores in the wp-config.php file.

To access the file, use an FTP client like FileZilla or the File Manager in your hosting control panel.

If you use the File Manager, navigate to the /public_html/ folder, right-click on the wp-config.php file, and select Edit.

Then, find and take note of DB_NAME (database name), DB_USER (username), DB_PASSWORD (password), and DB_HOST (the host of the database).

Finding the DB_NAME, DB_PASSWORD, and DB_HOST lines within the wp-config.php file on the hPanel's file manager

After that, navigate to MySQL Databases and look for your current database under the List of Current MySQL Databases and Users section. There you’ll find the database name, username, and host.

List of Current MySQL Databases and Users

If the login data in your wp-config.php file is different from the MySQL Databases settings, match them by modifying the wp-config.php file.

Edit these lines:

  1. define(‘DB_NAME’, ‘fill_this_with_your_current_database_name’);
  2. define(‘DB_USER’, ‘fill_this_with_your_current_username’);
  3. define(‘DB_HOST’, ‘fill_this_with_your_current_localhost’);

Once you’ve edited the file, refresh your site to see whether or not the problem is successfully resolved.

If the error still persists, it’s possible that the value of DB_PASSWORD is still false. To fix this issue, you need to reset your MySQL password.

Simply head over to the MySQL Databases menu, click on your current database, and select Change Password. After entering the new password, hit the Update button.

Method 2: Repair the Database

Your database might be corrupted if the admin dashboard shows an error message along the lines of “One or more database tables are unavailable. The database may need to be repaired.”

Thankfully, you can easily repair the database by inserting the following function into the wp-config.php file:

  1. define(‘WP_ALLOW_REPAIR’, true);

Make sure to put the function right before the section “That’s all, stop editing! Happy blogging.”

inserting the following function into the wp-config.php file

Once you’ve saved the modification, go to http://www.yoursite.com/wp-admin/maint/repair.php.

Next, select the Repair Database or the Repair and Optimise Database option. Choose one that fits you best.

After the database is repaired, don’t forget to head back to your wp-config.php file and remove the function immediately. This measure is important to prevent any website abuse as anyone can access the repair page URL without being logged in.

Method 3: Fix the Corrupted Files

Another possible reason for the “Error Establishing a Database Connection” issue is a corrupted theme or plugin.

Your theme might be the problem if the error happens after you’ve just activated a new one. To resolve the issue, simply navigate to Appearance -> Theme on your Admin Dashboard and then activate another theme from the available options.

Alternatively, you can disable all of your plugins if changing the theme doesn’t resolve the problem. After that, enable each plugin one by one to find the culprit.

If you cannot access your dashboard, deactivate your theme or plugins by renaming the corresponding folders from your hosting control panel’s file manager.

Go to the File Manager and open the wp_content folder located in the public_html directory. Then, rename the currently used theme or the plugins folder. This will disable your theme or all of your plugins at once.

rename the currently used theme or the plugins folder to disable your theme or all of your plugins at once

Keep in mind not to disable themes and plugins at the same time in order to find out the root of the problem.

For instance, rename the plugins folder first and then refresh your site. If the error disappears and you can access your dashboard, then you know for sure that one of the plugins is the issue.

But how do you know the exact plugin that causes the issue?

Well, after disabling all of them, restore the plugins folder to its original name. This way, all plugins will still be inactive, but you can adjust the settings from the admin area.

After that, activate the plugins one by one, refresh your site, and keep doing that until you find the one causing the issue.

Pro Tip: Don’t forget to delete the theme or plugin that causes the error.

Method 4: Create A New Database

If all of the methods above still yield no result, your database might be completely corrupted and cannot be used anymore.

In that case, you need to create a new database and user. To do so, head over the MySQL Databases top page and fill in the form.

head over the MySQL Databases and create a new database

Once a new database is created, import your database backup so you don’t have to start everything from scratch.

Conclusion

Getting the “Error Establishing a Database Connection” error can be frustrating. Your website won’t load, and there’s a risk of losing traffic and customers.

The error occurs when your site fails to retrieve the information from a database. It might be caused by different reasons, such as incorrect login credentials in the wp-config.php file, theme or plugin conflicts, or a traffic overload.

Try these three methods to solve the error:

  • Fix the Login Credentials — make sure to match the login credentials in your wp-config.php file with the ones in your MySQL Databases.
  • Repair Database — if your database is corrupted, repair it by inserting the define(‘WP_ALLOW_REPAIR’, true); function into the wp-config.php file.
  • Fix the Corrupted Files — deactivate the theme or plugin via your dashboard, or rename the folders from your control panel.
  • Create a New Database — start over with a new database and import a backup.

Turns out the error is not as scary as you may have initially thought, isn’t it? Simply follow the tutorial, and your site will be up and running in no time!

Original author – Hostinger  Best Cheapest Web Hosting in India.