Error establishing a database connection is a common one in WordPress. You just see the white screen with the inscription stating that this error has occurred and this is a bit frustrating, right? Let’s review the reasons for this error to appear and disclose several common ways of fixing it.
As you probably know, all core WordPress files are written with a PHP programming language and all content of the WordPress website is stored in the MySQL database.
WordPress uses PHP commands to connect to the database and then display the captured information to the user. If WordPress can’t access the information in the database and isn’t able to display it on the screen, then the error establishing database connection occurs.
There are a few reasons why WordPress is sometimes unable to connect to the database:
1. Contact your hosting provider and ask for help
If your site has been always working well and suddenly it shows the error we are talking about, then the problem is probably at your host’s end. Just contact your hosting provider through the live chat or by email and explain your problem.
Ask your host two questions:
If there are no problems with your server and traffic then ask them what the other reason for the error establishing database connection you are experiencing on your website is. They will find the real trouble with your website and probably even fix it for you.
2. Check your plugins and a theme
If the problem has occurred with your theme or plugin, you can make the same steps as indicated in this post. Here you’ll find the ways of fixing another common error in WordPress which is called ‘the white screen of death’.
You can also connect to your website through FTP and try to rename the ‘plugins’ folder and then go back to your website to check if it works. If the error still occurs after the renaming, then the trouble is with something else and you can rename the folder back to ‘plugins’.
Do the same with the ‘themes’ folder and check if it works or not.
3. Open your website’s wp-admin URL to see the suggestions
To make sure your database hasn’t been corrupted, first, go to ‘yoursite.com/wp-admin’ and see what the page shows. If you see the same error displayed, then skip this step.
If you go to ‘yoursite.com/wp-admin’ and see another text displayed on the page, see what is suggested there. Sometimes you can find the solutions for your problem and some specific questions which will help you learn possible reasons that might have caused the error establishing database connection to occur.
See the snapshot:
4. Make changes to the wp-config.php file
Sometimes when you go to ‘yoursite.com/wp-admin’ you may see the message stating ‘One or more database tables are unavailable […]’.
In this case, you should make some changes to your wp-config.php file.
Connect to your website through FTP and download a wp-config.php file. After that, insert the following line into the file right before the “That’s all, stop editing” phrase:
Now upload the file back to your website through the FTP.
Now go to yoursite.com/wp-admin/maint/repair.php
And you’ll see the following message:
Select the solution and click the button you prefer. Now the automatic database repair should be started. When it is finished, go to your site to check if the error still occurs.
Even if the problem is solved, open the wp-config.php file and remove the line you have just added. Save the changes and go to your site to see if it works now. If the error is still there, try the next step.
5. Check the database connection credentials (login and password)
Usually, the login and password for the database don’t change by themselves, but sometimes if you update your website, migrate to another host or make any other changes, you can face some unusual scenarios. In this case, your database login and password can be changed, so you are going to see the error establishing database connection that should be fixed.
Download your wp-config.php file just as well as I’ve described in the previous section and add the correct credentials into it.
If you have the doubt as for the credentials, copy them from your wp-config.php file and send them to your host through the chat or by email and ask them if your credentials are right. If they are not, ask them to provide you the correct credentials and insert them into your wp-config.php file. Save your changes and upload the file back to your server.
6. Restore the default files of WordPress
If nothing has helped, upload the new version of WordPress and delete the ‘wp-content’ folder from the newly downloaded archive not to replace your current content with the default one and not to lose all of your data.
After that, install a new WordPress to your host instead of the old one through FTP. Go to the main WordPress root and upload your new files to overwrite all the previous ones.
Please see this post to learn how to install WordPress manually if required.
If you want to keep your website safe and want to be confident as for your website’s security, use the backup plugins to make automatic backups and be always able to restore the initial look of your website if any problem occurs.
Of course, you should also use the quality hosting for your website. Please see this list of hosting providers we offer.
Hope this small guide was helpful and wish you to avoid any troubles with your website in the future.
Hi! I’m an experienced writer exploring WordPress for more than 6 years. I’m happy to share my knowledge and ideas with you and I hope you join me.