On your WordPress site, if you receive the “Error establishing a database connection” on both the front-end of the site, and the back-end of the site (wp-admin). Then you may need to repair your database.
You can do this by adding the following line in your wp-config.php file:
define('WP_ALLOW_REPAIR', true);
Once you have done that, you can see the settings by visiting this page: http://www.YOURSITE.com/wp-admin/maint/repair.php
You do not need to be logged in to your site for this to work. This is because the main intent is to repair the corrupted database, users can not login when the database is corrupt. So once you finish repairing/optimizing your database, make sure to remove the code from your wp-config.php file.