Guide to overcome “Error Establishing a Database Connection”

Whenever we start using WordPress, we often come across a number of errors. Some of them are grievous while others are just glitch that can be sorted out easily. No matter how hard we try, these errors are inevitable. They can be a result of human errors or due to the breakdown of the systems.In other words, these errors are bound to occur and we, the users, have to deal with them.

Similar to any man-made computer project, WordPress has its own share of errors. However, no other error is much more time consuming, baffling and strenuous as the “error establishing a database connection” error. The reason which makes this a complicated error is that it doesn’t come with any details as to why this error has occurred or what went wrong. All we can see is just a single line message that states Error establishing a database connection. Now being a user or a WordPress developer what can we make out with this single statement. The answer -Nothing!

It this article we would like to describe the error itself, the cause, and the prevention methods to overcome this error for the people who are using WordPress development services.

Before we move towards the cause and the cure for this error, let’s first try to understand what actually this error is all about and what does it mean.

What does “Error establishing database connection” (EEDC) means?

To understand this error message, we need to split WordPress into its fundamental fragments viz PHP (index.php and sidebar.php), MySQL.php and MySQL. When you type your website’s URL in the address bar, the PHP fragment starts playing its role. It fetches and displays the content on the homepage from the MySQL database. Media like images and videos are stored in a wp-content folder which is retrieved by PHP. In other words, MySQL database is the storage compartment of a website which has all the vital information of the website. So when, PHP –the communicator of the website- cannot access any information it is seeking in MySQL database, it generates the message “Error establishing a database connection”. Through this message, PHP says –“I am unable to find the required data to put together your website, so here’s an error message for you.”

Since now we know what this error message means, now, let’s try to have a look at the causes that results in such an error message.

Error Establishing

The root cause for EEDC message

Corrupted Database

We know that database is the collection of tables and cells. Sometimes, as a result of bad plugins, unwanted add-ons, heavy themes and data-transfer interruptions, the database might come short of tables or may get completely corrupted. In such case, the data which PHP looks for your website is lost or unavailable and it shows the EEDC message on the screen.

Corrupted WordPress files

Often, as a result of some technical glitches, some the files of WordPress platforms you’ve installed gets corrupted. This again hinders PHP from fetching the essential details to upload your website. As it is not able to open those files, it alerts the user with “Error establishing database connection” message.

Incorrect database credentials

We are humans and are bound to commit mistakes. While inputting login credentials, we may input incorrect details because of typos, wrong quotes, and extraneous spaces. This may result in blocking your website to access the data base and again EEDC message pops up on the screen.

Above are the three major reasons that might cause the restriction in PHP to access the required file from database resulting in the “Error establishing database connection” message.

So when such an error occurs, what should be the plan of action to rectify this situation? How can one re-establish the connection between the website and the database and let the website work smoothly?

Well, let’s have a look at some of the troubleshooting steps for each case that can help users to solve database connection error for their website.

Troubleshooting for a corrupted database

If you are facing “Error establishing database connection” message, we advise you to check that whether this error message is a result of database corruption or the reason is something else.

For this, all you have to do is, move address bar, and type “website.com/wp-admin/. Keep in mind that here website.com is the name of your website so make sure you replace it with the name of your website.

If you get the same EEDC message, then need not to worry about your database. It is as fit as a fiddle and you can jump to the next level where you check that you have correct login credentials in a wp-config.php file.

However, if you get any other message other than the infamous EEDC message like “one or more database tables are unavailable…or something similar to this, your database is corrupted and needs repairs.

No need to worry about the repairing of your database. In fact, repairing a WordPress database is an easy task to accomplish.

This can be done in 2 simple steps;

First->

  • Locate up-config.php file. This file is stored inthe WordPress root directory.
  • Once you’ve located this file, open it in edit mode by right-clicking on the file and select code editor. This will open up-config.php in a new tab and you need to add the following at the very bottom – “define (‘WP_ALLO W_REPAIR’, true);
  • Save your changes.

The above-mentioned code allows you to get your WordPress database repaired without logging in into phpMyadmin manually.

Second ->

  • Navigate to website.com/wp-admin/maint/repair.php.
  • This would open a pop-up screen like mentioned below

Troubleshooting for a corrupted database

  • In this just select “Repair database” or “Repair and optimize database”.

NOTE: Once your database is repaired, remember to remove the code you wrote in wp-config.php as it can grant hacker access to your website’s database and can wreak havoc in your website.

If the error is rectified by repairing your database,then it’s well and good for you and you can start working on your website. However, if you are still getting the same message then you need to check for incorrect database credentials. Don’t worry, this article will guide you through all the possible reasons for getting this error.

Let’s look at the steps to troubleshoot incorrect database credentials.

Troubleshooting for incorrect database credentials

Open wp-config.php one more time and look for following lines of code in it;

  1. // ** MySQL settings – You can get this info from your web host ** //
  2. /** The name of the database for WordPress */
  3. define(‘DB_NAME’, ‘database_name’);
  4. /** MySQL database username */
  5. define(‘DB_USER’, ‘database_user’);
  6. /** MySQL database password */
  7. define(‘DB_PASSWORD’, ‘database_password’);
  8. /** MySQL hostname */
  9. define(‘DB_HOST’, ‘localhost’);

NOTE: If you’ll notice, when you look at these lines of codes in your website, you won’t find the fields as database_user, database_password, database_name, and database_host in a manner they are written here. These fields are specific to your website or domain.

Once you are at these lines of code, all you need to do is crosscheck that whether the fields have correct information mentioned here. But, how to do that?

Database_name

  • Open cPanel and go to phpMyAdmin

Database_name

  • When you enter the phpMyAdmin, the first screen you see is the login screen for the phpMyAdmin.
  • After successfully logging in, click on database tab to query the list of all the databases.

From the list of the databases, are you able to locate the name of the database by the name of your domain as it appears in wp-config.php file?

If yes, then there’s no issue with the database name and you can proceed for the database_user and database_password. And, in case if you don’t, then you can restore it from the backup stored somewhere else. Now you understand why it’s so crucial to always have a backup for your WordPress website.

Now let’s have a look at database_user and database_password.

Database_user and Database_password

Well, this step is the only step where I would say that you might have to walk an extra mile. First, create a .php file and save it in the root folder in wp-config.php. You can name it Test.php or whatever you prefer.

Once you have added this file in root folder, it will appear in wp-config.php in a while.

Right click on this file and select code editor.

This will open a blank page where you have to add the following code;

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 that you have replaced the “localhost”, “root”, and password with the one you’ve saved it in the wp-config.php file. If, with these details you are able to log in, your credentials are not the culprit. However, if you get messages like “access denied” or something else, then,in that case, you need to create new credentials.

To create new database credentials
  • Open cPanel again.
  • Navigate to MySQL Databases.
  • Locate database tools.
  • Scroll to MySQL users and add New User.
  • Enter new username and password.
  • Update the above details in wp-config.php.
  • Try to log in again.

Once the user is created, assign the user to the database by opening MySQL Databases screen once again, and scroll down to‘Add User to Database’ and finally update this information to a wp-config.php file and you are good to go.

With the above steps, you can eliminate the error message if it is because of a corrupted database. However, if the EEDC message is because of corrupted WordPress files, you have to take another approach.

Troubleshooting for Corrupted WordPress files

As said earlier, that EEDC message can be the result of many technical problems. And getting WordPress files corrupted is one of them. If somehow your WordPress files are corrupted, you need not worry as this issue can also be resolved conveniently. Below are the steps to troubleshoot EEDC message caused as a result of corrupted WordPress files.

  • Take a backup of your entire website.
  • Download fresh WordPress from wordpress.org.
  • Unzip the package and delete wp-config.php and wp-content folders from freshly downloaded WordPress.
  • Upload the remaining file to the existing WordPress root folder with the help of FTP or filemanager.
  • Try reloading your website.

If you are still facing the same issue, try a hard refresh i.e.Ctrl + F5 and delete your browser cache. With these steps, you can restart your work on the website easily.

Also, before starting any troubleshooting, try to contact your web host as the problem may be on their side. So, to be on the safe side, initiate troubleshooting by contacting your web host.

Author’s take

These steps may look a bit confusing, but when you start troubleshooting the error, you’ll come to know that you don’t even break a sweat while doing them. WordPress is developed in a manner that any person with no or least technical background can also work on it conveniently. So, just take some minor precautions while building your website, and the rest can be easily dealt with.

Leave a Comment

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

Scroll to Top