When working on your site, making changes and adding new content and suddenly you see the dreaded error message Error Establishing a Database Connection. What is going on? This just happened to us and after countless hours trying to figure out the issue, we finally found out the reason and a couple of solutions.
There are quite a number of reasons why you may be getting this error. Some are easier to identify and fix and some… like in our case was a little more difficult to identify, but surprisingly, very easy to fix.
- Login credentials are wrong.
- Hostname is wrong.
- Database server is down.
- Corrupted database.
In our situation, the error happened both on the main site and in /wp-admin/. However, we were getting 2 different error messages. On the main site we were getting the following error:
Error establishing a database connection
Well, the error message wasn\’t exactly helpful. The error in /wp-admin/ had a little more text but still not very helpful:
Error establishing a database connection
This either means that the username and password information in your wp-config.php file is incorrect or we can\’t contact the database server at localhost. This could mean your host\’s database server is down.
Are you sure you have the correct username and password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?
If you\’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.
If you are getting the error messages above, you can proceed to identify the exact issue and fix it.
You made some changes to your database and something went wrong. This could happen when you messed about wp-config.php and changed the credentials on the database section.
The solution is pretty simple. Edit wp-config.php and put the right credentials. If you have edited wp-config.php there is a chance you might have accidentally altered the database information.
If you migrated your database to another server then you would obviously have the change the DB_HOST line to reflect your server IP. However, for shared hosting, instead of using localhost, you can try to change it to your local IP.
Do note that if your credentials are correct and they have always worked for you, there is no need to mess with it further as the issue lies else where.
In our case, we knew that we had not touched wp-config.php in sometime so there\’s no chance of us messing up the database credentials. We did however update some plugins and that could have problem screwed something up.
After checking with our server guys, we were sure the database server was up and running like it should. This only meant the issue was with the database itself. The solution would be to try and repair the database. We will be covering 2 ways of repairing your database. Which method you use really depends on your situation.
- Editing wp-config.php
- This method can be used in most scenarios
- Repairing the database with a plugin
- This is the simplest method but can only be used if you have access to wp-admin
If you are unable to login to /wp-admin/ then your only option is to edit wp-config.php and add the following line.
The line can be place just above your database-name like the example below. Leave your database credentials as they were.
Once you have included that line you can navigate to the following link on your web browser and you will be able to see the Repair Database and Repair and Optimize Database buttons just like the image below.
As we were able to access /wp-admin/, we used option as it was much easier than option one. As we have covered in our previous article on best WordPress plugins every blogger should have, we mentioned that we should keep our database in mint condition with WP-Optimize. In this article we will not touch on the functions of WP-Optimize but we will be looking at one of the Repair DB function.
It\’s fairly simple to use, just click on Repair DB and you will be presented with a list of your database tables. Just select Yes on the tables you want to repair, as your database is corrupted, it would be wise to repair all of them, then hit the Repair button.
Repairing the database did the trick for us and everything was back in order after that. Do let us know if you encountered this issue and if the solutions provided solved your problem.