How to fix the terrible WordPress Error establishing a database connection

If you are reading this article, you may have just encountered the terrible “error in establishing database connection” (EEDC). As a WordPress website owner, seeing a blank screen with an error message is one of your worst nightmares. Please be assured that we can help you solve the problem.

EEDC is very common and can usually be resolved quickly.

Error establishing a database connection is abbreviated as EEDC

The most frustrating part about this error is that you may encounter this error even if you have not made any modifications to the site. In this blog, we will guide you through the possible causes of the error and explain some possible solutions.

Why do I see this error?

Website errors are inevitable, and you may have accidentally triggered them.

When WordPress cannot access your MySQL database, your website will return an “EEDC” error.

The reason why WordPress cannot connect to the database may be one of the following:

  • Login credentials have been changed
  • The server is not responding
  • Corrupted database or WP file
  • High traffic

Follow the steps to fix WordPress Error Establishing a Database Connection

Step 1

The first thing you need to do is to check that both the front end (yoursite.com) and the back end (wp-admin) of the site receive the same error message “An error occurred while establishing a database connection.” If so, you can go to step 3.

If you see another message on the back end, it may indicate that your database needs to be repaired, and then continue with the following steps to resolve the issue.

Step 2: Repair the database

To repair your database, find the wp-config.php file, which you can find in the root directory of WordPress. The root directory is where your WordPress site is installed. After gaining access to wp-config.php:

  • Open it in edit mode.
  • Right-click the file and open the code editor (will open in a new tab).
  • Add the following code at the bottom. define (’WP_ALLOW_REPAIR’, true);
  • Save Changes.
  • Navigate to yoursite.com/wp-admin/maint/repair.php. It should open the following screen
Word Press数据库修复
  • Select “Repair Database” or “Repair and Optimize Database”. After choosing an option, WordPress will repair the database for you.
  • After repairing the database, go back and delete the code snippet added to the wp-config.php file.
  • Your database should be repaired.

Check if your website has been restarted and running. If the “EEDC” error is displayed, proceed to step 3.

Step 3: Verify your login credentials

Determine if you have the correct login credentials in your wp-config.php file. This is probably the single most important file in the entire WordPress installation. You should cross-reference the following four credentials with the credentials stored in the database.

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

Log in to phpMyAdmin and click the “Database” tab to view the list of databases. If the database after DB_NAME appears in the list, the database name is not a problem.

If you do n’t see the database name, you must contact the hosting provider because this may be a problem with their server.

Now, you should check whether the database user name and password are correct. To do this, create a .php file and store it in the WordPress root folder. To create a new file, simply open the WordPress root folder and click “New File”. Then continue to save this new file with any name you need.

After creating the file, right-click and open the code editor to enter a blank page. Go through the following code and make sure to replace “localhost”, “root” and “password” in wp-config.php with the database host, username and password.

Save your changes, and then navigate to yoursite.com/check.php. If you receive the message “connected successfully”, your login details are correct. If you see “Unable to connect …”, you can determine the problem.

You will have to create new login details and update the wp-config.php file.

Step 4: Repair damaged WordPress files

Eliminating problematic files can eliminate errors, but small errors can eliminate the entire site. Before deleting or restoring any files, make sure to back up the entire WordPress website.

After backing up the website, please download a new copy of WordPress and unzip the package. Delete the wp-config.php file and wp-contents folder. This will ensure that you will not overwrite or destroy the existing wp-config.php file. Then upload the remaining files to your WordPress root folder.

This should have replaced all the problematic files. Reload your website. If you still see errors, try clearing your browser ’s cache and reloading the website. If the error persists, you should call your web hosting provider.

Step 5: High traffic

Let’s see if there is a lot of traffic causing the terrible “error in establishing database connection”. If you use shared hosting, this problem is most likely. Your hosting server may be overwhelmed and unable to handle the influx of website visitors.

Talk to the hosting provider and ask if your MySQL server is responding. If you are confident, you can check it yourself. Test other websites that are using shared servers to see if they have the same problem. If so, then there must be a problem with your MySQL server.

WordPress修复 data

If this is an ongoing problem with your website, you should seriously consider setting up dedicated hosting or automatic scaling on one of the major cloud providers such as AWS.

Avoid this error in the future

There is no guarantee that your website is 100% fail-safe. However, in order to minimize the possibility of encountering website errors, we recommend that you work with quality web hosting service providers and back up your data regularly. The best practice for WordPress is to install a security plugin that will automatically run tests on a regular basis to let you know the status of the website.

2 thoughts on “How to fix the terrible WordPress Error establishing a database connection”

  1. Avatar

    Hello there! This blog post couldn’t be written any better!
    Looking through this post reminds me of my previous roommate!
    He constantly kept preaching about this. I most certainly will forward this information to him.
    Fairly certain he’s going to have a great read. Thanks for sharing!

Leave a Comment

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