How To Fix The Error Establishing A Database Connection
If you use WordPress on your website it's likely you've ran across the Error Establishing A Database Connection:
This is one of those errors that can be extremely frustrating as it brings your entire website offline.
First, it's important as to why you're seeing this error in the first place.
There are two parts to any WordPress installation:
The Core Files
The mySQL Database
All WordPress installations share the common core files and then are customized when adding themes, plugins, and media such as images.
All of the customizations and content displayed by your WordPress are stored inside the database. This is what makes your site unique.
When you're seeing Error Establishing A Database Connection, this means your WordPress files are unable to successfully connect to your database.
The first step to trouble shooting this, is to locate your wp-config.php file and see what database your install is trying to connect to.
If you're hosting with us, you have access to cPanel, which you can use file manager to locate:
cPanel -> File Manager -> Root Folder Of WordPress (a lot of times public_html)
You can click on edit inside file manager and you'll notice:
This tells us that the database WordPress is attempting to connect to is keydiets_wp.
Next, you want to make sure the database actually exists inside your cPanel.
You can find this in cPanel -> MySQL® Databases:
If you don't see your database listed in this table, you need to find out where it is.
Was it accidentally removed? Do you have a backup? Did you recently move web hosts?
This is why I'm constantly reminding people to keep good backups. If nothing else, sign-up for a free account at ManageWP and have them do an automatic backup once a month.
As long as your account isn't larger than 100,000 Inodes or 20GB of disk space, we automatically take a full backup of your account each night, but if the database was removed prior to this backup, it's likely it's also missing. Another reason why it's very important to keep your own backups.
Once you've located the database, you want to double check the database name inside wp-config.php to make sure it's the same database name as the one in your cPanel.
If it is, you then want to take a look at the database user:
You'll notice in my screenshot above this user is also in the Privileged Users column. If it's not, you should assign the database user to the database.
You can do this by going to cPanel -> MySQL® Databases:
The user should have full privileges to the database (defined once you click the Add button):
If all of the above is true, you should take a look at the database user's password inside your wp-config.php file:
It's likely the database user's password got reset, so you can easily change this inside of cPanel -> MySQL® Databases -> Change Password (beside the correct user under Current Users):
I always recommend using the Password Generator so the strength is Very Strong 100/100. If it's any lower than this, your entire WordPress installation could be at risk.
If changing the database user's password, you must update it inside your wp-config.php file:
If you've done all of the above and you're still getting the error establishing a database connection, it's important to check the mySQL Hostname inside your wp-config.php file, especially if you're moving from an old web host:
At NameHero this is always localhost unless you're running a VPS cluster with your WordPress files on one server and your database on another.
I've filmed a video tutorial where I walk you through these troubleshooting tips in order so you can also follow along:
If you go through all of these steps but still have the error, please feel free to reach out to our Superhero Support Team so we can dive even deeper into your account and see where the conflict is.
If you're running a custom VPS setup, our engineers will be more-than-willing to help ensure you have the proper configurations.
This is one of those errors that can be extremely frustrating as it brings your entire website offline.
Why You Get The Error Establishing A Database Connection
First, it's important as to why you're seeing this error in the first place.
There are two parts to any WordPress installation:
The Core Files
The mySQL Database
All WordPress installations share the common core files and then are customized when adding themes, plugins, and media such as images.
All of the customizations and content displayed by your WordPress are stored inside the database. This is what makes your site unique.
When you're seeing Error Establishing A Database Connection, this means your WordPress files are unable to successfully connect to your database.
Find Your WordPress Database Name
The first step to trouble shooting this, is to locate your wp-config.php file and see what database your install is trying to connect to.
If you're hosting with us, you have access to cPanel, which you can use file manager to locate:
cPanel -> File Manager -> Root Folder Of WordPress (a lot of times public_html)
You can click on edit inside file manager and you'll notice:
This tells us that the database WordPress is attempting to connect to is keydiets_wp.
Check That The WordPress Database Exists
Next, you want to make sure the database actually exists inside your cPanel.
You can find this in cPanel -> MySQL® Databases:
If you don't see your database listed in this table, you need to find out where it is.
Was it accidentally removed? Do you have a backup? Did you recently move web hosts?
This is why I'm constantly reminding people to keep good backups. If nothing else, sign-up for a free account at ManageWP and have them do an automatic backup once a month.
As long as your account isn't larger than 100,000 Inodes or 20GB of disk space, we automatically take a full backup of your account each night, but if the database was removed prior to this backup, it's likely it's also missing. Another reason why it's very important to keep your own backups.
Check Your wp-config.php Values
Once you've located the database, you want to double check the database name inside wp-config.php to make sure it's the same database name as the one in your cPanel.
If it is, you then want to take a look at the database user:
Make Sure The DB User Has Privileges:
You'll notice in my screenshot above this user is also in the Privileged Users column. If it's not, you should assign the database user to the database.
You can do this by going to cPanel -> MySQL® Databases:
The user should have full privileges to the database (defined once you click the Add button):
Check DB User Password
If all of the above is true, you should take a look at the database user's password inside your wp-config.php file:
It's likely the database user's password got reset, so you can easily change this inside of cPanel -> MySQL® Databases -> Change Password (beside the correct user under Current Users):
I always recommend using the Password Generator so the strength is Very Strong 100/100. If it's any lower than this, your entire WordPress installation could be at risk.
If changing the database user's password, you must update it inside your wp-config.php file:
Check mySQL Hostname
If you've done all of the above and you're still getting the error establishing a database connection, it's important to check the mySQL Hostname inside your wp-config.php file, especially if you're moving from an old web host:
At NameHero this is always localhost unless you're running a VPS cluster with your WordPress files on one server and your database on another.
How To Fix Error Establishing Database
I've filmed a video tutorial where I walk you through these troubleshooting tips in order so you can also follow along:
If you go through all of these steps but still have the error, please feel free to reach out to our Superhero Support Team so we can dive even deeper into your account and see where the conflict is.
If you're running a custom VPS setup, our engineers will be more-than-willing to help ensure you have the proper configurations.
Updated on: 10/10/2024
Thank you!