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

A code explaining how to fix the "error establishing a database connection" in WordPress on a screen of a laptop.

Do you have some trouble with your WordPress site? Are you seeing the dreaded error message that you don’t know how to handle? Well, this article is for you. Today, we’re talking about how to fix the “error establishing a database connection” in WordPress.

We’re talking about a fatal error here. WordPress can’t connect to the database, and that’s why you’re seeing it. Unfortunately, it means that none of the users can access the website. So, it will affect your site performance for sure — it’ll make it non-existing. Good luck trying to make money from blogging without a working website.

But since so many things can cause this issue, it can be tricky for a beginner to troubleshoot it. However, if you’re eager to learn and want to try it, you’ll find everything you need here.

Let’s talk about the cause of the problem first.

Why do I see the “error establishing a database connection” message?

As we have already mentioned, there are a few possible reasons why your site can’t connect to a database. It may be that your database information in WordPress is incorrect, maybe you have a corrupt database, or the database server quit working.

If that’s still too complex for you, let’s go a bit deeper in. A database is software used for storing, organizing, and retrieving data from other systems. WordPress uses databases to store everything that is on your pages. So, all the content, pictures, formatting, etc., are there. Each time someone visits your site, they see the information directly from that database.

To make a connection with it, WordPress needs some information:

  • Database name
  • Username
  • Database password
  • The server that the database is on

You can find all these in your wp-config.php file. If any of it is incorrect, you’ll get the infamous error.

We know that it all sounds scary, but it’s, in fact, one of the most common WordPress errors out there. Thus, there are many more reasons why this might be happening to you. Let’s go through the process together, and we’ll explain to you what you should check.

Process to Fix Database Errors

Take a look at your WordPress credentials

Although it’s unexpected, experts from WP Full Care say that wrong credentials are the most common reason for this problem. If you just moved your site to a new host or changed something big like that, this is the first thing you should check.

You’ll need to locate your wp-config.php file to do this. Once you’re in, you want to find lines of code that say ‘DB_NAME’; ‘DB_USER’; ‘DB_PASSWORD’. Make sure that all the information here is correct. Check it with your web host to make sure all is as it should be.

On the other hand, if the information is incorrect — change it. Save all the changes, and try to visit your site once again. Did that fix it? If not, here’s the next step.

Check your database host information

If you’re positive that your database name, username, and password, are all correct, it’s time to look at the ‘DB_HOST’. You’ve probably already seen it when checking the first three things, so just scroll a bit down.

Most web hosting companies that work with WordPress use localhost as your database host. However, if you’ve upgraded to one of those managed web hosting solutions, they might be using a separate server. If this is the case, they’ll tell you what exactly you need to type in.

Repair WordPress database

The steps we’ve just talked about will be enough to fix the “error establishing a database connection” in WordPress for most of you. However, it’s also possible that changing things in your wp-config.php file will bring up new errors.

Maybe now you’ll see that ”One or more database tables are unavailable,” or it might tell you that you need to repair a database. So, how do you go about that?

First, go back to your wp-config.php, as you’ll have to add a line of code there. Find the comment line that says, ”That’s all, stop editing,” and add it right before it. Here’s the line (you can copy it from here):

define(‘WP_ALLOW_REPAIR’, true);

Second, you want to go to: yoursitenameexample.com/wp-admin/maint/repair.php. You’ll see the option to ”Repair and Optimize Database,” and that’s what you want to go for.

After you’re done with it, make sure to delete that one line of code from your wp-config file. If it’s enabled, you don’t need any credentials to access the repair database page, and you don’t want anyone but yourself sneaking around there.

As you can see, things get pretty technical here. If it seems a bit too much for you, don’t be afraid to contact the pros to make sure everything is working on your site. You’ll have peace of mind since there’s no chance for you to mess something up, and that’s often priceless.

Check if your database server is down

If you’ve tried everything and nothing seems to work, the chances are that your database server (MySQL server) is down. It usually happens when there’s too much traffic on the server. It just can’t take it, so it crashes.

If you put down the effort to create your WordPress site from scratch and only let another company host it, it is always frustrating to see that it doesn’t work. However, you mustn’t lash out. Take a deep breath and call your hosting provider. If you have other sites running on the same server, check them beforehand.

If the issue is indeed in their server, you’ll know right away when you can expect it to be resolved. But if it’s not, it’s a good idea to go back to your wp-config file and check it once again. A simple typo can make you go through all this hustle over and over.

Conclusion

We hope that now you have a better understanding of how to fix the “error establishing a database connection” in WordPress. You need to think about plenty of things, so don’t rush it and don’t panic. If you feel like you’re over your head, call in the experts. Whatever the problem may be, there’s a solution for it.

Was this helpful?

1 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *