How to fix the Error Establishing a Database Connection in WordPress?
Error Establishing a Database Connection – As the name suggests, this error will appear if your site can’t access its database. In case you’re unfamiliar with the term, your site’s database is where all its content is stored. This includes your posts, pages, and user information. As such, if you can’t access the database, your site won’t be able to function at all.
There are a few reasons this error can occur, but most of them stem from a single file on your site, namely wp-config.php. This file contains all the information about your site’s database, so it’s the most likely origin for this type of issue.
How to fix the Error Establishing a Database Connection
To repair the database connection, access your site’s wp-config.php file, this should be located in its root folder.
Right-click on the file, and select View/Edit. You’ll first want to check that the credentials in the file are correct. To do that, you should access your phpMyAdmin to find the exact details. If the hostname, username, password, and/or database name in the file are incorrect, replacing them should fix the error.
However, if the error persists, you may need to activate WordPress’ automatic database optimization tool. This feature can be used to repair corrupted databases, and can be turned on by adding the following line to your wp-config.php file:
define( ‘WP_ALLOW_REPAIR’, true );
After saving the file and uploading it back to your server, you can run the new script by navigating to https://yourwebsiteurl.com/wp-admin/maint/repair.php in your browser. The resulting page will look like this:
All you need to do is click on either of the buttons to run the repair tool. When the process is completed, the page will reload, and you should see a message telling you that the database tables have been fixed.
This should deal with your database connection error. Just don’t forget to remove the line WP_ALLOW_REPAIR from wp-config.php when you’re done.
That was all from How to fix the Error Establishing a Database Connection in WordPress.