Error Establishing a Database Connection in WordPress

Error2
If you have been browsing the web for a while, you have seen this error a few times. Error Establishing a Database Connection is one of those errors that could be due to different causes. As a WordPress beginner, this could be awfully annoying exclusively when it occurred on its own without you modifying anything. We ran into this issue last night on our own website. It took over 30 minutes to identify and fix the issue.
Previously we have fixed the errors like WordPress posts returning 404 Error and WordPress: White Screen of Death .
In this post, we will explain to you those steps which we have take to fix the error establishing a database connection in WordPress.
The error message is clear that your website is unable to connect to the database.This error can occur due to different reasons.
Wrong Connection Parameters:
The most common reason, having wrong database credentials will cause the error. These credentials are in the wp-config.php file placed in the main directory for your WordPress. There are three credentials: data source name, database user, and database security password.Make sure these credentials match the details located in your cPanel.

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

If you know the current database username and password, you can reset it. If you do not know it, you will not be able to check the current password, so you will need to remove and then recreate the database user, by giving the same password in the wp-config.php file. If this was the issue, then the error should be fixed and the website will be good.
Database Corrupted:
If you have verified the credentials are ok and still getting the error, the problem could be due to corrupted database tables. Database tabels can be checked and repaired from cPanel. Follow the instruction in our article How to repair crashed tables through phpMyAdmin .
Too Many Concurrent Database Connections:
This error can also occur if there are too many connections to your database at once,In this case you can handle it by enabling cache plugins on your websites.This will limit the overall database connection on your WordPress site.
In our case issue was with database credentials, we have reset them and website start working.Hope these solutions will also work for you.