Error Establishing a Database Connection – How to Fix it?
WordPress users often face errors while working on their websites or blogs. Errors are very annoying, no matter how they occur. The “Error establishing a database connection” is one of the most common and perhaps the most annoying WordPress errors. Have you ever faced this WordPress error? Were you able to fix that? If you have not received an error like this one, this does not mean you will never receive it. It is very normal to receive errors while working on WordPress. So don’t be surprised if you ever receive an error establishing a database connection. In this article, we are going to discuss this common WordPress error message in detail. We will also learn how to fix error establishing a database connection.
Error Establishing a Database Connection
Do you know what causes the WordPress error establishing a database connection? Whenever you receive this WordPress error, your website will go down because it fails to communicate or it has no longer access to your WordPress database. WordPress stores all your site’s information in your MySQL database. The site’s information includes post data, page data, meta information, login information, plugin settings, etc. The MySQL database does not store data like media content and core files, such as index.php, wp-login.php, etc. So whenever people visit your website, PHP executes code on the webpage and queries the information from the MySQL database. And then your website gets displayed on the web browser of the visitor. But if your MySQL database fails to work properly due to any reason, you will receive an error message saying – Error establishing a database connection.
When this WordPress error appears, the home page of your website will display no information because it fails to retrieve any data to show that on the page. And the reason is that the connection is not working properly. This will break the frontend of your WordPress site. Besides, you will not be able to access your WordPress dashboard. Sometimes it may happen that your visitors do not see this error. And this happens just because your website is most likely using the cache. This a good approach to improve your site’s user experience. For example, if your site is cached for one hour, a day, or even a week, it will appear fine to your visitors even if there is any issue going on.
How to Fix Error Establishing a Database Connection
The WordPress error establishing a database connection should never be taken lightly because this may affect your website’s traffic, sales, and analytics. So you should try to resolve this WordPress error as soon as it appears on your screen. If you are a WordPress beginner, it may not be easy for you to resolve the WordPress errors. But don’t worry! We have come with some of the most effective fixes for this particular WordPress error. So let’s start it.
Check WordPress Database Credentials
The most common reason for this WordPress error is incorrect database credentials. And if you have moved your WordPress website to a new host recently, you are most likely to receive this WordPress error. The wp-config.php file stores all your WordPress database credentials. It is a WordPress configuration file that contains WordPress settings including your database information.
In the wp-config.php file, you need to look for the following information –
Make sure the information like database name, username, password, and database host is correct. You need to confirm this information from the WordPress hosting account dashboard. To do that, log in to your WordPress hosting account > go to Databases section > click MySQL Databases.
This will take you to your database management page on your hosting dashboard. Now find your database name and username.
You will a list of users in the database. There is a Change Password link through which you can change the user password.
You need to check the database name, username, and password. If required, you need to change that information in your wp-config.php file. After that, you need to visit your website again to check if the WordPress error is fixed. But if the error still occurs, this means the problem is something else. Try out the solutions given below.
Check Database Host Information
If the problem is not associated with the WordPress database credentials, then maybe you need to check your database host information. Make sure the database host information is correct. Most of the hosting companies use localhost as your WordPress database host. However, some managed hosting service providers use separate servers to host WordPress databases. If that is the case, your WordPress database host information will not be localhost. You have to contact your hosting service provider to confirm your WordPress database host information.
Check if Database Server is Down
If everything seems to be fine but still you face an error establishing a database connection, then maybe your database server, i.e. MySQL server is down. And this often happens due to heavy traffic on your database server. It fails to handle the load when there is heavy traffic, especially when you are using shared hosting. Your WordPress site will become really slow, even some users may receive the error. So the best thing you can do here is that contact your web hosting provider to confirm whether your database server is responsive or not.
If you have other sites running on the same database server, you should check those sites to confirm whether your MySQL server is really down or not. But if you have no other site on the same server, then you should go to your hosting dashboard. Try to access phpMyAdmin and connect the database. If you can successfully connect, you need to verify whether your database user has sufficient permission or not. To do that, you need to create a new file, called testconnection.php. In this file, you need to add the following code –
Note: Do not forget to replace the username and password with the correct information.
And then, upload this newly created file to your WordPress site, and access it through any web browser. If it connects successfully, this means your database user has sufficient permission, and something else is wrong here.
To make sure that everything is fine, you need to go back to your wp-config file and check everything thoroughly.
Repair WordPress Database
If you still face the same WordPress error, maybe you need to check for database corruption. And if it is corrupted, you need to repair your WordPress database. And to do that, you can simply add the code (given below) in your wp-config.php file.
define( ‘WP_ALLOW_REPAIR’, true );
You need to add this code right before this – That’s all, stop editing! Happy blogging. Once you have successfully added the code, you need to visit this page to see the settings – http://www.yoursite.com/wp-admin/maint/repair.php
The screen that appears is shown below:
There are two options: “Repair Database” and “Repair and Optimize Database”. You can choose any of these accordingly.
Note: To access the database repair page, there is no need to be logged in. After repairing your database (or repairing and optimizing it), do not forget to remove the code (you added earlier) from the wp-config.php file.
Watch this video tutorial for a better understanding of how to fix WordPress error establishing a database connection –
The main objective of this article is to make you familiar with one of the most common WordPress errors – Error Establishing a Database Connection. In this article, we have discussed the reasons why this WordPress error occurs. And we have also learned how to fix this WordPress error. Hopefully, you are now able to resolve this WordPress error without being dependent on any professional or expert.