Save your changes and go to yoursite.com/wp-admin/maint/repair.php. You should see the following message.
You can click either button, but the latter is recommended to optimize your site’s performance. When the repair is finished, go back to your site and check if the error is still there.
If there’s no error, that’s great — but you’re not quite finished. You need to go back into your wp-config.php file, remove that line of code you added, and save it. If the error still appears when you visit your site, you’ll have to move on to the next step.
Step 2: Verify your database login credentials
You know you need to remember your login credentials to access your database. That’s a no brainer. But there may be incorrect details stored in your WordPress configuration file (wp-config.php) impacting your connection. This can happen when your file is not updated properly after migrating to a new hosting provider.
To verify that your information is correct, open the wp-config.php file via your cPanel and look at the following lines of code.
define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');
You can compare these values to those stored in your database to ensure they match. You can find that info by logging on to your cPanel and going to the Database section.
If the values do not match, edit the wp-config.php file and save your changes. Then navigate to yoursite.com/check.php. If you get a “connected successfully” message then your login details are now correct and the database connection error should be resolved.
Step 3: Restore default files
It’s rare but core WordPress files can be corrupted. A hacking attempt on your site or a rogue plugin may modify these files.
Whatever the reason, you can restore the default files. The process is simple, but the stakes are high. One mistake can wipe out your site. That’s why you need to back up your site before proceeding.
Now that you have a backup, download a new version of WordPress. Unzip the file, then find and delete the wp-config.php and wp-contents folder. This will ensure you don’t override your existing wp-config.php file or lose any themes, plugins, and media stored in the wp-content folder once you restore WordPress.
Next, open File Manager on your cPanel and upload your new WordPress files. This will override all the previous files (except for wp-config.php and wp-contents folder).
When the upload is complete, go back to your site to see if the error is resolved. If you’re still seeing an error message, it’s time to contact your hosting provider.
Step 4: Contact your hosting provider
If none of the steps above resolve your issue, then it could be an issue with your database server. A spike in traffic on the server, for example, could be the cause since web hosts like WordPress.com and Bluehost limit how many database connections are allowed at once.
If you let them know you’re experiencing an error establishing database connection on your site, they can let you know what the exact cause is. Best case scenario: they might even fix it for you.
There’s no foolproof method for preventing every error establishing a database connection, but there are steps you can take to resolve them. By fixing this error, you’ll ensure that visitors continue to have seamless experiences on your site.