11 Ways To Fix the WordPress “Error Establishing a Database Connection” Issue

Your website has been running smoothly for weeks, but then, just like that, it crashes.

Peering at the screen, you see a short yet sinister message.

“Error establishing a database connection.”

No matter your browser or device, the error appears when you load your site.

It’s the same for all your visitors and customers, too.

The longer you leave the error, the more business you lose. You need to act fast.


GIPHY

But how – and where – do you even start to fix this downtime problem?

Relax. In this tutorial, we’ll take you through precisely what the WordPress error establishing database connection message means, how to fix it, and who to call if you’d prefer help.

If you’re ready to start troubleshooting, jump to our suggested fixes list.

What Is the WordPress Database Connection Error?

The “Error establishing a database connection” message usually appears when your website can’t communicate with its WordPress database. Whenever people try to access your website, your database can’t transfer the information needed to display it.

It’s a common error with many different reasons for it to arise.

WordPress runs on a content management platform, storing all your website’s information in a MySQL database. Whenever someone visits your site, they request information from this database.

Your database contains data, including login details, page data, and plugin configurations. This data resides in a WordPress core file called wp-config.php in your site’s root folder.

If WordPress can’t communicate with your database to get this information, your website crashes.

What Causes the “Error Establishing a Database Connection” in WordPress?

The database connection error usually occurs because there’s incorrect information stored in wp-config.php.

However, there are a few other triggers for the error. Let’s break them down.

Error establishing a database connection meaning: Four possible explanations

 

Potential causeIn brief
There’s incorrect database information in your WordPress settingsDetails such as your database name, database username, database password, and hostname are different from those in your MySQL database, preventing communication
You have a corrupted databaseMissing or deleted data, malware, or hacking can cause WordPress databases to corrupt and stop speaking to your website
Your database server is experiencing problemsSpikes in traffic could cause your MySQL server to overload and crash
There are corrupt files in your WordPress installationCorrupted plugins, themes, and core files can prevent databases from working efficiently

11 Ways To Fix the Error Establishing a Database Connection in WordPress

It’s time to get your website and its database talking again.

Below, we’ve split 11 potential fixes into four categories:

  • Common fixes (quick checks that frequently solve this error)
  • Fixes to try next (techniques to fix less common triggers)
  • Technical fixes that work best with professional help (you’re free to try them, but it’s safer to ask for support)
  • Call in the cavalry (where you call a WordPress developer to take over)

Before you start…

You need access to an FTP program for many of the fixes suggested below.

FTP stands for “File Transfer Protocol.” It’s a manual route for making edits to your WordPress website.

We recommend downloading programs like Cyberduck or FileZilla to access your site via FTP. What’s more, you’ll need to contact your web hosting provider for login details.

Remember, too, that you should always back up your website before making changes – even – and especially – when your website is working normally.

Ideally, you should already be automating WordPress backups. If not, take a save point now.

Finally, please remember you don’t have to try these checks and fixes yourself.


GIPHY

If you’re uncomfortable about any step listed below, or you don’t have the time or free headspace to fix the error yourself, reach out to a WordPress developer who can handle it for you.

Otherwise, when you’re ready, let’s get started.

Fixing the “Error establishing a database connection” in WordPress: A Summary

 

Common fixesWhat to do
1. Ensure your domain points toward your host provider
  1. Log into your web host’s dashboard
  2. Make a note of your host’s “nameservers”
  3. Use a WHOIS tool to find your domain’s nameservers
  4. If the details don’t match, contact your host
2. Check and update your WordPress database credentials
  1. Log into an FTP program or cPanel, look for “wp-config.php,” and download it
  2. While still in FTP, right-click and edit “wp-config.php” and look for DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST
  3. Log into your web host’s dashboard and look for MySQL databases
  4. Check the database name in the dashboard against wp-config to see if they match
  5. Change your password in your host’s dashboard and edit wp-config to match
3. Check your database host information
  1. Check the hostname under “DB_HOST” in wp-config
  2. If it’s “localhost,” you might not need to take any action
  3. Contact your host to make sure the details are correct in any case
4. Update your WordPress website URL
  1. Log into phpMyAdmin through your host dashboard or cPanel
  2. Find your database and “SQL,” and enter code as a new MySQL query (see our guide for the code)
5. Repair your WordPress database
  1. Open wp-config to edit in your FTP program
  2. Add “define(‘WP_ALLOW_REPAIR’, true);” before the last line of text
  3. Save the file and navigate to your website with “/wp-admin/maint/repair.php” at the end of the URL
  4. Click “Repair” or “Repair and Optimize”
  5. Remove the repair code from wp-config and save
6. Check if you’re running corrupted plugins or themes
  1. Log into your WordPress dashboard and head to “Plugins”
  2. Deactivate and reactivate plugins one by one, refreshing your website as you go
  3. If your website comes back online after deactivating a plugin, leave it inactive
  4. Switch your theme to an alternative by heading to “Appearance” and “Themes”
Fixes to try nextWhat to do
7. Check if your database server is down
  1. Log into your web host dashboard or FTP program and create a new PHP file in the public folder
  2. Name the file “servertest.php,” and open it to add code (details are in our guide)
  3. Save the file, and navigate to your website with “/servertest.php” at the end of the URL
  4. If “connecting successfully,” your MySQL isn’t to blame
8. Restore default WordPress files
  1. Head to WordPress.org and download the latest software version
  2. Open the zipped file and delete “wp-config-sample.php” and wp-content
  3. Open your FTP program and drag the remaining files to the root folder
Technical fixes that work best with professional helpWhat to do
9. Create a new database
  1. Save a backup of your website
  2. Log into cPanel or your host’s dashboard, and look for “Databases” and “MySQL Databases”
  3. Create a new database following the process on-screen
  4. Look for “MySQL Users” and add new users to your database
  5. Repeat step two above and check your MySQL details match what’s in wp-config
10. Reboot your web server
  1. Log into your host dashboard or find customer support
  2. Ask your host to reboot your web server and advise how long it might take
Call in the cavalryWhat to do
11. Get a WordPress expert to help
  1. Contact your web host and advise them you’ve tried all the above points
  2. Work with a WordPress expert through a platform such as Proto by raising a support request

Common fixes

You can carry these fixes out independently if you have an FTP program. They’re typically the quickest ways to reverse the error.

1. Ensure your domain points toward your host provider

To run a WordPress site, you need a web hosting provider such as SiteGround or Bluehost.

A common reason for domain communication errors is that, upon changing a host, your database isn’t up to date with its details.

To check your domain and database point toward the correct host, you need to check that you’re connecting to the correct nameservers.

Start by logging into your host’s dashboard. Let’s assume you’re using Hostinger in this example.

Hostinger presents nameserver information under “Hosting” and “Plan Details” when you log in. Scroll to “Nameservers” and note down the details:

You now need a WHOIS tool to check your domain is using the correct host nameservers.

Load up Hostinger’s WHOIS tool and search for your domain name. A list of results appears:

Compare the details listed with the nameservers your host advised you of.

If they differ, either apply new nameservers yourself in the “Domain Management” or “Domain Overview” section of your WordPress host or contact your host directly and ask them to make changes.

Remember, this process only shows how Hostinger manages nameserver lookup. While this process works with most other hosts, you might need to use an external WHOIS tool, such as the service available through DomainTools.

2. Check and update your WordPress database credentials

If you change web hosts, your database details might not transfer properly.

You can check your database details using software such as cPanel and cross-reference information in your wp-config.php file.

Alternatively, you can log into your host’s dashboard and check database information to cross-reference.

Log into your FTP program (we use FileZilla here) and look for your root directory, typically named “public_html”:

In the root folder, look for wp-config.php. Right-click, then select “Download” to save it to your computer or local storage, or open a local folder in FileZilla and drag the file over to it. Doing so creates a backup of wp-config.php should you need to restore it later.

Now, right-click the original file in the root and click “Edit.” If you can’t access the right-click options using a Mac, hold down the “command” key and click the file.

In the text file, look for code lines marked:

  • DB_NAME
  • DB_USER
  • DB_PASSWORD
  • DB_HOST

They should appear like so:

// ** MySQL settings – You can get this info from your web host ** //
/** 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’ );

 

Keep the text file open and head to your host dashboard or cPanel.

To illustrate here, we’re using Bluehost.

Log into Bluehost and open your cPanel account.

Next, scroll down to “Databases” and select “MySQL Databases.”

On the following page, scroll down to find a list of current databases:

Scroll down further, and there’s a list of users with the option to change passwords:

Check your database name in wp-config.php next to “DB_NAME” and see if it matches what your host suggests is correct.

If the details differ, edit the text file by changing the correct database name next to “DB_NAME,” shown here:

/** The name of the database for WordPress */
define( ‘DB_NAME’, ‘database_name_here’ );

 

Next, edit your password as an extra measure. Follow Bluehost again and click “Change password” next to the default or main user.

Return to wp-config.php and edit in the correct username and password, as shown here:

/** MySQL database username */
define( ‘DB_USER’, ‘username_here’ );
/** MySQL database password */
define( ‘DB_PASSWORD’, ‘password_here’ );

 

“Save” the text file and close it, then try to reload your site.“Save” the text file and close it, then try to reload your site.

3. Check your database host information

Follow step two again to open up wp-config.php, and check the details under “Database / MySQL hostname,” like so:

/** MySQL hostname */
define( ‘DB_HOST’, ‘localhost’ );

 

Your host likely uses “localhost” as a functioning name, but it’s wise to check this is correct.

Contact your host directly for the correct name to use here and move on to the next steps.

4. Update your WordPress website URL

Again, if you’ve recently moved hosts, you might need to update your site’s URL with your MySQL database. You can do this by logging into phpMyAdmin through cPanel or your host’s dashboard.

Once logged in, select your database before selecting the “SQL” tab.

Enter the following code as a MySQL query in the field that appears, replacing the domains marked “old” and “new” with your previous and current URLs:

UPDATE wp_options SET option_value = replace(option_value, ‘http://www.old-domain.com’, ‘http://www.new-domain.com’) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;

UPDATE wp_posts SET guid = replace(guid, ‘http://www.old-domain.com’,’http://www.new-domain.com’);

UPDATE wp_posts SET post_content = replace(post_content, ‘http://www.old-domain.com’, ‘http://www.new-domain.com’);

UPDATE wp_postmeta SET meta_value = replace(meta_value,’http://www.old-domain.com’,’http://www.new-domain.com’);

 

Click “Go,” and the update should take effect. Reload your website to see if the error has disappeared.

5. Repair your WordPress database

You typically need to repair your database if you receive an extra error when you log into your WordPress dashboard:

To activate WordPress’s database repair mode, reopen wp-config.php in your root directory via FTP and add the following code toward the bottom of the text file, ideally just before “That’s all, stop editing! Happy blogging”:

define(‘WP_ALLOW_REPAIR’, true);

 

Save the file and open a new browser tab or window, then add “/wp-admin/maint/repair.php” to the end of your website’s URL.

Doing so brings up the following dialog box:

Click “Repair” or “Repair and Optimize” as you desire, and let the process complete.

Head back to wp-config.php in your FTP, remove the repair code you added, and then save and reload your website.

6. Check if you’re running corrupted plugins or themes

Poorly configured software can cause various problems, such as the 403 error and the dreaded “critical error on this website” message.

Start by simply switching your current theme. Log into WordPress and head to “Appearance” and “Themes” before switching to an alternative.

Check your website.

If there is no change, head to “Plugins” from WordPress’s side menu to bring up your full list of software.

Filter your active plugins by selecting “Active” and then click “Deactivate” underneath the first in the list.

Reload your website in another browser window or tab. If it’s back online, leave the rogue plugin deactivated.

If not, repeat the process and deactivate and reactivate each plugin, checking your website’s status in turn.

Deactivate WooCommerce

 

Fixes to try next

The following checks aren’t common fixes for database connectivity issues, but we advise you to follow them if steps one through six haven’t helped.

7. Check if your database server is down

The MySQL server could be down if manual checks and changes aren’t helpful. This problem occurs occasionally due to high traffic, affecting sites running on shared servers through cheaper WordPress hosting solutions.

The easiest option here is to speak to your web host directly. Ask if they can check server performance and make any recommendations.

If running several sites on one server, check your other pages to see if the same problem persists.

If you’re running a single website, you can check MySQL server issues by heading back to your root or public_html folder via your FTP program or host dashboard and creating a new PHP file.

For example, if you’re using Hostinger, you’d log in as usual, head to public_html, and click the “New file” option:

Name the file something memorable, such as “servertest.php” and open it. Then, insert the following code and swap out the “username” and “password” for your database credentials:

<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
}
echo ‘Connected successfully’;
mysqli_close($link);
?>

 

Save the file and open a new browser tab or window. Type in your site’s URL and add “/servertest.php” to the end (replace “servertest” if you used a different name).

If you receive a message stating “Connected successfully,” your server’s working as it should.

8. Restore default WordPress files

In dire circumstances, your core WordPress files have been corrupted and need replacing. Make sure you have a backup handy, as this can sometimes be a nuclear option.

Head to WordPress.org and download the latest version available to a desired folder.


Once downloaded, open the zipped file and the resulting folder, deleting “wp-config-sample.php” and the wp-content folder.

Then, reopen your FTP program and drag the remaining files in the unzipped folder to your root (or public_html).

Technical fixes that work best with professional help

Here’s where we typically recommend you contact a WordPress developer or your web host to support your website recovery.

If you’re not confident following these next two steps, skip to step 11.

9. Create a new database

Creating a new database is recommended if your MySQL is beyond repair. It’s one of the last options you should attempt, and we don’t recommend you consider it lightly.

Once again, remember to save a backup of your site at this point!

Let’s assume you’re using cPanel.

Log in with your credentials and head to “Databases” then “MySQL Databases.” Then, look for “Create New Database,” where you need to give it a name, and click “Create.”

create new database

It’s also wise to create at least one new user here, so scroll down and select “MySQL Users.”

Then, enter a new username and password before clicking “Create User.” You can then return to “MySQL Databases” and select “Add User To Database.”

Select the specific user and database you wish to associate, then click “Add.” Choose the privileges you’d like your user to possess or check “All Privileges.” Then, click “Make Changes.”

Follow step two in this guide again to ensure your new database credentials appear as expected in wp-config.php and update details with your host.

Finally, once your new MySQL database is created and all the credentials are correct, restore a backup to make sure you don’t lose essential website data.

10. Reboot your web server

Rebooting your web server is best reserved for your host unless you manage one yourself. For example, if you use local, virtual, or dedicated servers, you can use an SSH program and root access to kick-start your server and fix errors.

Please check with your web host if it’s possible to reboot your server from scratch.

Call in the cavalry

Whether you’ve tried all the steps above or prefer an expert’s insight, here’s how to reach out for advice.

11. Get a WordPress expert to help

It’s possible to fix many issues simply by contacting your web host. They have various controls and authority access to tools you might not have available.

Reach out to your host through their “contact us” page, live chat, or by logging into your host dashboard. Let’s assume you’re using SiteGround to demonstrate.

Log into SiteGround’s Client Area and select the “?” icon before choosing “View Help Center.” Then, select “Contact Us.”

Doing so brings up a series of support options. Select one if you wish (in this case, it’d be “Site is down, slow, or displaying an error”).

Alternatively, you can choose to speak to advisors through phone or live chat:

Finally, SiteGround also provides a “ticketing” system where you can fill out details about your error and track its progress through “Support History” available in the “Help Center.”

Remember, timescales vary depending on your issues, and there’s no guarantee your host can fix everything.

In which case, it’s worth consulting a WordPress developer separately. For example, you can raise support requests through Proto, StateWP’s WordPress management software.

Proto lets you track your WordPress site’s performance and enables you to raise requests with StateWP’s support team. Head to “Service Requests” and “Submit a Request” and include as much detail as possible about the communication error.

Screenshot of Proto service request screen

You can then track the progress of your service request within the Proto app while experts aim to address and resolve problems within a day.

You can raise all kinds of WordPress problems with expert devs. Whether your database isn’t communicating properly, your WordPress site is hacked, or elements such as your website’s sidebars are disappearing.

Getting the WordPress Error Establishing a Database Connection Message? Time to Get Your Tech Talking

The WordPress error “establishing a database connection” is common but still frustrating. After all, the longer you leave it, the more customers you lose.

Thankfully, as you can see, you can follow a few internal checks and measures to fix communication problems. Sometimes, it’s just a case of checking if your plugins work and that your database’s details match your website’s configurations.

However, checking and fixing these areas can get complicated – and you’re not alone if you feel you could benefit from some help.

Luckily, getting support to bring your site back online quickly is easy.

For example, when you raise support requests for WordPress errors through Proto, StateWP’s team can get your website back up and running within a day.

What’s more, you don’t have to worry about fixing technical problems yourself – and you can focus on running your business instead.


GIPHY

In the meantime, to prevent errors such as database connection problems, we highly recommend you check out our website maintenance checklist.

x