How to Fix the Error Establishing a Database Connection in WordPress

Prevent Authors From Deleting Posts in WordPress

Sometimes when you are surfing the web, you may find a website saying just “Error Establishing a Database Connection” in bold on a white page in place of actual content. Yes, this is one of the few error pages we see on the web. If you are seeing this error on your own website, there is something wrong. If WordPress doesn’t solve issue automatically, you should be worry and think about resolving this issue.

Also read: What is Gravatar and why should you know about it?

Why do a website show ‘Error Establishing a Database Connection’ error

As it already explains itself, the website is not able to connect to the database. WordPress software is written in PHP to use MySQL database server. It saves and retrieves data from MySQL.When you type the URL of a page in the address bar, PHP scripts execute and fetches the content of the page from MYSQL database.

 In case the connection is failed, it shows the error saying Error Establishing a Database Connection.

The reason for this issue may not the same for the all time. There may be various cases including error login credentials, database server unresponsive of change in login credentials. If I list possible error reasons, there are:

  • Unresponsive database server
  • Incorrect login details
  • Corrupted database or table
  • Corrupt WordPress files

Most of the times, the reason could be “Unresponsive database server”.

How to Fix the Error Establishing a Database Connection in WordPress

In this article, I will try to explain the possible cause and possible solution to Fix the Error Establishing a Database Connection issue in WordPress.

Solution for: Unresponsive database server

This is the most common issue and most of the time, you should not care for it. MySQL database automatically restarts to fix this issue. So, in few seconds or minutes, database server of your website will automatically start responding to resolve this issue.

Solution for: Incorrect login details

This cause is the very rare case and it can only happen if you mistakenly added wrong login information of MySQL at the time of WordPress installation. Other reason could be the change of MySQL credentials.

To know if the issue is due to the wrong MySQL credentials, you need to first test the login credentials. For this open WP-Config.php and you will see database credentials in the code lines below:

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

If you changed the login credentials and know the new one, you should add the new login credentials here. If you are not sure whether these login credentials are right or wrong, you need to test it.

To test these credentials, create a test file in the root of your WordPress installation and add te the following code. Replace user_name and password with the username and password mentioned in the wp-config.php file.

<?php
$link = mysql_connect('localhost', 'user_name', 'password');
if (!$link) {
die('Could not connect to database: ' . mysql_error());
}
echo 'Connection is right';
mysql_close($link);
?>

Now save this test file as test.php and run it from the browser.

If it shows ‘connection is right’, you database credentials are fine. Otherwise, you need to work on putting the right WordPress credentials in the wp-config.php file.

Solution for: Corrupted database or table

If login credentials are fine but WordPress is still showing the error, there may be the serious issue with the database. It is possible that a table of the whole database is corrupt. To fix this, login into your database. For this, open the cpanel and the select PHPMyAdmin.

Warning: Be very careful while using PHPMyAdmin, because you will be dealing directly with your database.

In phpMyAdmin, select the database you are using for your WordPress blog. Here, select all the tables and then select repair table from the dropdown.

Repair table MySQL WordPress

If you do not want to login into the phpMySql, you can try alternate way. WordPress, allows you to repair the database. For this, add this line in the wp-config.php file.

define ( 'WP_ALLOW_REPAIR' , true);

Now, you open this URL yoursite.com/wp-admin/maint/repair.php. This should open the following screen:

wordpress database repair

Select any of these options and let WordPress do the rest of the things.

Note: Once you’re done repairing your database, be sure to remove the line of code you added to wp-config. It is because anyone can open this page and run this file.

Solution for: Corrupt WordPress files

If you tried the all other things but you do not see price fixed, there should be some problem in the WordPress installation. TO fix this issue, find the corrupt file and fix the issue. Finding the corrupt file in the WordPress installation can be a big headache. So, you can try replacing all WordPress core files with the new fresh files.

But this is risky and can wipe away your entire website. So, you need to perform this carefully. If you are not sure, you can hire a developer. If you are thinking to do by yourself, you should first take the backup of the whole site.

Then download the free copy of WordPress from WordPress.org and unzip on your computer. From this, delete wp-config.php and wp-contents folder. Be sure to delete both things. It ensures that you don’t lose your themes, plugins, media and existing settings.

Now upload all these files in your WordPress root folder using FTP or the File Manager.

Once you have uploaded all fresh files, reload your website to see if the error is fixed. You should try opening the site in incognito mode.

Final thoughts

I tried to list all the possible ways and solutions to fix the Error Establishing a Database Connection issue in WordPress. It will solve the issue for sure. If you still have anything to ask, you can always leave a comment.

Advertisement

Tags: | |

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker