Skip to main content

How To Fix the “Error Establishing a Database Connection” in WordPress

When you start using WordPress, you’re bound to encounter a number of errors, some grievous, other not so much. It’s inevitable; it can be your fault, at times systems break, someone fails at their job, natural calamities strike – anything could cause an error.

Even something as good as an increase in traffic can break your WordPress site, leaving you with an error and a bad taste in your mouth.

Now, just like any other human-made project, WordPress is susceptible to its share of errors, none of which is as baffling and strenuous as the “error establishing database connection” error. That’s because this error doesn’t come with details of what went wrong, despite that it can be caused by several things, which – of course – only adds salt to the wound.

All you get when you try loading your site is:

error establishing connection

In today’s post, we will look a little deeper into this WordPress error, and then cover some of the solutions you can use to restore your site to its former glory. Enjoy and please share your tips and solutions, or ask your questions in the comment section at the end.

What Does “Error Establishing a Database Connection” mean?

To give you an insider’s understanding of the “EEDC” error, we first need to break down WordPress into its two fundamental components.

WordPress is built using PHP (e.g. index.php, sidebar.php etc) and MySQL. PHP is in charge of all functions on your WordPress site (e.g. posting comments, retriving posts and pages etc) and MySQL is in charge of data storage.

For instance, when you type your URL in the address bar and hit enter, PHP kicks in, fetches and displays the contents of the homepage (index.php) from your MySQL database. Images, videos and such media are, however, stored in the wp-content folder, from where they’re retrieved by PHP.

Your MySQL database contains all the vital information PHP needs to create your site. Now, when PHP can’t access your MySQL database for one reason or another, you get the dismaying “error establishing database connection” message on your screen. Which is just PHP-speak for I couldn’t find the data I needed to put together you WordPress site. Here’s an error to show you I tried.

That said, the “EEDC” error can be caused by a number of reasons including but not limited to:

  • Incorrect login details; typos, wrong quotes and extraneous spaces – Changes to your database login details can “block” your website from accessing the database.
  • Unresponsive database server – The server where your database lives could be as dead as a dodo; caput, unresponsive or still recovering from a traffic upsurge.
  • Corrupted WordPress files
  • A corrupted database – Bad plugins, addons, themes and data-transfer interruptions might leave your database short of tables or completely corrupted at worst.

In the next section, we’ll outline the most common solutions that are known to work.

Troubleshooting “Error Establishing Database Connection.”

Of course, we would be insane if we tried to fix the problem without first establishing the cause. If we can determine the root of our error, then getting a viable solution is a matter of when.

Is Your WordPress Database Corrupted?

We can start by checking whether the error is as a result of a corrupted database. For this, we have to test if the error has crippled the back-end as well.

All you have to do is navigate to yoursite.com/wp-admin/. Please don’t forget to replace yoursite.com with your own domain name.

If loading the front-end (i.e. yoursite.com) and the back-end (i.e.  yoursite.com/wp-admin/) results in the “EEDC” error, your database is fit as a fiddle, and you needn’t concern yourself with the next step. You can skip all the way to determining whether you have the correct login credentials in your wp-config.php file.

However, if you’re getting “One or more database tables are unavailable…” or something else but the infamous “EEDC” error, your database is corrupted and in die need of repairs.

Don’t worry one bit though, as repairing your WordPress database is easy peasy work. You can be done in three simple steps. Firstly, locate the wp-config.php file, which lives in your WordPress root directory. The root directory is the folder where your WordPress site is installed.

You can usually find this folder inside the default home folder or “public_html”. Just login to your cPanel, and navigate to File Manager.

cpanel-file-manager-in-bluehost

Then navigate to your WordPress root folder:

open-file-manager

Locate your WordPress Root Folder Here

From this point, locating your wp-config.php file should be easy:

locating-wp-config-file-in-wordpress-root-folder

Once you have access to wp-config.php, open it in editing mode. Right click on the file to choose the code editor, or select code editor in the menu:

editing-wp-config-using-the-code-editor-menu-item

This will open your wp-config.php in a new tab. Add the following code at the bottom:


define ( 'WP_ALLOW_REPAIR' , true);

Save your changes.

The above code allows you to repair your WordPress database without having to login into phpMyAdmin manually, which might be quite a tussle depending on your experience.

The second step involves navigating to yoursite.com/wp-admin/maint/repair.php. This should open the following screen:

wordpress-database-repair-screen

Third step, just choose either “Repair Database” or “Repair and Optimize Database” and let WordPress do the rest.

NB: Once your database is repaired, remember to delete the code you added to your wp-config.php file. Otherwise, you risk granting hackers access to your database, where they will wreck havoc.

If repairing your database solved the error, well and good, just go ahead and pop the bubbly. If you’re still seeing the error after repairing the database, perhaps the error stems from incorrect login details, corrupted WordPress files or an unresponsive database server.

Let’s check whether your database login details are the problem.

Checking Your Database Login Credentials

Open your wp-config.php once again and look for the following lines of code:


// ** MySQL settings - You can get this info from your web host ** //

/** The name of the database for WordPress */
define('DB_NAME', 'database_name');

/** MySQL database username */
define('DB_USER', 'database_user');

/** MySQL database password */
define('DB_PASSWORD', 'database_password');

/** MySQL hostname */
define('DB_HOST', 'localhost');

The details in the second pair of parentheses, where we have ‘database_name’, ‘database_user’ etc, will not appear as shown in the code above. These details are specific to your website and host.

The four lines simply mean:

  • ‘database_name’ – The name of your database goes here e.g. ‘wordpress_vista’
  • ‘database_user’ – Your database username goes here e.g. ‘wordpress_new’
  • ‘database_password’ – This where you put your password e.g. ‘test!@#!@#’
  • ‘localhost’ – This is where you input your database host. The default is ‘localhost’ for most web hosts.

If any of these four credentials are incorrect, you will get the “error establishing database connection” error.

At this juncture, all you have to do is counter-check these credentials against those stored in your database. It’s easy peasy work.

First, let’s check if you have the correct database name in your wp-config.php. Follow the following steps:

From your cPanel, go to phpMyAdmin. In most hosting accounts such as Hostgator, Goaddy, and BlueHost, phpMyAdmin is usually located under the database tools tab:

database

The first screen you will see after clicking phpMyAdmin is the login screen:

phpmyadm

Once you’re logged into phpMyAdmin, click on the Databases tab to query a list of your databases:

databases-tab-in-phpMyAdmin

From the list of databases, can you see the database that appears after DB_NAME in your wp-config.php file?

list-of-databases-in-phpmyadmin

Example of what to expect.

If you can see it, you’re golden – the problem does not lie in the database name. If you can’t see the database in phpMyAdmin, you need to contact your WordPress hosting provider, as the problem might be their server.

If the database doesn’t exist, you will need to restore one from backup (if you have a backup in place) or create a new one. See why it’s super-important to backup your WordPress site?

Let’s assume you’ve found your database and you have the correct name in wp-config.php. Now let us check whether your database username, password and host are correct.

Checking Database Username and Password

Checking your username and password involves a little work. You need to create a .php file and store it in your WordPress root folder. This is the same folder where you found the wp-config.php file. Create a new .php file, and name it check.php or whatever you want.

This you can do easily by opening your WordPress root folder and clicking New File on the menu:

create-a-new-php-file-in-wordpress-root-folder

Once you create the file, it should appear in your root folder in a moment. Right click on it and choose Code Edit, or select it and click Code Editor on the menu. This should lead you to a blank page. Add the following code:

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

Be sure to replace ‘localhost’ ‘root’ and ‘password’ with the database host, username and password in wp-config.php respectively. Save changes, and navigate to yoursite.com/check.php. If you get “Connected successfully”, your login details are correct.

If you get “Could not connect: Access denied for user ‘yourusername’@’localhost’ (using password: yourpassword)” or some other error, your database host, username, or password is the culprit here.

If your login details are not working, you need to create new ones and then update your wp-config.php with the new details. To get the new credentials, you need to create a new user, which is also as easy as A, B, C.

Aside: Your problem could be as a result of the last credential, DB_HOST. By default, most web hosts use ‘localhost’, but you can try changing this to an IP address or URL as per your web host. You can get a list of hosts and their DB_HOST values on the WordPress Codex. If your host is not on the list, please contact them for this information.

Moving on, let’s create a new user.

Creating New Database User and Password

Go back to your cPanel and access MySQL Databases. It is also located under database tools, the same tab you found phpMyAdmin:

phpmysql

Clicking on MySQL Databases should open something like this:

mysql-databases-screen

Once the MySQL Databases screen loads, navigate down to where you have MySQL Users. Under this, you will notice Add New User:

add-new-user

Enter a new username and password, and click Create User. Keep note of these details for your wp-config.php file.

Note: Your setup could be different, but the procedure is much the same.

Once you create the new user, you need to assign the user to your  WordPress database. This is the database that is shown after DB_NAME in your wp-config.php file.

To assign the user to your database, go to the MySQL Databases screen once again, and scroll down to where you will see Add User to Database:

add-new-user-to-database

Select the user you just created and your database, and click Add. You can choose the user privileges in the next screen, but that’s not really important. Choose “All Privileges” if you must, and save changes.

Go back to wp-config.php and update the file with the new information. Make sure there are no spaces between the quotes. For examples, ‘ your-username ‘ is bad, but ‘your-username’ is great. Also, use single quotes as opposed to double quotes. Save all changes you’ve made to your wp-config.php.

Now, try accessing your website.

Are you still getting the error? If that’s a yes, the problem could be corrupted WordPress files.

Fixing Corrupted WordPress Files

Bram Valk once had the error after including interrupting PHP files in his footer. It could have caused him a lot of trouble had he not known the files were the problem, so he just eliminated the problematic files, and the error was gone.

Your error could be as a result of corrupted WordPress files, which shouldn’t worry you as other users have resolved this error simply by restoring their WordPress files. You need to be careful here because a tiny mistake can wipe away your entire site. First, make sure you back up your entire WordPress site before proceeding.

Then download a fresh copy of WordPress from WordPress.org. Unzip the package on your computer and then delete wp-config.php as well as the wp-contents folder. This will ensure you don’t break your existing wp-config.php file. It will also ensure you don’t lose the themes, plugins and media stored in wp-content once you restore WordPress.

Once you have this covered, upload the remaining files to your WordPress root folder using FTP or the File Manager. This should replace all problematic files.

Try reloading your site, and if you’re still seeing the error, first try cleaning your browser’s cache. If the error persists after  cleaning your cache, please ask your question in the comments, and we will come to your aid.

Additionally, please feel free to contact your web host because the problem could be on their side.

Final Thoughts

At the end of the day, you should not sweat when you encounter this error. As we’ve outline here today, it’s really simple to resolve, and should you run into any problem, your web host is always at your beck and call.

We are also always here to help you. We might not fix the error (if the problem is with your host) but we can and will always hold your hand and help you the best way we can.

We hope this article helped you learn how to fix the “Error Establishing a Database Connection” error in WordPress. You may also want to see our article on how WordPress beginners can dodge these 7 common mistakes.

Now you…

Have you experienced and resolved this error before? What caused it, and how did you solve it? Please share your tips and solutions with us, and make this post more resourceful. Thanks in advance!

Wp-dd.com

WordPress Design, WordPress Development, cPanel Hosting, Web Design, Web Development, Graphic Design, Mobile Development, Search Engine Optimization (SEO) and more.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.