How to Fix the Error Establishing a Database Connection in WordPress
In some cases, it could be that your database has become corrupt. This can occasionally happen (although not very often) as over time hundreds of tables are constantly added/removed by new plugins and themes. If you try to login to your WordPress site’s dashboard and are receiving the following error, it means your database is corrupt: “One or more database tables are unavailable. The database may need to be repaired.” It is important to note that you might only see this error on the back-end, whereas you see the error establishing a database connection message on the frontend.
How to fix the ‘Error Establishing a Database Connection’ in WordPress
Log into PHPMyAdmin, and you’ll see a list of databases on the left. Click the one that matches the name of the database you have in your wp-config.php file (the text value to right of DB_NAME). If you don’t see any database listed, the problem might be with the host server, not the database, because there absolutely should be a MySQL database.
When you click on your database name, you’ll a screen listing the names of the tables in the database. If in doubt, click “Browse” next to the table named wp-options, and you should find your site name and URL along with other settings. If it’s there, and the name matches your wp-config.php entry, you have the right database.
How to Fix “Error Establishing a Database Connection” in WordPress
As I mentioned, WordPress uses a specific username and password to connect to the database. Normally, those access credentials don’t change on their own. However, you can experience some weird scenarios after making any changes to your site files, the hosting environment, or anything else. In other words, even though it shouldn’t be happening, the access credentials sometimes do change. And with bad access credentials – “error establishing a database connection” happens!
How to Fix Error Establishing a Database Connection in WordPress
If you’ve carried out all the aforementioned steps, and you are still facing the error establishing a database connection in WordPress, then check this official list of DB_HOST values for various web hosting providers. Otherwise, you can contact your web hosting provider and ask them to provide you with the DB_HOST value and enter it into the wp-config.php file.