How to Fix the Error Establishing a Database Connection in WordPress
If you get an error message “Error Establishing Database Connection,” this means that your website is unable to connect to the database. In fact, the error establishing a database connection is quite common and there could be a number of reasons for this.
Let’s take a look at how to fix error establishing database connection issue in WordPress with step by step troubleshooting.
1. Get In Touch With Your Web Host Provider
You can contact your hosting provider and ask
- if the database server is up and running with no problems, and
- if there isn’t any spike in activity or traffic on the server right now.
In most cases, if you tell them what sort of error you’re having, they will either let you know what the cause is exactly, or they will even fix it for you. If the fault is not on their end (the server is operational and there are no spikes in traffic), and they don’t want to help you, proceed to the next step.
2. Check Your WordPress Database Credentials
Usually this occurs when a user has entered or modified their database credentials (database name, database username, and database password) incorrectly. WordPress database credentials are stored in the wp-config.php file. Near the top of the file, you should see the line that begins
define( 'DB_NAME', 'some_db' );
You need to make sure that the information for the database credentials (database name, database username, and database password) is correct. If you can still see the error, then this means that something else is wrong. Continue reading for more troubleshooting steps.
3. Check Your WordPress Database Host Information
If you are confident that your database name, username, and password information is correct, then you may want to make sure that you are using the correct database host information.
define( 'DB_HOST', 'localhost' );
The hostname is usually localhost, which indicates that the database is running on the same server as your WordPress. However, some managed WordPress hosting companies use separate servers to host databases. In that case, your database host information will not be localhost.
At this stage, you can go back to your site and see if the error has been fixed.
4. Repair Your WordPress Database
You can do this by adding the following line in your wp-config.php file. Make sure to 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, Go to http://www.yoursite.com/wp-admin/maint/repair.php
This is what you should see:
Click either of the buttons. Once the automatic repair finishes, go back to your site and see if the error is still there.
Note: Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php.