Share this Post
When working with any website online, even WordPress, it can be very frustrating to deal with random errors that pop up.
Probably one of the most annoying ones is getting an error "Establishing a Database connection" when to connecting to the WP database.
Thankfully, it’s a relatively simple issue to fix, so let’s get right to it!
Why Does This Error Occur?
To understand why this issue comes about, you first have to understand that most databases are generally built around database languages, in a lot of cases, this is a combination of PHP and MySQL.
This is actually the case when it comes to WordPress, so it’s important to note this bit of information.
PHP is more specifically a programing language, whereas MySQL is a database management system that handles the content of your website. MySQL is actually incredibly important since it stores and manages everything such as text, images, the pages themselves, any theming you’ve done, even colors and layouts.
So MySQL is pretty important, and the way that it’s accessed and edited is through PHP. Ergo, two very important things that tend to go hand in hand and the foundation of how WordPress is built.
What Causes ‘Establishing a Database Connection’ Error?
Generally speaking, there are four main ways that this issue might pop up:
While not something that happens often, there is a possibility that your database is corrupted. Unfortunately, there’s not much you can do except doing a restore, which you hopefully have in some form of top cloud storage, or through the host that you’re using.
Bad Login Details
This one is probably the most likely scenario. Not only in terms of us just putting in passwords incorrectly, which happen all the time, but the login credentials might have changed.
Another possibility is that the login credentials for the database are different than the ones you use to log into the WordPress admin, which can sometimes cause a bit of confusion.
Database Server is Down
If the database server itself is down, then of course you can’t log in. Generally speaking, when it comes to websites, they’re run on two servers: a web server that stores all the content, and a database server that has the database that manages that content.
One can be down and not the other.
Since WordPress functions this way, the site can work but you won’t really be able to do any content management, which is a problem.
If too many people are trying to access the database, this can cause an overload where very few or nobody can. Imagine it sort of like a high-way on-ramp: if everybody tries to force their way on it, then it will cause a blockage and nobody ends up getting on the highway.
This can be both a good and bad thing because lots of people accessing the database can mean a popular page or lots of work needing to be done. On the other hand, it’s a problem because people can’t actually reach it and you’ll probably have to increase capacity or processing power.
If you’ve got a good WordPress host they should automatically handle this for you.
How To Fix ‘Establishing A Database Connection’ Error
The way you fix this issue actually mirrors the type of errors that you can possibly get.
Talk to Your Host
The very first thing you should do is contact your host and see if they can help you. They will have not only the technical expertise but also the know-how to actually fix the issues you might be having.
More importantly, if they’re hosting a blogging platform, then more likely than not, they are responsible for making sure your database is always up.
Failing that, if your host can’t fix it directly, then maybe then can either help you or at the very least point you in the right direction. This is especially useful if you don’t have much technical know-how, such as if you’re just starting out with PHP and MySQL.
So if your database is not doing really well, either because there are some corrupted files, or the database is down, then you need to fix that first.
If you’re using a web host like Hostgator, you can log in to your admin panel and find a ‘repair database’ button through there. Usually, this will be under ‘MySQL Databases’ and the link will say ‘Repair DB’.
Once you click on that, your host will go through the motions of trying to repair any issues that arise.
Restoring From a Backup
Unfortunately, if fixing the database won’t work, you’ll likely have to restore it from a backup. This again depends widely on the nature of your database hosting.
Some hosts do backups automatically, some charge you for it, and yet others will leave you to figure it out on your own.
If your host is handling it then great! Usually, you can either do a request for a restore, or there will be a restore link for you to click on the admin panel.
If it’s a manual backup, on the other hand, it requires a bit more effort, and you need to get into the guts of the database. Mostly this just means logging in, deleting the old database, and copying over the new one.
Keep in mind that restoring your database from a backup will have you lose anything you’ve done for that backup, so make sure you keep your backup up to date!
Again, this is one where you’ll need to get back to your host, but thankfully this one doesn’t require their input as much. Instead, you’ll want to upgrade your plan to one that can handle the new capacity.
Usually, this will mean getting a dedicated or VPS hosting that has better hardware for processing power. Most WordPress hosts tend to manage their own load-balancing when it comes to the bandwidth, so you shouldn’t have to worry about that.
That last point is important to keep in mind, if the traffic issue is just temporary, it’s not really worth it to spend money on upgrading your plan. You’re better off waiting for it to be over. If you do get traffic issues regularly, that’s when it’s certainly worth upgrading.
Getting the error "Establishing A Database Connection" can be frustrating, especially because it cuts you off from access to your database or your website. Even so, if you follow good practices with backups and database maintenance, the solutions should be relatively easy.
Even better, if you pick a good dedicated hosting, you can rely on the host themselves to deal with database issues (assuming that’s part of the plan or that you’ve paid for it. It’s a bit more expensive, but it’s worth not having to directly deal with these types of errors.