How To Fix the WordPress "Error Establishing a Database Connection"
Blog Menu

I write and curate content for Bluehost. I hope this blog post is helpful.
Are you looking at creating a blog, website or an online store? Bluehost has something for everyone. Get started today.

No WordPress user wants to see the WordPress error message “error establishing a database connection.”

While it’s one of the most common errors experienced by WordPress users, it’s as frustrating as Microsoft’s blue screen of death.

Plus, if your website is inaccessible, your reputation, organic traffic and sales will take a hit. In a 2022 Uptime survey, 45% of respondents said their most recent outage cost them between $100,000 and $1 million.

Thankfully, the WordPress “error establishing a database connection” issue can be fixed with fewer complications than the blue screen of death.

In this guide, you’ll learn why you’re getting an error establishing a database connection in WordPress, the steps you need to take to fix the error and specific measures you can take to prevent the issue from happening again.

What does the WordPress message “error establishing a database connection” mean?

This database connection error happens when two essential parts of WordPress — PHP (Hypertext processor) and MySQL (My Structured Query Language) — stop communicating with each other.

To quote the famous line from the 1967 film Cool Hand Luke, “What we’ve got here is a failure to communicate.” Thankfully, unlike the dire situation in the movie, WordPress’s communication issues only impact a website and aren’t life-or-death situations.

For added context, let’s describe what each of these two parts of WordPress does:

       PHP (Hypertext processor): The scripting language used to carry out actions or operations on the server.

       MySQL (My Structured Query Language): The relational database management system that stores and manages WordPress data, including user information, posts and settings.

WordPress can’t retrieve the data needed to display your website when these essential parts stop talking to each other.

What causes the “error establishing database connection” in WordPress?

The most common reasons why you’re getting a database connection error in WordPress.

Like most things coding-related, multiple reasons can be behind a disconnect between PHP and MySQL.

Knowing the most common causes is key to successfully resolving the “Error establishing a database connection” issue in WordPress.

The most common causes are:

  • Your database credentials are incorrect.
  • You have a corrupted database.
  • The database server is down.
  • WordPress files are corrupted.

How to fix “error establishing a database connection” in WordPress

When an issue has many potential causes, the best solution is to check and address each possible cause systemically until you get to the root of the problem.

When you’re getting the “error establishing a database connection” message in WordPress, that means going through seven steps.

A word of caution before we proceed. Take time to back up your WordPress website. While these steps normally don’t cause issues, there is always a chance of a typo creating a new problem. If you backup first, you can restore your WordPress website to the saved version in case your changes break something else and need to be rolled back.

1. Check your database login credentials

You’re most likely getting the database connection error in WordPress because your database credentials (your name, username, password or server information) are incorrect. When that happens, WordPress doesn’t believe the database access attempts are safe and blocks them.

Your database credentials might be incorrect because:

  • You moved your WordPress website to a new host.
  • The password may have expired due to the database system enforcing password expiration policies.
  • The password may be non-compliant, and the database system enforced password policies.

You can locate your WordPress database credentials in the wp-config.php file, a core WordPress configuration file that contains essential information about the database.

This critical information includes:

  • Database name.
  • Database username.
  • Database password.
  • Database server.

Access the WordPress installation’s root directory and open it via a text editor to edit the wp-config.php file.

If you’re using Bluehost, you can find your WordPress database with these steps:

1. Log in to your Bluehost Account Manager.

2. Click the Hosting option located on the left-hand side of your Account Manager dashboard.

Navigate to Hosting on the left-hand side of the Account Manager dashboard.

3. Choose File Manager under Quick Links.

Go to File Manager under Quick Links.

4. Locate the folder holding your WordPress website files in the cPanel File Manager (usually within the public_html folder). Look for the wp-config.php file in the list of files.

Look for the wp-config.php file in cPanel file manager.

5. Next, open the code editor by right-clicking on the file and selecting Edit from the drop-down menu.

Select Edit from the drop-down menu to edit the code editor.

6. In the file editor, locate the following lines of code for your MySQL settings. Note that the first line may read “MySQL settings or “Database settings.”

// ** MySQL settings** //

/** 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’ );

WordPress database credentials are located in the wp-config.php file.

7. Next, check if the database name, username, password and host information are correct.

To confirm if your database credentials are correct, log into your WordPress hosting account dashboard and check the MySQL database associated with the website.

To access your credentials with Bluehost, log in to your Bluehost account, then go to Hosting under Quick Links and then CPANEL.

Go to CPANEL under Quick Links.

8. Under Databases, select MySQL® Databases.

Navigate to MySQL Databases.

9. Under Current Databases, you’ll find your database username (or names).

Find your database username under Current Users.

10. Edit your credentials in the wp-config.php to match the credentials shown by your current host.

11. After editing, refresh WordPress to check if you’re still getting the database connection error.

If the error persists, the password in your wp-config.php file may be incorrect. In this case, resetting the password might fix the issue.

To reset the password, go back to MySQL® Databases and scroll down the page until you see the Current Users section. Find the current database and tap Change Password.

How to change your MySQL user password.

After resetting the password, modify the password in your wp-config.php file to match the new version. Save the changes and refresh your WordPress website to see if that resolved the error.

If you still get an error establishing a database connection on your WordPress website, move on to the next step.

2. Check if your database host information is correct

Another possible reason for the “error establishing a database connection” could be incorrect database host information.

If you’re using Bluehost, your database host is localhost. To confirm if your WordPress website is using the same database host information, open your wp-config.php file again and make sure you have the following line of code under  /** Database hostname */.

define( ‘DB_HOST’, ‘localhost’ );

If you’re not a Bluehost user, contact your web hosting company to determine the correct database host information.

Once you’ve confirmed the database host information is correct, you’ll need to repair your database if the database connection error continues.

3. Repair your WordPress website’s database

Even if your WordPress database credentials are correct, a broken or corrupted database due to problem plugins or theme instructions could cause trouble.

Access your WordPress admin dashboard and look for a message that says, “One or more database tables are unavailable. The database may need to be repaired.” That message indicates a corrupted database.

You can repair your database using the built-in WordPress repair tool.

Open your wp-config.php file again and enter define (‘WP_ALLOW_REPAIR’, true);above the line:

/* https://yoursiteURL.com/wp-admin/maint/repair.php

That’s all, stop editing! Happy publishing. */

Enter this line of code to repair your WordPress website’s database.

Choose Save Changes and then go to: URLhttps://yoursiteURL.com/wp-admin/maint/repair.php (replace ‘yoursiteurlcom’ with the actual URL of your website).

Once the URL loads, you’ll be given the following two options:

  • Repair Database
  • Repair and Optimize Database

If your priority is to resolve the database connection issue as soon as possible, choose Repair Database. However, if you also need to optimize your database, go with Repair and Optimize Database instead (if you’re willing to wait 30 minutes to an hour).

It theses steps fixed the issue, remove the line of code you entered into your wp-config.php file so no one else can access the repair.php page.

If the database connection error is still there, continue to the next step.

4. Restore the WordPress default files

Some WordPress files are essential for the platform to establish a connection with the database. If those files are corrupted from a hack or an update malfunction, you’ll need to fix them to stop the database connection error in WordPress.

Thankfully, restoring the default tiles won’t remove your plugins, themes or content.

That said, you may lose certain custom changes you’ve made on your website, so make sure you’ve backed up your WordPress website before proceeding.

To restore the default files of your WordPress website:

  • Download the default version of WordPress via https://wordpress.org/download/.
  • Unzip the file.
  • Delete the wp-content folder and the wp-config-sample.php file.
  • Use an FTP client to upload the new WordPress files to the main WordPress root.
  • Proceed with the overwrite.

When complete, refresh WordPress and see if the database connection error has resolved.

5. Restore your WordPress website with a database backup

If the database connection error is still happening, try to restore your WordPress website with a database backup.

The idea is to revert your website to a previously known working state by restoring a backup of your database. If recent changes, updates, or a hack corrupted your database, this will likely solve the problem.

Restore your WordPress website with a database backup using the following steps:

1. Log in to your Bluehost Account Manager.

2. Click on CPANEL under Quick Links.

3. Scroll down until you reach the Databases section and click on phpMyAdmin.

Go to phpMyAdmin under the Databases section.

4. Tap the Import tab at the top of the screen.

Tap the Import tab.

6. Locate the SQL file of your WordPress database backup on your computer.

Choose a database file to import into the current server.

6. After selecting the file, click the Import button to begin the import process.

Press the Import button.

7. Wait for the import job to complete.

8. Once you’ve successfully imported your WordPress database, reinstall WordPress using the imported WordPress database.

6. Check for server downtime or maintenance

Sometimes, the “Error Establishing a Database Connection” issue is caused by server downtime or ongoing maintenance activities on the hosting server instead of a problem with your website.

If the server hosting your WordPress website is down, it disrupts the connection to the database.

Try these methods to check for server downtime or maintenance:

  • Monitor server status: Some hosting providers offer a status page or dashboard where you can view real-time information about server status and uptime. Check this page to see if there are any reported issues with the server hosting your website.
  • Contact hosting support: If you need help finding information about server downtime or maintenance through the hosting provider’s website or status page, consider contacting their support team for assistance. They can provide insights into any ongoing server issues and advise you on the best action to resolve the problem.
  • Monitor website traffic spikes: Occasionally, a sudden surge in website traffic can overwhelm the server, leading to temporary downtime or performance issues. Check your website analytics or monitoring tools for any unusual spikes in traffic that coincide with the occurrence of the database connection error. If you detect traffic spikes, consider implementing caching solutions or upgrading your hosting plan to handle increased traffic demands effectively.

7. Contact your web hosting company for more troubleshooting help

At this point, you’ll need your web host’s help to track down some of the less common causes for the “error establishing a database connection” message in WordPress.

Contacting your web hosting company allows for a thorough diagnosis of the problem and provides tailored solutions. The support team can provide help based on your specific website setup, hosting and configuration.

With Bluehost, you get access to the following support options:

  • AI-driven support resources: Access our comprehensive library of troubleshooting guides and tutorials designed to help you resolve common issues quickly and efficiently.
  • 24/7 Bluehost support via chat or phone: Our dedicated support team is available around-the-clock to provide personalized assistance and expert guidance.

Take security measures to protect your WordPress database

Once you’ve fixed the database connection error in WordPress, you’ll want to take precautions to prevent future problems. These measures save a lot of time and energy down the road.

  • Perform regular backups: Schedule routine backups of your entire WordPress website, including the database. Bluehost’s managed WordPress plans offer free daily website backups for the first year (excluding the Basic plan), giving you peace of mind in case of unforeseen errors.
  • Keep everything updated: Ensure your WordPress installation, themes and plugins are always up to date. Regular updates often include security patches that help protect against vulnerabilities.
  • Choose trustworthy plugins and apps: Plugins are useful, but not all are safe. Some plugins can compromise your website’s security. Plus, installing too many plugins can result in code bloat and may compromise the performance of your website.
  • Choose a secure hosting provider: Choose a reputable hosting provider like Bluehost that prioritizes security to reduce the risk of database-related errors and issues that may compromise your WordPress website’s performance.

Final thoughts: How to fix “error establishing a database connection” in WordPress

The WordPress “error establishing a database connection” issue can be frustrating. But you can fix this all-too-common error with a systematic approach and a dash of patience for good measure.

Your webhost choice can help avoid the error in the first place as well. Pick a reliable web hosting provider like Bluehost that offers 24/7 support and hosting plans optimized for WordPress websites. Bluehost’s WordPress hosting plans are designed to provide a secure and efficient environment for your WordPress website.

Contact us today to learn how you can get more from your WordPress website with Bluehost.

  • Minal Agarwal

    Minal is the Director of Brand Marketing at Bluehost. With over 15 years of business experience in the technology industry, she strives to create solutions and content that fulfill a customer's needs. She is a dog mom and a stickler for calendaring.

    Education
    Masters in Marketing Management
    Previous Experience
    Strategic Partnerships, Customer Success, Events and Community
Learn more about Bluehost Editorial Guidelines

Write A Comment