How to Fix the Error Establishing a Database Connection in WordPress

If your wordpress site was working fine and out of nowhere you started seeing the following error:

Error establishing a database connection

Then this is the best and easiest way to fix it:

Add the following line in your wp-config.php file. Add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

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

Repair database in WordPress

Remember, the user does not need to be logged in to access this functionality when this define is set. This is because its main intent is to repair a corrupted database, Users can often not login when the database is corrupt. So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.