, ,

Fix the Error Establishing a Database Connection

Note: Before you make any database changes, make sure you have sufficient backups.

Why do you get this error?

You are getting this error because WordPress is unable to establish a database connection.
Reason: It could be that your database login credentials are wrong or have been changed. It could be that your database server is unresponsive. It could be that your database has been corrupted. In our experience, many times this error happens because of some sort of server error however there could be other factors as well. Lets take a look at how to go about troubleshooting this problem.

Does the problem occur for /wp-admin/ as well?

First thing you should do is to make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error message is the same on both pages “Error establishing a database connection”, then proceed onto the next step. If you are getting a different error on the wp-admin for instance something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

You can do this by adding the following line in your wp-config.php file. Add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

1 define('WP_ALLOW_REPAIR', true);

Once you have done that, you can see the settings by visiting this page:

http://www.yoursite.com/wp-admin/maint/repair.php

Database Connection

Remember, the user does not need to be logged in to access this functionality when this define is set. This is because its main intent is to repair a corrupted database, Users can often not login when the database is corrupt. So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.

If this repair did not fix the problem, or you are having trouble.

Checking the WP-Config file

WP-Config.php is probably the single most important file in your entire WordPress installation. Where you specify the details for WordPress to connect your database. If you changed your root password, or the database user password, then you will need to change this file as well. First thing you should always check is if everything in your wp-config.php file is the same.

1 define('DB_NAME', 'database-name');
2 define('DB_USER', 'database-username');
3 define('DB_PASSWORD', 'database-password');
4 define('DB_HOST', 'localhost');

Remember your DB_Host value might not always be localhost. Depending on the host, it will be different.

You can fixed this problem by replacing localhost with the IP.

1. define(‘DB_HOST’, ‘127.0.0.1:8889’);
IP’s will vary for online web hosting services.

Check your Web Host (MySQL Server)

You will notice this Error establishing database connection when your site gets swarmed with a lot of traffic. Your host server just cannot handle the load. Site will get really slow and for some users even output the error. So the best thing -livechat with hosting provider and ask them if your MySQL server is responsive.

Who want to test if MySQL server is running yourself. Test other sites on the same server to see if they are having the issue. If they are also getting the same error, then most definitely there is something wrong with MySQL server. If you do not have any other site on this same hosting account simply go to your cPanel and try to access phpMyAdmin and connect the database. If you can connect, then you need to verify if database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

1 <?php
2 $link = mysql_connect(‘localhost’, ‘root’, ‘password’);
3 if (!$link) {
4 die(‘Could not connect: ‘ . mysql_error());
5 }
6 echo ‘Connected successfully’;
7 mysql_close($link);
8 ?>

Make sure to replace the username and password. If the connected successfully, then it means that user has sufficient permission, and there is something else that is wrong.

If you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server. It does not necessarily means that your MySQL server is down. It could mean that your user does not have sufficient permission.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *