This article helps you to understand how to fix Error establishing a database connection.
The WordPress software is written using PHP and MySQL. Why two languages? Because all the information that makes up your website is stored in a MySQL database, and WordPress uses PHP to store and retrieve information from that database.
Generally, whenever a page is loaded on your website, it’s built on-the-fly. PHP code is used to access your MySQL database and retrieve every bit of information needed to create the page. There’s a PHP query to access the post title, another to grab the author name, another to retrieve the publication date, and so on.
“Error establishing a database connection.” This means, the PHP code was unable to connect with your MySQL database to retrieve the information it needs to build that page. That’s why this error is often all you see on an otherwise blank page. If you can’t connect to your database, there’s no way to know what to display; no information about your website is available
How to fix Error establishing a database connection
1. Determine where the error is occurring
Can you see your website normally, but get the error when trying to access wp-admin? Or maybe your website is down, but you’re getting a slightly different error when you try to login: “One or more database tables are unavailable. The database may need to be repaired.”
These are signs of database corruption.
WordPress has a built-in way to attempt to repair your database, but you’ll have to enable this feature first. To do so, you’ll need to access your wp-config.php file, which contains your WordPress installation settings and configuration.
You can find wp-config in the your root WordPress file directory. You can access it by logging into your cPanel, selecting File Manager, and navigating to the folder where you installed WordPress.
2. Check your database login settings in wp-config
If the above step didn’t work or didn’t apply to you, then the next thing you can do is take a look at your database settings in your wp-config file.
Wondering how your database login credentials could have suddenly stopped working? Things like switching hosting companies or changing anything about your database name or user information could cause the error if your wp-config file wasn’t updated to the new information.
You may still have the wp-config file open from the last step. If not, log into your host’s cPanel and open the File Manager. Navigate to the directory where you installed WordPress, and look for a file called wp-config.php. Go ahead and open that for editing.
3. Still getting the error message? Check with your hosting company
If you’ve followed the above troubleshooting steps and your website is still down, it could be a problem on your host’s end. Contact your web hosting company’s support and give them all the details of the error and the troubleshooting actions you’ve taken, and they may be able to help you determine the cause of the error.