If you are a regular WordPress website visitor or the owner of a WordPress website then you might have come across such kind of DB error multiple times. In fact, this “Error Establishing a Database Connection” is one of the most scared error for all the WordPress admins.
Usually, what happens is, when you open a WordPress website, instead of getting the designated page, you receive a page something like below-
As you are reading this post, that means either you are receiving this error message or have seen such kind of error. No matter what is the reason behind looking for the solution, you will get the complete details about this DB WordPress connection issue.
If you are not facing the issue also, you should know the solution to this WP error as this is one of the most common error any WP user face. Here we are going to talk about-
- What this “Error Establishing a Database Connection” message means
- Why such error arises and the root cause behind this
- Solutions involved to fix this WP DB related error
So, let’s start and explore this error, its solution, and the reasons causing this error. You can also check 000webhost.com post for more details about this database related WordPress error.
What is Error Establishing a Database Connection?
The Error Establishing a Database Connection is a WordPress error and it displays when your websites frontend code is unable to connect to the backend database. WordPress is developed in PHP as the frontend and MYSQL as the backend and when this two framework and languages won’t be able to connect to each other, you may face “Establishing a Database Connection” related error.
So, this error means, your PHP code was unable to connect to the database to retrieve the information it needed to display the requested page by the user. And this is the major reason behind not showing anything else rather than the error message because the system doesn’t retrieve any kind of further information due to a connection error.
Root cause behind Establishing a Database Connection Error
There can be various reasons behind this error and we are going to talk about the few important and major reasons behind this.
- Login Credentials
This is one of the major reasons behind getting the error establishing the database connection error. If you have made some changes to credentials of the database, then those need to be updated with the new credentials in the connection request so that the system will work properly. This ensures the proper connection between the frontend and the backend.
- Corrupted Database
This is another major reason behind the WP DB error. If your database itself is corrupted then definitely it won’t be able to make or serve any request. And the corruption of database can be caused by any means. For example, if you have installed any new plugin which has caused heavy load on the database and the load is continuous for some time, your database may get hung or corrupted.
- Database server is down
Another reason is if your database server itself is down then nothing will work for you. Again you can have the dedicated database server or it has been shared with others. So, before doing any triaging, make sure your servers are up and running and then jump to the further steps.
Now as you know what this WP DB connection error and the reasons behind the error is, let’s see how you can fix these.
Fixing Establishing a Database Connection Error
Here are some of the methods you can try to fix the error related to database connection in the WordPress.
Check if you are able to access the WP dashboard
There are chances that if you are getting this WP DB connection related issue, still you can get access to your WordPress dashboard. If you are able to access the wp-admin page of your WordPress dashboard and getting access to “One or more database tables are unavailable. The database may need to be repaired” then be sure that your database is corrupted and you need to triage accordingly.
Now let’s see how you can solve this database corrupted issue!
WordPress provides a mechanism through which you can repair your database but this feature needs to be enabled. By default, this is not enabled for anyone.
To do that open your file manager and access the wp-config.php file. Once you have open this file, add the below line to the end of the file-
define( 'WP_ALLOW_REPAIR', true );
This will enable the repair of the database of your website and you can do that by following the below URL-
Now just repair it and you are done. But make sure this database repair page is not secure and anyone can do for any URL if it has been enabled. And so, once you are done, remove the line we added to avoid any hacking.
Database credentials setting
If the above method has not worked for your, then the below is an option. Usually, the credentials don’t change but if you have changed the hosting or database name etc. then it may cause the problem. Again to triage and resolve this, follow the same wp-config.php page and you will find a section of code like the below-
/ ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define( ‘DB_NAME’, ‘database_name_here’ );
/** MySQL database username */
define( ‘DB_USER’, ‘username_here’ );
/** MySQL database password */
define( ‘DB_PASSWORD’, ‘password_here’ );
/** MySQL hostname */
define( ‘DB_HOST’, ‘localhost’ );
Just scan the above parameters and ensure that all are working fine. By correcting these, you will be able to access the site correctly. If you are not able to retrieve or check your previous credentials, you can create one new and use the newly created to avoid any issue.
Take help of hosting company
If the above two options haven’t worked for you, you need to check with the hosting company as this might be an issue with the hosting also. As we discussed, if your database server itself is down, nothing will work related to it.
Just take a screenshot of the error and write that you have done all kind of troubleshooting from your side and ask to check the health of the server. Rest of the things they will take care if you are the customer of some best web hosting companies for blog.
These were all about the WP DB related error and its resolution steps. As this is one of the frequently occurred issues and so being a WordPress admin, you need to understand the complete flow of this issue as we discussed above. Any comment/suggestion/clarifications are welcome on this post.