Fix “Error Establishing a Database Connection” Error in WordPress?
This article explains in details how to fix “Error Establishing a Database Connection” in WordPress. Each step is explained with the relevant screen shot of the step. So that user understand the topic well.
This is a very boring message when a site owner comes across such an error. In this article you will learn what the reasons of this error are and how to fix it.
What does “Error Establishing a Database Connection” mean?
Well, this error says that your database can not be connected to. In WordPress there are two parts of the website.
The files: it consists of files such as wp-content, wp-uploads, plugins, theme files etc
The Database: Database consists of the actual contents and information of all the posts and pages on your website.
PHP coding is used to extract information from these database files. This coding determines what information is requested by user and how to present it and in which format. If Mysql connection to the database fails, then the PHP coding will not be able to extract the required information. And that is the reason that you have the error “Error establishing a database connection”.
Reasons of “Error establishing a database connection”
This error can happen due to one or more of the following problems.
- Problem with hosting
If you have opted for cheap shared hosting then it is most probably due to your hosting. Contact your hosting provider and inform them of your problem. This is possible in shared hosting that database server is down because the service provider put restrictions on the number of active database connections or either your database server is down completely.
- Database is corrupt
This is another possibility of “Error establishing a database connection”. The database of WordPress is corrupted due to multiple reasons. One reason which is most of the time a reason is plugin. The plugin code is either incorrect of incompatible with the current version of WordPress.
- Wp-config.php is having incorrect database connection credentials
In this case the wp-config.php user name and password for database connection is invalid. If you have recently made changes to the login details then you will not be able to access the admin panel with the old user name and password. Make sure they are correct in wp-config.php file.
- Corrupt WordPress Core files
This is another possibility of the error “Error establishing a database connection”. If you update the WordPress core files and it could not update successfully then you will be facing the database connection problem. In most of the time the error will disappear if you revert to the previous version and update again.
How to Fix “Error Establishing a Database Connection” error?
If you want to fix the “Error Establishing a Database Connection “ error, apply the following methods one by one in the order given below. We have given a top to bottom approach, so it is possible that your site become OK in the first or second step.
Solutions to fix “Error Establishing a Database Connection”.
Solution 1: Repair the Database
If you can access your WordPress admin dashboard, then this is not the potential problem. Skip this step and go to the next step. And if you get the same error message when you access the WordPress Admin Dashboard, then you should apply this step as this may be the potential problem.
- Open your Cpanel
- Open file manager
- Navigate to public-html and elect wp-config.phh and click on edit
- Insert the following piece of code to the end of the file wp-config.php
Define ( ‘WP_ALLOW_REPAIR’,true);
- Save your changes
- Now open a browser and type the following in URL address
Domainname.com/wp-admin/maint/repair.php
Solution 2: Check wp-config.php for login credentials
If the database is fine, the next thing you should confirm about is the wp-config.php file. Here you have to check login username and password if they are correct or not. Again login to Cpanel. Go to public-html>wp-config.php and click on edit.
You will see the following file when you click on edit.
If the DB_USER and DB_PASSWORD is not matching the one given when you were installing WordPress for the first time, then php will not be able to connect to the database. Now you will have to create another user in the database.
Login to Cpanel and select MySql databases from Databases.
Scroll down to the users section in database menu. Now here you can add a new user or modify the current user to rename it and change its password so that it matches the entries present in wp-config.php
Solution 3: Solving Problem with Core WordPress file
Here have two options. Try both whichever works for you. First thing is to revert to the previous version of WordPress and update again. Second is to upload a new WordPress file. Download the WordPress from its official site in zip format. Go to Cpanel and delete wp-config.php and wp-content. Now upload the WordPress file in zip format and unzip it then in the same public html folder. If you get a message that some of the files are already present, overwrite them with the new files.
This was all about how to fix “Error Establishing a Database Connection” in WordPress. Hope you understand the causes and fixes of the error. If you have any question related to the procedure or still have the problem with your site, feel comfortable to ask it in comments. Your comment will be replied to as soon as possible.