When you are new to WordPress then you don’t want to get into any trouble instead it would be nice if you can have a comfortable journey. But that is not always the case instead some of you must have gone through this error message which says “Error establishing a database connection“. Whenever we receive any error message without doing anything, then we are always in a fix about how to get things back to normal. Here we have explained how you can fix this so that you don’t have to wander around looking for a solution.
Contents
Let’s get to the root of the problem
When we know what is the cause of an error, then it becomes easier for us to solve the problem. So in this case the error message is self-explanatory. WordPress is actually not able to set the connection to the database efficiently and that is the reason you are getting the error message. There are various reasons for this error and they are;
- Your credentials must have changed
- You might have entered wrong credentials
- Due to unresponsive database server
- Corrupted database might be another reason
- Error in the server is one of the major reasons for this error
It might happen for WP-Admin too
In order to check whether you are getting this error for WP-admin or not you will have to verify whether you are getting this error message in;
- Site’s front end
- Site’s Back end
If you find this error message on both these pages, then you can go ahead and follow the below mentioned steps to solve the issue. If you get some error message which informs you that more than one database tables are unavailable then you might consider repairing your database. In order to repair the database you have to add a line in WP-config.php file:
define('WP_ALLOW_REPAIR', true)
After doing this you can go through the settings when you visit: http:/www.yoursite.com/wp-admin/maint/repair.php
You don’t need to register with the site to have access to the settings after defining the repair because its main objective is to just repair the database which is corrupted therefore once you are done with the process you must remember to remove everything from that php file.
Recommended for you:
Migrate From Blogger To WordPress Without Losing SEO!
Webhost or MySQL server
If you are getting huge traffic all of a sudden then you will realize that you have started receiving this database error which was not there when you had less traffic. The database connection failed because your server is not allowed to take so much load ad it was not ready for it. Initially you will see that your site has started getting slower so when this happens you must act fast and talk to your hosting service provider to see whether the MySQL server that you are on is responsive or not. You can also carry on the checking yourself by verifying some other sites of yours that are on the same server are working fine or not if all of them are facing the same problem then you can be sure about one thing and that is your MySQL server has gone wrong somewhere.
In case you have only one site on this server then you can go through the check through cPanel from where you can access phpMyAdmin to get connected with the database. In case you are able to connect, then the next step will be to check the permission aspect and see whether the user is allowed or not. Now create a file, namely testconnection.php and in there you have to paste the code:
<?php $link = mysql_connect('localhost', 'root', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?>
If you have sufficient permission, then you will be able to connect easily and that means the problem lies somewhere else. You need to go back to the WP-config file just to make sure that there are no typo errors. If you cannot verify the connection through phpMyAdmin, then it is for sure that there is something seriously wrong with the server.
WP-Config file
WP-Config.php one of the most important file in the WordPress installation is the place to specify details in order to connect database. This file will need changing when you are changing the root password or the user password of database. Everything must be the same in your wp-config.php file.
define('DB_NAME', 'database-name'); define('DB_USER', 'database-username'); define('DB_PASSWORD', 'database-password'); define('DB_HOST', 'localhost');
When you are putting value for DB_HOST then you must remember that it is localhost only for popular hosting services like HostGator, Site5 or BlueHost. If your hosting service is different from these three then the value will be different in there. You can also fix the issue by replacing the localhost with the IP address. That means instead of DB_HOST, localhost you have to use DB_ IP address and you will be able to do the necessary checking.
You might get some kind of error when your user’s permission is reset due to some reason so when you know that the server is working fine by doing the checks, then give a call to your hosting services and see whether they are able to see what’s wrong with your site.
Some other solution
Everybody has their own reason of getting this error and not all solution works for everybody. There are people who have been getting this error message even when the database was repaired and he had done everything possible to solve the issue. Ultimately, it was seen that there was some URL problem so in that case the code used was:
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
When you are updating the code you need to mention the URL which has been facing this error message. This might solve the issue with many users, but there is no guarantee that all will be benefitted by the same thing so it is best to check what the exact problem is and then go by the solution.
Another important thing while carrying out the process is that you must take proper backups before you start with any of this process because in case you went wrong in the middle then you will not lose out on any information.