How To Fix The “Error Establishing A Database Connection” Error In 2022
Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our websiteContact us for Questions
You just spent days, or possibly weeks, developing your WordPress website.
Now, it’s out in the world for all to see. You’re getting traffic, generating leads, and hopefully, getting a few clicks.
Then, one day, you see that fatal white screen informing you of an error establishing a database connection.
It may not be the White Screen of Death, but it’s pretty close to it.
But don’t panic just yet.
Fortunately, there are several things you can try to get your WordPress site back online.
So, if you’re ready to try your hand at a bit of troubleshooting, you’ve come to the right place.
Let’s dig in.
Table Of Contents
What Is the “Error Establishing a Database Connection” in WordPress?
An error establishing a database connection is just that—an error connecting to WordPress’ database.
When that happens, your entire website becomes inaccessible to your users.
Not only that, but you won’t be able to access your WordPress dashboard, either.
So now, the front end of your WordPress site is unavailable to your users, and you might not be able to get into your account to fix it.
Many web hosts give you a bit of a buffer to fix this issue.
Some web host providers run websites from the cache.
If your site’s cached, the server can continue to run it as long as it’s in the cache.
For example, Kinsta WordPress sites are cached for one hour by default.
This gives you a one-hour buffer to find and address the issue.
Bear in mind that not all problems can be fixed in one hour.
However, sometimes the error is a simple fix, so site monitoring is essential.
Why Does WordPress Need a Database?
WordPress requires two things to allow your website to function: a programming language and a database.
The programming language is PHP, and the database is MySQL.
Your site’s content, including widgets, settings, content, and metadata, is stored on WordPress’s MySQL server.
When a user visits your site, WordPress reaches out to the database, retrieves all of your site’s information, and displays it to the user.
Other Common Errors in WordPress:
What Causes “Error Establishing a Database Connection”?
If WordPress can’t connect to its database host for some reason, all you and the user will see is a white screen with an error message.
Your site’s content will be absent because WordPress can’t access any of it.
There are a few things that can cause errors accessing the database.
For example, your login information could be incorrect, there could be an issue with the server itself, or you might have corrupt files on your database or WordPress.
In other words, the issue could be on your end, or it could be the database server itself.
We’ll get into each of the ways you can troubleshoot in the next section.
5 Ways To Fix the Error Establishing a Database Connection in WordPress
Unfortunately, fixing the WordPress database connection error might be out of your hands in some cases.
If your web hosting provider or WordPress is down for whatever reason, you’ll need to wait until they resolve the problem on the back end before you can attempt any fixes on your end.
The good thing about it being out of your hands is that you won’t have to spend hours fixing the problem.
However, you can try a few things if you know the issue is on your end.
Before You Start
There are two things you should do before you start messing with your WordPress site or your credentials.
First, make a backup of your site if you haven’t already.
A best practice is to back up your WordPress site each time you make a change.
If you haven’t gotten into that habit, now is a great time to do so.
Second, reach out to WordPress before you touch anything.
A customer support representative will be able to tell you if the database connection message is due to difficulties on WordPress’ side.
If WordPress has assured you the problem is due to something on your end, you can try the following fixes.
1. Check Your Database Login Credentials
The first thing to try is checking your WordPress database login credentials.
It’s rare to have an actual database error, so the most likely cause is an incorrect database username or password.
A forgotten username or password is especially common after users switch to a new host.
Try to remember if you recently changed your username or password.
If so, that’s probably the cause of the database connection error.
If you’re a new user, it’s more likely the problem is incorrect database credentials.
However, if the “error establishing a database connection” message isn’t a result of an incorrect database username or password, you can move on to the following troubleshooting method.
Where Can I Find My Database Credentials?
Your database login credentials are in your wp.config.php file in your WordPress site’s root folder.
You’ll find the wp.config file in your Bluehost cPanel account in the file manager.
Right-click on the file and select “Edit.”
Here, you’ll find the following pieces of information about your MySQL database:
- Database name
- Database username
- Database password
- Host name
It’ll look something like this:
<!-- wp:paragraph --> <p>( 'DB_NAME', 'database_name_here' );</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>( 'DB_USER', 'username_here' );</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>( 'DB_PASSWORD', 'password_here' );</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>( 'DB_HOST', 'localhost' );</p> <!-- /wp:paragraph -->
Look over all of the information and make sure it’s correct.
Then, make the necessary changes to your MySQL database information.
2. Repairing Corrupt Database
If your database login credentials were accurate, your next step to fixing a WordPress error establishing a database connection is to see if the problem is a corrupt database.
A WordPress site has many moving parts, most of which are in the WordPress database.
When one of those moving parts goes a bit off the rails, you might run into an issue.
First, head to your WordPress dashboard at yoursite.com/wp-admin.
If you see an error that says, “one or more database tables are unavailable.
The database may need to be repaired,” then the issue is a corrupt database.
If you see the “one or more database tables” error, you’ll need to head back to your wp-config file and do the following:
- Right-click on the file and click “Edit.”
- Scroll to the bottom and add define(‘WP_ALLOW_REPAIR’, true); to the bottom of your file.
- Upload the file back to your wp-config folder.
- Go to yoursite.com/wp-admin/maint/repair.php and click either of the buttons.
- Once WordPress runs the automatic repair, check if the error is still present.
If the error is gone, return to your wp-config file and remove the line of text you just added.
However, if the error is still there, you can move on to the next potential fix.
3. Fixing Corrupt Files
Anyone who’s used a computer has likely come across corrupted files.
File corruption usually happens when files are transferred from one place to another.
However, a hacker accessing your files could have the same outcome.
Fixing corrupt files will require doing a new WordPress installation.
- Download a new copy of WordPress from WordPress.org.
- Unzip the file.
- Delete the wp-content folder and wp-config-sample.php file.
- Upload the rest of the files to your site.
This upload will overwrite all the files currently on your site.
Ideally, it’ll replace any corrupt files and fix the issue.
4. Issues With Your Database Server
After going through the previous three troubleshooting attempts, your next step is to see if there are issues with your database server.
Your database server is the machine your web host uses to run your database.
Server issues could arise due to too many connections or attempted connections, especially if you’re using a shared host.
Reach out to your host’s customer support team to see if they can give you any information.
5. Restore Latest Backup
Finally, you can restore your site using your latest backup.
This is a quick way to remedy the problem, as long as you aren’t worried about losing any data between your last backup and now.
To restore your site from a WordPress file backup, you’ll need to access Jetpack via your WordPress dashboard.
- Go to the Jetpack menu and select “Backup.”
- Choose the backup you’d like to use.
- Click “Restore.”
Follow the prompts to confirm which parts you want to restore.
Just be aware that a full backup restore will override every WordPress file on your site.
How To Avoid This Error in the Future
You can do a few things to avoid the error establishing database connection issue in the future.
- Write down or save your username and database password someplace safe.
- Always have a backup of your WordPress site files.
- When you migrate your files, confirm none of them are corrupt before relaunching your site.
It can be a bit nerve-wracking when your site goes offline, especially if it’s a WordPress database error and not one on your end.
Fortunately, the fixes above should remedy the “error establishing a database connection” issue.
Contact your CMS or web host provider’s support team to get expert help if you still need guidance.