How to Fix “Error Establishing Database Connection” in WordPress

Get HubSpot's WordPress Plugin
Anna Fitzgerald
Anna Fitzgerald

Updated:

Published:

As a WordPress site owner, there are a few error messages that you dread. The “error establishing database connection” is among the most feared, but rest assured that it’s common and can be resolved in a few ways.

User fixes “Error Establishing Database Connection” in WordPress

In this post, we’ll take a closer look at what exactly this error is and then walk through the different steps you can take to fix it.

Grow Your Business With HubSpot's Tools for WordPress Websites

What does “Error Establishing a Database Connection” mean?

You can figure out that WordPress had trouble connecting to a database from this error message, but what exactly does that mean?

To fully understand this error, we need to look behind the scenes at how WordPress displays web pages. This complexity is hidden from the visitor.

You’ll remember from installing your WordPress site that you had to create a database. That’s because every site needs a centralized database that stores information in order to run. Your posts and pages, categories and tags, user data and comments, plugin and theme-related data, and site-wide settings are all stored there.

To access that treasure trove of data, WordPress uses a programming language called PHP.

Whenever someone enters a URL into their browser, WordPress uses PHP to ask for information from the database. It then brings back the necessary data to display that web page to the visitor on their browser. When WordPress is unable to establish a database connection, it will display an error message instead.

The "error establishing a database connection" may appear like this in your browser

Source

See how the page is mostly blank? That’s because no data can be retrieved to render the page since the database connection isn’t working. Such an error will not only prevent your visitors from accessing the frontend of your site, but it will also prevent you from accessing your WordPress dashboard.

To get your site running as smoothly as possible again, let’s look at a few different solutions for this issue.

Though the “error establishing a database connection” message might seem ominous, we know that one common cause is as simple as updating your credentials. While some of these steps are more difficult and require a bit of coding, they are all workable solutions for users at any level. Let’s look at these solutions more closely below.

Step 1: Repair and optimize your database

Your first step? Go to your WordPress dashboard and see what error message appears. If you see the same “error establishing a database connection,” then move onto the next step.

However, if you see another message, notice the suggestions that WordPress offers. One such message might begin, “One or more database tables are unavailable...” This means you need to repair and optimize your database before accessing it.

WordPress simplifies the process for you. Just log in to your control panel, open the File Manager, and navigate to the wp-config.php file. At the end of the file, right before the line that reads /* That's all, stop editing! Happy blogging. */ add the following line of code.

 

 

   define('WP_ALLOW_REPAIR', true);

Save your changes and go to yoursite.com/wp-admin/maint/repair.php. You should see the following message.

One way to resolve an error establishing a datasbase connection is to add code to your wp-config.php file and then click repair and optimize datasbase in your your admin dashboard

Source

You can click either button, but the latter is recommended to optimize your site’s performance. When the repair is finished, go back to your site and check if the error is still there.

If there’s no error, that’s great — but you’re not quite finished. You need to go back into your wp-config.php file, remove that line of code you added, and save it. If the error still appears when you visit your site, you’ll have to move on to the next step.

Step 2: Verify your database login credentials

You know you need to remember your login credentials to access your database. That’s a no brainer. But there may be incorrect details stored in your WordPress configuration file (wp-config.php) impacting your connection. This can happen when your file is not updated properly after migrating to a new hosting provider.

To verify that your information is correct, open the wp-config.php file via your cPanel and look at the following lines of code.

 

 

   define('DB_NAME', 'database-name');

define('DB_USER', 'database-username');

define('DB_PASSWORD', 'database-password');

define('DB_HOST', 'localhost');

You can compare these values to those stored in your database to ensure they match. You can find that info by logging on to your cPanel and going to the Database section.

If the values do not match, edit the wp-config.php file and save your changes. Then navigate to yoursite.com/check.php. If you get a “connected successfully” message then your login details are now correct and the database connection error should be resolved.

Step 3: Restore default files

It’s rare but core WordPress files can be corrupted. A hacking attempt on your site or a rogue plugin may modify these files.

Whatever the reason, you can restore the default files. The process is simple, but the stakes are high. One mistake can wipe out your site. That’s why you need to back up your site before proceeding.

Now that you have a backup, download a new version of WordPress. Unzip the file, then find and delete the wp-config.php and wp-contents folder. This will ensure you don’t override your existing wp-config.php file or lose any themes, plugins, and media stored in the wp-content folder once you restore WordPress.

When restoring your default files to resolve an error establishing a database connection make sure to delete find and delete the wp-config.php and wp-contents folder

Source

Next, open File Manager on your cPanel and upload your new WordPress files. This will override all the previous files (except for wp-config.php and wp-contents folder).

When the upload is complete, go back to your site to see if the error is resolved. If you’re still seeing an error message, it’s time to contact your hosting provider.

Step 4: Contact your hosting provider

If none of the steps above resolve your issue, then it could be an issue with your database server. A spike in traffic on the server, for example, could be the cause since web hosts like WordPress.com and Bluehost limit how many database connections are allowed at once.

If you let them know you’re experiencing an error establishing database connection on your site, they can let you know what the exact cause is. Best case scenario: they might even fix it for you.

There’s no foolproof method for preventing every error establishing a database connection, but there are steps you can take to resolve them. By fixing this error, you’ll ensure that visitors continue to have seamless experiences on your site.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Related Articles

Capture, organize, and engage web visitors with free forms, live chat, CRM, analytics, and more.

DOWNLOAD THE FREE PLUGIN

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

START FREE OR GET A DEMO