Even if you’re not a website owner, you’ve certainly come across the famous ‘Error Establishing a Database Connection’ while surfing the web.

The reason why this error is so common is that it can happen due a variety of reasons. In fact, it is so often that it becomes terribly frustrating, especially when you’re a WordPress beginner who has no idea what could have gone wrong.
establishing_a_database_connection
The truth is that most of the time such errors are not your fault, neither could you be familiar with all possible causes. That’s why we decided to gather some critical information, believing that we came at least close to compiling a proper list of causes and solutions.

Before you apply any of them, make sure you’ve backed up your data.

Error Establishing a Database Connection: What does it mean?

Let’s introduce you to some basic things first, as for example the manner in which WordPress displays your site on the web. Knowing this will be helpful to understand the effect of this mistake.

WordPress and everything related to it is written with MySQL and PHP. As you can see, two programming languages are applied: MySQL to store information in a database, and PHP to use it and retrieve it from there.

PHP takes over when you insert the URL in the address bar and then press Enter, in order to capture the homepage content (index.php) from the database, and to display it on the screen. At the same time, it accesses your wp-content folder, to retrieve media content, such as images and videos.

Let’s check the error now. When something like ‘Error establishing a database connection’ appears on the screen that indicates that the PHP code didn’t manage to establish a connection with the MySQL database, and to locate the information it has to display.

The reasons are numerous, which is why your homepage is often replaced by a blank one. Logically, there will be nothing because no information is available to display.

Here are some reasons for EEDC errors (and consequences), but not all of them:

  • Wrong login credentials, quotes, typos, or extraneous spaces. Failing to insert the right ones multiple times can block the database completely;
  • Unresponsive servers – It doesn’t have to mean that the database is the problem; it can also be the server where that database is ‘living’. If EEDC appears as a result of server problems, the server is irresponsive, affected by traffic upsurges, or even completely dysfunctional;
  • Corrupted files;
  • Corrupted database – Your database can be corrupted by malicious plugins, add-ons, themes, or data interruptions, meaning that it either misses certain files, or it is completely corrupted in the worst of all cases.

 

Does /wp-admin/ experience this problem?

In case that the same error is appearing on the front and back end of the website, it does. Theoretically, the message displayed should be equal on both pages. In case it is different (something like ‘One or more database tables are unavailable’), you are not supposed to undertake EEDC fixing measures, but to repair the database.

Assuming you got the same error on both ends, you should add this line to the wp-config.php file. Put it just in front of ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

define('WP_ALLOW_REPAIR', true);

Once this is done, visit this page to see the settings: http://www.yoursite.com/wp-admin/maint/repair.php

As to the second case, you should repair the database using the database repair page. However, have in mind that this is not a safe procedure: your URL will be open for anyone to access even without login information. That’s why you should remove the line you added in the code immediately after you’ve finished repairing it. It is the best way to protect your database from impostors and corruption.

The wp-config file should stay open only while you’re resolving the problem and immediately closed after confirming that the website is functional. If EEDC starts appearing again, it means that you have to check some of the upcoming steps.

Looking at the wp-config file

WP-Config files are often responsible for this type of errors, meaning that a connection with the phpMyAdmin files from the database was not established.

These files can be found in the public_html root directory on the server.

Those of you who edited/installed WordPress files manually will be familiar both with the file and the procedure:

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

The DB_Host value will not always be ‘localhost’ (usually, it will have the name of the host). That means that you can see something like HostGator, BlueHost, Site5, etc.

How do you solve the problem? Some members of the WP community suggested replacing the localhost with their IP because they believed that the local server environment is to blame for the error. On MAMP, for instance, changing the DB_Host value with the IP is a successful strategy.

define('DB_HOST', '127.0.0.1:8889');

Obviously, the IPs will be different depending on the hosting service.

Check the name (with the typos) first. If the name of the file seems correct, the file is not your problem.

Recheck the username and the password of your database

Had you arrived this far? You’re actually lucky. Checking the username and the password is not that much of an effort: all it takes is to create one .php file and to store it in the WP root folder. Just to remind you, that’s the very same folder where you looked for your wp-config.php file. Create the new file there, and choose whatever name for it.

All you would have to do is to open the menu in the root folder and to click on the New File option.

The file will appear instantly upon creation. Right-click on the file, and select Code Edit (you can also do this by selecting the file and choosing the option from the menu). The next thing to appear will be a blank page where you have to insert the following code:

<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Remember to change both the username and the password, but try the old ones first. In case they are connecting, it would mean that your user already has the permissions needed and that you have to look for the mistake somewhere else. A good way to start is to open the wp-config file again and to see if you’ve inserted the information correctly (pay attention to typos again).

Not being able to connect the database to the phpMyAdmin is a sign that the error appears due to server issues. Once again, that won’t necessarily mean that the MySQL server is down, but more probably that it didn’t provide enough permission to your user.

Is the error still there? Contact the hosting provider!

confusion
In the worst of all cases, you’d try the troubleshooting suggestions from this article, but the website will still be down, which shows you that the problem hides on the host’s end. Ask the company for support, and let them know that you’ve tried everything else before contacting them. They are the only ones that can determine the reason for such error, and fix it accordingly.

Conclusion

The most important thing to remember from this article is not to be alarmed when EEDCs appear. These errors are the simplest ones to resolve, and you’re not alone while doing it: The hosting provider is one call away!

2 FREE RESOURCES TO INCREASE PAGE SPEED AND GOOGLE RANKING

Join our mailing list to receive your FREE ebooks about how to boost your site speed, how to increase your google ranking and your visitors count along with the latest news and giveaways.

there are 2 comments added

  1. Abhay 5th July 2016

    Thanks dimi, i am also experienced this issue many times. But for me , its always because of hosting..

  2. Wordpress Developer 12th August 2017

    Excellent tutorial! This is really helpful to used my business site development. Great tutorial.Thanks.

Reset fields

back to top