WordPress is the best blogging platform. Almost 40% of all the websites around the world runs WordPress. WordPress’s popularity doesn’t ensure its stability. Sometimes, it may break. It may break due to your fault or due to your hosting provider’s fault – anything can cause an error in WordPress.
If your hosting is not good, a simple traffic spike can crash your WordPress site. Errors can lead to lost customers, crawl errors etc. So, it’s really important to fix WordPress errors.
One of the WordPress errors is “Error Establishing A Database Connection”. This is actually more common than you might think and I will tell you all of the possible reasons for this error and its fixes.
Before I go into the root cause of this error, let me explain in brief about how WordPress works. WordPress is built using PHP and uses a database (MySQL) to store content like posts, pages etc.
The above diagram explains the working of WordPress. When you enter a website URL in the URL bar, your request is sent to that site’s web server which then requests the PHP processor (in this case we are using WordPress as the PHP program). WordPress then further proceeds to the database to fetch the URL’s contents like the text, comments etc. Media is stored in the folder called wp-content.
The error “Error Establishing A Database Connection WordPress” appears when WordPress cannot establish a connection to the database. Some of the main causes of this error are:
- Database server down
- Incorrect database info in wp-config
- Corrupted WordPress files
Now, let’s troubleshoot each of them one by one.
1) Database Server Down
At times, when the database server gets overloaded (mostly due to traffic spike), the database server will reject connections. So, WordPress won’t be able to connect to it. This mostly happens in overloaded shared hosting servers (*coughs* GoDaddy). The solution? Contact support and tell them about the problem. If they try to upsell you a more expensive plan (*coughs* GoDaddy), then you have two choices:
- Wait for a while till the database connection is restored
- Migrate out of that host (if it’s GoDaddy)
These are the two things you can do if support is unhelpful.
2) Incorrect Settings in wp-config
At times, during manual installation of WordPress, it may happen that, by mistake, you entered incorrect database information in the wp-config.php file. To check if this isn’t the cause of the problem, open your wp-config file and make sure the database name and password match your WordPress database details. Below, I have shown you how to check it in Hostinger (the method is the same for every hosting, only the method of accessing the database and the files may differ).
- Open the database control panel from your hosting panel
- Check your WordPress database username and password from the database list
- Check the DB details in wp-config. Make sure that they match with the DB details in your control panel
After checking and making necessary changes (if required), save the file. The error should now be solved.
3) Corrupted WordPress Files
This is the worst possible scenario, but, it can definitely happen. Unfortunately, there is nothing much that can be done other than trying to restore the default wp-config.php file from wordpress.org’s GitHub repository from HERE. Download it and rename it to wp-config.php. Delete the old wp-config.php from your WordPress installation. After that, enter the DB details by the steps shown above. In most cases, this is enough to solve the problem. If not, then try to restore your site from a backup. If even that fails or if you don’t have a backup (you MUST have a backup. For more info, check here – How to Backup a WordPress Site), then reinstall WordPress. That will fix your problem.
I hope that this guide has helped you to solve the annoying problem – “Error Establishing A Database Connection” in WordPress. If you have any queries or suggestions, then please let me know in the comments down below.