You’re trying to access your website, which was running smoothly the last time you visited, but instead of the homepage, you’re faced with the WordPress error message “Error Establishing a Database Connection“.
The database connection error can cause goosebumps to the best of us but guess what? This error is entirely manageable and is one of the common WordPress errors. And I will show you how to fix the ‘error establishing a database connection’ in 6 different ways.
So, don’t sweat and keep reading.
What is a WordPress Database?
Before you troubleshoot the database error, you should know what the heck the word database means and why it is so important. Well, WordPress utilizes the MySQL database for storing all information related to your website. This includes information about your login credentials, users, plugins, posts, and pages.
The media files and other uploads are not stored in the database but are stored in the wp-content folder separately.
WordPress retrieves all the information it needs from the MySQL server constantly. If there is an error in your database, WordPress will not be able to communicate with the MySQL server and will throw up a database connection error that we will troubleshoot below.
1. Check your database credentials
The most common reason why the database connection error occurs is due to wrong credentials in the wp-config.php file.
This file is located in your root WordPress installation. You can use either an FTP program like FileZilla or a file manager in your hosting panel to access your WordPress directory.
Next, navigate to the directory where WordPress installed. If you can see the folders wp-content, wp-includes, and wp-admin, you are in the right place.
Now scroll down to the wp-config.php file. Before tinkering with this file, you should backup all its contents.
So, open the file and copy all the contents of the file. Now create a text file with some name like wp-config.txt on your computer and paste the copied contents. Save the text file.
You can now return to editing the wp-config.php file.
The wp-config.php file contains the details of your WordPress database. If there is any error here, it can throw up the database connection error.
There are 4 important things to verify here:
- Database name
Look for the line:
The ‘Name’ should match with your database name. To know your database name, you can either ask your hosting provider or you can access your MySQL database using your hosting panel.
Once you log in to the MySQL panel, you can see your database name on the left panel just below the information_schema.
Alternatively, you can click the Databases tab on the top to know your database name.
Ensure that the name matches the one in the wp-config file. If not, make the correction and save the file.
- Database username and password
The database username and password in wp-config.php should also tally with the actual username and password.
If you’re using cPanel then the database username will be listed under the Databases tab. If you’re using a custom panel like Dreamhost (which we are), then the database details like username and password will be listed under the Manage Database tab.
In case of any doubt, just ask your hosting provider for the database username and password.
Now, ensure that the username and password match with those in the following two entries in the wp-config.php file.
If they match, there’s nothing to do here. But if they don’t you should update the entries with the correct username and password.
- Database hostname
Finally, check this line in the wp-config file:
This defines the MySQL (database) server location in the wp-config file. Most of the time, your database server will be the same as the one on which WordPress is installed. In this case, the hostname will correspond to ‘localhost’.
But if your database is located on a separate server, as in the case of Dreamhost, then the proper host location should be present like mysql.yoursite.com.
When in doubt, ask your hosting support for the correct database host entry.
2. Database connection error caused by plugins
In any type of WordPress error, a malfunctioning plugin or theme is the usual suspect.
To find out whether the error is caused by a plugin, log in to your web hosting panel and use the file manager to go to your WordPress directory. You can also use an FTP program like Filezilla for this if you don’t have access to a file manager.
In the WordPress directory, go to the wp-content folder. Within this, there is a ‘plugins’ folder. Rename this folder to any other name like plugins.hold or plugins-old.
This will deactivate all plugins on your website. Check your site; if the database connection error has disappeared, it means that one of the plugins was causing the error.
To identify the misconfigured plugin, log in to your website and go to the plugins dashboard. You will see an error message that ‘plugins have been disabled since the plugins folder does not exist’.
Don’t panic. Just restore the plugins folder to its original name i.e. ‘plugins’ in the wp-contents folder.
Next, head back to the plugins dashboard. You will see that all plugins are in a deactivated state. Now activate all plugins one by one.
If the database connection error recurs when you activate a plugin, means that the plugin was causing the error. You should deactivate the plugin to resolve the error.
However, if renaming the plugins folder did resolve the database connection error, you should follow the next step after restoring the name of the plugins folder in your WordPress installation.
3. Error connecting to database server
Sometimes, due to a sudden spike in website traffic, your database server is unable to handle the load, leading to a connection error. This situation is more likely to occur on shared hosting as compared to VPS or dedicated hosting.
In order to verify the connection between the database server and WordPress, you need to do the following.
- If you have another website hosted on the same server then simply visit that site. If it connects, it implies that the database connection is fine.
- If you have a single website, then first log in to phpMyAdmin and then connect to your database by selecting it. If the database tables show up, then the connection is OK.
If for any reason, the connection fails then you should your hosting support and ask them to resolve the error ASAP.
4. Error in the database
If there is some error in the database, it could also cause the database connection error. In this case, you will need to repair your database. Fortunately, this is quite easy to accomplish.
Again, open the wp-config.php file for editing as described in step 1.
Then, add the following line of code after this line:
What this code does is allows you to access the database repair options on your site. This option can be accessed by visiting the following link:
Here, you will see the following two options.
Click on the ‘Repair Database’ button to repair the database. Once you have done this, delete the code you added in the wp-config.php file.
Now check whether the ‘error connecting to WordPress database’ message has vanished. If it has, your problem is solved. Otherwise, move to the next solution.
5. Corrupted WordPress files
Sometimes, adding or deleting files can cause some core WordPress files to become corrupted. These files which are located in the wp-admin and wp-includes folders are vital for the smooth working of your website.
To rule out the possibility of corrupted files, you will need to replace the wp-admin and wp-includes folders in your WordPress installation.
Before, you proceed further I highly recommend that you take a full backup of your website using a free backup plugin like UpdraftPlus.
Now, you should download a fresh copy of WordPress from here.
Next, unzip the files to your computer. In the unzipped files, you will see three folders: wp-admin, wp-includes, and wp-content. Delete the wp-content folder since we don’t need to upload it.
Next, upload the wp-admin and wp-includes folder to your WordPress installation using an FTP program like FileZilla.
You can also use your File Manager to upload the folders. When the ‘overwrite files’ message pops up, allow overwriting the existing files in these folders.
Once all the files have been replaced, visit your website. Hopefully, you will no longer see the database connection error message.
6. When everything fails
I have described five solutions to the WordPress database connection error. Hopefully, one of them worked for you.
In the unlikely event that none of the above methods clicked, you should contact your hosting support forthwith.
After all, every minute of downtime will cost your readers and possible revenue loss.
Describe your problem in detail and any plugin or theme that you might have installed before the error occurred. Managed web hosting companies like Cloudways have dedicated WordPress support and should resolve such errors easily.
You can also check out our reliable WordPress troubleshooting service to get expert help with this nagging problem.
Did you manage to fix the database connection error? If yes, which solution worked for you? Please let us know in the comments below.
Download the WordPress SEO eBook
Go from WordPress SEO Zero to Hero in no time. Also receive 2 Bonus PDFs with this free eBook.
Give it a try. You will thank yourself later.