4 Tips to Fix your Error Establishing a Database Connection

error establishing a database connection

Error messages are generally upsetting, but there’s something particularly intimidating about the “Error Establishing a Database Connection” message. Maybe it’s the fact that it’s black text against a stark white background with none of the usual WordPress branding, or maybe it’s because very few people know what “error establishing a database connection” even means.

So, what’s up with this error message? And how can good web hosting help?

What “error establishing a database connection” means

When you buy a domain, your website still needs somewhere to store all the posts, images, and everything else that makes your website what it is. “Error establishing a database connection” means that WordPress is having trouble accessing the database that houses all your website’s content.

This can happen for many reasons, but it usually happens because of one of these:

  • The database server is down: If the server that stores your website is different from the one that stores your database, you might get an “error establishing a database connection” message if the database server is down.
  • You’re getting more traffic than usual: More web traffic is the goal for most websites, but with more traffic comes a lot of extra pressure on your server.
  • The database is corrupted: This sounds scary, but it’s not necessarily the result of any malice. Something as innocent as a failed theme can corrupt your database.
  • The login credentials are incorrect: To access your database, WordPress needs a particular combination of username and password. If any of those change, WordPress won’t be able to connect with your database.

Now that you know what causes the “error establishing a database connection” message, here’s how you fix it:

4 tips to fix “error establishing a database connection”:

1. Check your login credentials

It’s very rare that login credentials will change on their own, but just in case this is what’s causing the “error establishing a database connection” message, it can’t hurt to check your credentials.

To do this, you’ll need to head to the “wp-config.php” file and look at these lines to make sure they’re displaying the correct information:

  • define(‘DB_NAME’, ‘some_db’);
  • define(‘DB_USER’, ‘some_username’);
  • define(‘DB_PASSWORD’, ‘some_pass’);
  • define(‘DB_HOST’, ‘localhost’);

If your name, username, password, or host is incorrect, WordPress won’t be able to connect to your database. Here’s what you’ll need to do to fix it:

  • STEP ONE: Copy the four lines from the “wp-config.php” file to the clipboard.
  • STEP TWO: Contact your web host’s customer service via email or chat.
  • STEP THREE: Paste the four lines into the chat window or email.
  • STEP FOUR: Ask your web host if the lines are displaying the correct information.

If the information is wrong, your web host will give you the correct information, which you can now enter into your “wp-config.php” file in place of the incorrect information.

Once you’ve finished with this, use FTP to reupload the file.

If your credentials are correct, try the next tip:

2. Check for corrupted themes or plugins

Here’s how you do this:

  • STEP ONE: Use FTP to connect to your host. (You’ll need the login and password to your FTP account. Ask your web host for help if you’ve forgotten these.)
  • STEP TWO: Connect to the server and head to the WordPress root folder. (You can usually find it if you look for “public_html”. You’ll know you’re in the right place when you start seeing sub-folders with names like “wp-admin” and “wp-content”.)
  • STEP THREE: In the “wp-content” sub-folder, rename the “plugins” folder to something else. Choose any name you like.
  • STEP FOUR: Check your website to see if it’s still displaying the “error establishing a database connection” error. If it isn’t, you’ll know that a plugin was causing the error. (If it’s still displaying the error, skip the rest of this section and move to tip #3.)
  • STEP FIVE: Rename your plugins folder back to “plugins”.
  • STEP SIX: In the “plugins” folder, rename the first plugin file to something other than its original name, then check to see if the error is gone. If it’s still there, rename the plugin back to its original name.
  • STEP SEVEN: Repeat step six for each of the other plugin files, one by one, until you no longer see the “error establishing a database connection” message.
  • STEP EIGHT: Once you’ve identified the misbehaving plugin, you can either delete it, check online to see whether there are any tips on how to fix it, or contact the plugin developer for help.

3. Check if your database has been corrupted

Before you follow any of the steps in this tip, go to your WordPress dashboard. If you’re seeing the “error establishing a database connection” message there, you can skip this tip and move to tip #4.

If you’re seeing another message from WordPress, follow the steps WordPress tells you to, and you’ll have fixed your corrupted database.

If you see a message beginning with “One or more database tables are unavailable”, follow these steps to repair the database:

  • STEP ONE: Go to your WordPress root and download the “wp-config.php” file to your desktop.
  • STEP TWO: Edit the file in Notepad by adding this line before the line that says “That’s all, stop editing”:

define(‘WP_ALLOW_REPAIR’, true);

  • STEP THREE: Using FTP, upload the new file to your website so it overwrites the old one.
  • STEP FOUR: In the “repair” section of your “wp-admin” folder, click “repair database” or “repair and optimize database”.
  • STEP FIVE: Check if you’re still seeing the error message. If you are, move to the next step. If not, Congratulations on fixing your error! Either way, be sure to follow the final step in this tip:
  • STEP SIX: Revert your “wp-config.php” file to the way it was before you added that extra command line in Notepad. Once you’ve removed the extra line, reupload the file.

If none of these tips work, it’s time to call in the cavalry:

4. Contact your web host

Tell your host that you’re seeing the “error establishing a database connection” message and let them know all the things you’ve done to fix it. It might be that the problem is on their side and that they’re working on it. But even if it’s an error on your side, a good web host should be able to help you fix it.

To prevent an error message from ever grinding your website to a halt again, ask your web host about cloud backups.