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 scary errors for all WordPress admins.
Usually, what happens is, when you open a WordPress website, instead of getting the designated page, you receive a page something like the one 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 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 did such an error arise 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 the 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 website’s frontend code is unable to connect to the backend database. WordPress is developed with PHP as the frontend and MYSQL as the backend and when these two frameworks and languages aren’t able to connect to each other, you may face an “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. This is the major reason behind not showing anything else other than the error message because the system doesn’t retrieve any kind of further information due to a connection error.
The 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.
1. Login Credentials
This is one of the major reasons behind the ‘error establishing the database connection’ error. If you have made some changes to the 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 front end and the back end.
2. 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. The corruption of a database can be caused by any means. For example, if you have installed any new plugin that has caused a heavy load on the database and the load is continuous for some time, your database may get hung or corrupted.
3. The 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 next steps.
Now that you know what this WP DB connection error is and the reasons behind the error, 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 WordPress.
1. 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 get 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 opened 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-
www.yourwebsite.com/wp-admin/maint/repair.php
Now just repair it and you are done. But make sure this database repair page is not secure and anyone can do it for any URL if it has been enabled. And so, once you are done, remove the line we added to avoid any hacking.
2. Database credentials setting
If the above method has not worked for you, 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 one 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 a new one and use the newly created one to avoid any issues.
3. Take help from the 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 write that you have done all kinds of troubleshooting from your side and ask to check the health of the server. The rest of the things they will take care of 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 occurring issues and so being a WordPress admin, you need to understand the complete flow of this issue as we discussed above. Any comments/suggestions/clarifications are welcome on this post.