In this quick article, we’ll discuss how to troubleshoot and fix the Error establishing a database connection error on a WordPress site.
The Error establishing a database connection error is one of the most common errors which WordPress users can encounter while visiting your website. It’s pretty similar to the white screen of death (WSOD) error, which displays a blank page on your WordPress website, and you’re clueless about what to do next. In this case although, it at least displays a helpful message, which allows you to debug further and fix it.
Having said that, this can be frustrating for you as a WordPress admin, if you don’t know the cause of this error, since your entire site goes down with this error. You’ll want to fix this error immediately, since it’s irritating for users to see this error with a blank page. And of course, you will lose all of your website traffic as well until you fix it.
Today, we’ll go through the possible solutions that could fix this error.
Primary Suspect: WordPress Database Details
In most cases, the reason for this error is the wrong database details in your WordPress configuration file. Since WordPress uses a database to manage your website content, it needs to connect to the database to run your site in the first place.
To connect to the database, WordPress needs the following information.
- database host name
- database username
- database password
- database name
WordPress stores all the above information in the wp-config.php configuration file, which is located in the document root of your website, so that’s the first thing which you want to check. You can download the wp-config.php file by connecting to your site with a FTP client or you can use cPanel if it is supported by your host.
Try to find the following snippet in your wp-config.php file, which you’ve just downloaded.
/** The name of the database for WordPress */ define( 'DB_NAME', 'demo_db' ); /** MySQL database username */ define( 'DB_USER', 'demo_user' ); /** MySQL database password */ define( 'DB_PASSWORD', 'demopassword' ); /** MySQL hostname */ define( 'DB_HOST', 'localhost' );
As you can see, it holds all the details that WordPress need to connect to the database. If any of these are wrong, WordPress won’t be able to connect to the database and you would end up with the Error establishing a database connection error.
Make sure that all the details are correct, and if you’re in doubt, you can always ask your hosting provider to verify it. If any of these are wrong, correct it and upload the wp-config.php file on your server. In most cases, that should fix it and you should notice that your website is working back again!
On the other hand, if you’re sure that all these details are correct in your wp-config.php file, and you’re still getting this error, move on to the next step.
Connection to Your Database Server
You’ve already checked that the database settings in your wp-config.php file are correct, and your site is still down with the Error establishing a database connection error, the next thing which you would like to check is the status of your database server.
For any reason, if your database server is down, WordPress won’t be able to connect to the database and produces this error. The most common reason behind your database server being down is the heavy traffic on your website, which can’t be handled with the limited resources on your server.
If you’re on a dedicated server and have got shell access to your server, you can try rebooting your database server to see if that helps to fix this error. On many occasions, this turns out to be one of the most effective solutions.
Finally, if you haven’t got shell access or you are not comfortable with running commands in the shell, you canto contact your hosting provider and confirm the availability of your database server. If there’s something unusual with your database server, they should probably fix it and get back to you, and that should also solve this error for you as well.
Corrupted Database
In very rare cases, a corrupted database may also produce this error. There are various reasons for your database to get corrupted so that WordPress will not be able to use it.
In this case, the best option is to restore your recent database backup and check if it does the trick. Unfortunately, if you don’t have any database backups, you can’t perform this step. So it’s always a good habit to make database backups regularly.
There are a few different ways that you could restore your database. If you’ve got shell access to your server, you could use a command to restore your database from the database dump file. On the other hand, you can use a tool like phpMyAdmin, if you’re using the MySQL database. Finally, you can always contact your hosting provider or a developer just in case if you don’t know how to restore it.
Needless to say, it’s a good idea to backup your WordPress site and database regularly. If you want to learn more about backing up your WordPress website, check out some of our other tutorials here on Envato Tuts+.
-
WordPressHow to Backup Your WordPress Site
-
WordPressChoosing the Best Free WordPress Backup Plugin
Conclusion
In this quick article, we discussed a couple of possible solutions to the Error establishing a database connection error in WordPress. We looked at how to diagnose an error in your WordPress database configuration, how to make sure WordPress can connect to your database, and what to do in case of a corrupted database.