Monday, 16 May 2016

Solution to Error Establishing Database Connection on WordPress Sites

So you just finish setting up a self-hosted WordPress site and got it running for some time. Then one day, you visit the site and all that you saw was “Error establishing database connection”. Things get worst when some of your friends or known subscriber to your blog start calling to tell you the same thing. This can be a very confusing situation, especially if one is experiencing it for the first time. It happened to me too but I was able to find a solution to it after doing some thorough research on the internet. In this post I’ll share how I fixed the problem with you.

Reasons for the Error
The reason browsers display this error on WordPress sites is because there is a problem with the database used in setting up WordPress. Usually there may be a problem with some tables in the database that need to be repaired. Other reasons may be; unresponsive database server, invalid login credentials or someone gained access to your database and change the login details. Several other factors could be the cause but the most common cause is that the database has been corrupt and need to be repaired. We’ll go through how to do this in the next section.

Solution
The first thing to do when trying to solve this problem is to back up your database. This put you on the safe side when things get out of hand unexpectedly. The solution ultimately lies in the wp-config.php file of your WordPress installation. So the next step is to determine whether the database needs to be repaired or the connection credentials need to be reconfigured. If you visit the admin panel of your site (e.g. yoursite.com/wp-admin) and you receive the error “One or more database tables are unavailable” then the database need to be repaired. Otherwise, if you receive the same error message “Error establishing a database connection” on the admin panel, then the connection settings in the wp-config.php file need to be tweaked.

Repairing the Database: To repair the database, open the wp-config.php file in an editor and locate the line where you have the code:

define('WP_DEBUG', false);

Insert the following code in the next line just before the comment that says “That's all, stop editing! Happy blogging.”

define ('WP_ALLOW_REPAIR', true);

Next repair the database by visiting yoursite.com/wp-admin/maint/repair.php.

WordPress will display two options to you. The first one is to only repair the database while the second option allow you to both repair and optimize. It is advisable to choose the second option, though it may take longer time depending on the size of your database. After the repair and optimization has been completed, undo the changes made to the wp-config.php file and visit your site once again.


Changing Connection Settings: Like I said both solutions lies in the wp-config.php file. So we’re going to tweak this file once again. It is in this file connection details are specified. So open your wp.config.php once again and check if the connection details is the same as what you initially provided when installing WordPress. The connection is the first you’ll see after the introductory comments and is as follows:

define('DB_NAME', 'database_name');

define('DB_USER', 'database_username');

define('DB_PASSWORD', 'database_password');

define('DB_HOST', 'localhost');

Check if the setting is okay. Make necessary changes if what you have in the settings in the code above is incorrect.


Conclusion

That is my contribution to this WordPress issue. When using the second troubleshooting option and you see that the database connection setting is correct, then something else is wrong. What should be done in this case is to check the MySQL server. Sometimes there may be too much traffic that the server is unable to handle. This is popular with WordPress sites using shared hosting. 

4 comments:

  1. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    wordpress training in chennai | Wordpress course in chennai

    ReplyDelete
  2. I’m really impressed with your article, such great & usefull knowledge you mentioned here.
    Web D School

    ReplyDelete
  3. Hi, I wish to be a regular contributor of your blog. I have read your blog. Your information is really useful.

    ✔️ PLC Training in Chennai
    ✔️ PLC Training Institute in Chennai
    ✔️ PLC Training Center in Chennai

    ReplyDelete

Add a comment here

Advert