Fixing WordPress “Error establishing a database connection” WordPress “Error establishing a database connection”

If your website is powered by the WordPress CMS, then you have experienced this error and if not, its bound to happen.

On the surface the error is straight forward, WordPress cannot connect to your database.

If you have just installed your website or playing around with your configurations then it could be as simple as the settings on your config file are wrong. Simply confirm that your database settings are the same with those on your wp-config.php file, and that should sort the issue.

But before you get there, how certain can we be that it’s not just a misconfiguration?

The best way to check that is by going to the admin section (wp-admin) of your website.  If you get the same error, then bingo, your database settings are wrong and can go ahead and verify them.

However, if you get a different error like, database

“One or more database tables are unavailable. The database may need to be repaired”. Then the above tip won’t work, you will need to repair your database.

So how do we do this?

Wordpress provides a feature of how to repair the database, but, before you use it, you will need to add this line of code to your wp-config.php file

define('WP_ALLOW_REPAIR', true);

after adding that go to

wordpress repair and click either repair option provided.

That should fix the issue and your website should be fine.

However, sometimes some tables fail to repair, you could get an error like,

“db_options: Table is marked as crashed”

To fix that, go to phpMyAdmin and repair the crushed table.

Once you have repaired the table, you can run the repair script again.

Your website should be fine now.

Don’t jump, just yet, finally remove the line of code

define('WP_ALLOW_REPAIR', true);

you added from your wp-config.php file.