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

I’ve spent the last five years diving deep into the world of WordPress as a Technical Support Shift Lead at WPX. Throughout these years, I’ve encountered almost every issue you can think of, and one of the most common ones people bring to me is the “Error Establishing a Database Connection.” It can be pretty frustrating to see this error on your site, but don’t worry – you’re not alone, and it’s usually a straightforward fix.

Let me share a detailed guide with you to help troubleshoot and resolve this issue, just as I’ve done for many others. Together, we’ll get your site back up and running in no time!

What Is Error Establishing a Database Connection?  

The error itself looks like this when you open your site in the browser. It is a common message that occurs when a website cannot communicate with its database. You’ll often see this in web applications built with CMS (Content Management Systems) like WordPress, which rely heavily on a database for storing and retrieving content.

error establishing a database connection

What are the most common reasons for “Error Establishing a Database Connection” in WordPress?

  1. Incorrect Database Credentials: 

This is one of the most common reasons for the error to appear in your browser. The database name, hostname, username, and password should match the configuration file of the website. 

For WordPress, those database details are found in the wp-config.php file. 

  1. Corrupted Database

The database may be corrupted, causing the website to fail when trying to establish a connection. The WordPress database is made of tables that store your site’s data. Although the database is a robust system like any other technical system, it can experience failure and corruption.

  1. Firewall or Security Software

Firewall rules or security software may be blocking the connection between the web server and the database server.

  1. Exceeded Database Limits

The website has exceeded the maximum number of database connections allowed by the server, often due to high traffic or poorly optimized queries.

  1. Database Server Down

The database server is not running or is inaccessible due to network issues or server downtime

How to Fix the WordPress “Error Establishing a Database Connection” error?

Before making any changes to the website, you need to create a backup of your website. Here is how you can back up your website in the WPX control panel.  

This ensures you can easily restore everything if any other issue pops up in the meantime. Taking this step can save you a lot of headaches down the road! Now that you have your backup ready, let’s fix the error!  

Step 1: Check Your Database Credentials

Incorrect credentials are the most common cause of this “Error Establishing a Database Connection”. To resolve it you need to verify and correct your database login details and apply changes to your wp-config.php file. 

1.1. Access Your File Manager or FTP Client

Depending on your preference, you can use an FTP client like FileZilla, or your hosting provider’s file manager to access your WordPress files.

1.2. Locate wp-config.php

This file is located in the root directory of your WordPress installation and connects your site’s file and the database. Because I am using WPX for this example, the root directory for my site is called /public_html, which should be the case for most hosts. 

1.3. Open wp-config.php

Open the wp-config.php file in a text editor and check the credentials for your database. Here is how it looks opened from the WPX File Manager. 

wp-config.php

1.4. Verify the credentials

Make sure that the database name, username, password, and host are correct. You can verify these details in your hosting control panel. For example, here is how you can check the database details in the WPX Control Panel. 

Note: If your site is hosted with WPX as is mine, make sure that the DB_HOST is always set to localhost. 

verify credentials - step 1
verify credentials - step 2

In this example, my site uses a database called wpxexamp. So the parameter after DB_NAME is set to ‘wpxexamp’. 

Step 2: Repair the Database

If the credentials are correct but the error still appears, your database may be corrupted. You can repair the database by following these steps:

2.1. Enable Database Repair Mode

To allow the repair of your database, add the following line to your wp-config.php file, just before the /* That’s all, stop editing! Happy publishing. */ line:

define(‘WP_ALLOW_REPAIR’, true);

2.2. Run Repair Script

After that, load your website in the browser, but add / wp-admin/maint/repair.php

For example: https://yourwebsite.com/wp-admin/maint/repair.php

2.3. Repair Database

In the on-screen wizard, you will have two options: 1. Repair the database and 2. Repair and optimize the database. What do the two options mean? 

  1. Repairing the database focuses on fixing any corrupted or damaged tables within the WordPress database.
  2. Repairing and optimizing the database not only fixes any corrupted tables but also optimizes the database for better performance. The optimization consists of reducing data fragmentation, reclaiming unused space, defragmenting tables to enhance read and write operations, and Updating table statistics for the query optimizer.

2.4. Remove Repair Line

After repairing your database, you can safely remove the line you added to wp-config.php.

define(‘WP_ALLOW_REPAIR’, true);

Step 3: Update the WordPress Site URL

Incorrect site URL settings can sometimes cause database connection issues. You need to update these settings by adding the following lines to your wp-config.php file, just before the /* That’s all, stop editing! Happy publishing. */ line:    

define(‘WP_HOME’, ‘https://yourwebsite.com’);
define(‘WP_SITEURL’, ‘https://yourwebsite.com’);

Step 4: Restore a Backup

If you recently made changes that caused this error, restoring your site from a backup can also resolve the issue. If you are using WPX as your host, you can be reassured that your website is backed up, as we backup our servers daily, and the backups are kept for 28 days.

If that is the case, you can follow our guide on how to restore your websites.

Step 5: Contact Your Hosting Provider

If none of the above steps work, it’s best to contact the support team of your hosting provider, as the issue is most likely server-related

You can contact the WPX Support team anytime, as they are available 24/7/365 to help you with any technical issues. 

Additional Tips

  • Review Error Logs: Check your server’s error logs for more details on what might be causing the connection issue.
  • Optimize Database Regularly: Regular database optimization can prevent corruption and ensure smooth operation.

Conclusion

It’s a bit daunting to encounter “Error Establishing a Database Connection” when you try to load your website, but with the right approach, you can typically manage that issue. When you understand the common causes, such as incorrect database credentials, server downtime, corrupted databases, exceeded database limits, host issues, or firewall settings, you can easily troubleshoot and resolve the error.

Additionally,  you can make regular database optimization and monitor server error logs to help prevent such issues in the future. With these strategies, you’ll be well-equipped to restore your WordPress site quickly and maintain its stability.

For more WordPress tips and troubleshooting guides, be sure to visit the WPX blog regularly!

Share Your Love
Svilen Draganov
Svilen Draganov

Svilen Draganov is a Shift Lead at WPX with five years of experience. As a WordPress expert, he specializes in working with customer sites, troubleshooting, and providing solutions to various issues. In addition to his technical expertise, he manages a team of support agents, working to enhance their technical knowledge. His leadership and skills contribute significantly to the overall success of the WPX team.

Articles: 1