How to fix an error establishing a database connection in WordPressIt’s a sinking feeling when you go to access your WordPress site only to be met with a big fat Error Establishing a Database Connection message.

If you’re reading this post, you’re probably experiencing it right now. You typed in your URL, hit enter, and…this:

an example of error establishing a database connection

What’s causing the problem? And is your site lost for good?

Don’t worry about that second point. It’s highly unlikely that there’s any permanent damage to your site. And most of the time, you can get back up and running without too much stress.

In this post, we’ll tell you a little bit more about what the Error Establishing a Database Connection error actually means. Then, we’ll get into some of the strategies that you can use to diagnose what’s causing the Error Establishing a Database Connection message, fix the problem and get your site back to proper form.

What Does Error Establishing A Database Connection Mean?

In broad strokes, think of your WordPress website as two co-dependent halves.

In one half, you have all of the code that determines how your content looks and functions. This is what your theme and plugin files handle. For example, your theme’s template files say “put the post title here” and “put the post text there.”

The other half, your database, is where all of your actual content is stored. Your database houses everything – posts, usernames, settings, etc.

Basically, your site’s code affects the structure and function of your website, but your database is where the actual words on your website are stored.

Of course, that explanation is a little oversimplified. But the important thing to understand is that both halves are essential to the functioning of your WordPress site.

Beyond that, both halves need to be able to “talk” to each other for your site to work.

This Error Establishing a Database Connection message is what you see when the two are unable to communicate with each other.

What Causes the Error Establishing a Database Connection Message?

A few different things can cause the issue. We’ll cover solutions for each. But to give you a broad overview of the potential issues, here’s a quick list:

  • A corrupted database
  • Incorrect database login credentials
  • Hosting problems like an overloaded database or other issues

You can fix the first two by yourself, but you’ll need the help of your host for the third issue. For that reason, we’re only able to show you how to fix the first two potential problems.

How to Diagnose and Fix a Corrupt WordPress Database

When you see the Error Establishing Database Connection message, the first thing that you should do is see if you can still log in to your WordPress admin dashboard.

If you can access your dashboard, the issue is likely just a corrupt database, rather than either of the other two problems.

Luckily, WordPress actually includes a built-in tool to fix a corrupt database.

To access it, you’ll need to connect to your server via FTP and edit your wp-config.php file. You’ll find this file in the root directory of your WordPress site:

how to edit wp-config.php

Add this line of code to the end of your wp-config.php file and save the file:

define('WP_ALLOW_REPAIR', true);

where to add code snippet

Then, head to “yourdomain.com/wp-admin/maint/repair.php”. Make sure to replace “yourdomain.com” with your actual domain name.

On that page, click the Repair Database button and let WordPress work its magic:

repair WordPress database

Once the repair tool finishes, your site should hopefully work again. Then all you need to do is go back and delete the line of code that you added to your wp-config.php file and you’re done!

How to Fix Incorrect Database Login Credentials

If you’re unable to access any part of your WordPress site, another likely Error Establishing a Database Connection culprit is incorrect database login credentials.

While there is a way to diagnose if this is the problem, it actually takes longer to diagnose the problem than it does just to fix it! For that reason, it’s easier to just fix the problem from the get-go.

To do that, log in to your hosting dashboard. Most of the time, your host will use cPanel for its dashboard.

In cPanel, find the MySQL Databases tab:

mysql database

Scroll down to the Add a New User box and create a new user. Keep the username and password handy because you’ll need them in a second:

add new database user

Once you create your new user, scroll down to the Add a User to a Database area and add your new user to your WordPress site’s database:

give user access to database

On the next page, make sure to give the user All Privileges:

make sure to give all privelages

Then, connect to your site via FTP and edit your wp-config.php file. In the file, update the values for DB_USER and DB_PASSWORD to reflect the new username and password you just created:

update wp-config.php with new details

With any luck, the Error Establishing a Database Connection message should be gone and you’ll see your site like normal.

Still Not Working? Call in the Pros

If neither of the above methods worked, try talking to your host to see if there’s an issue with your database server. Either the host may be experiencing known outages, or you may be exceeding the limits of your hosting.

And if none of the above has solved the problem, you can always lean on WordPress experts to dig in and do the dirty work for you.