How to Fix Error Establishing a Database Connection in WordPress

Now you…Open your wp-config.php once again and look for the following lines of code:
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.
Additionally, please feel free to contact your web host because the problem could be on their side.
First, let’s check if you have the correct database name in your wp-config.php. Follow the following steps:
Once the MySQL Databases screen loads, navigate down to where you have MySQL Users. Under this, you will notice Add New User:

What Does “Error Establishing a Database Connection” mean?

Example of what to expect.
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.
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.
Clicking on MySQL Databases should open something like this:
This will open your wp-config.php in a new tab. Add the following code at the bottom:

  • 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.

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.

Troubleshooting “Error Establishing Database Connection.”

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.

Is Your WordPress Database Corrupted?

Let’s check whether your database login details are the problem.
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.
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:
Enter a new username and password, and click Create User. Keep note of these details for your wp-config.php file.

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

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.
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.
From the list of databases, can you see the database that appears after DB_NAME in your wp-config.php file?

open-file-managerThe first screen you will see after clicking phpMyAdmin is the login screen:

Third step, just choose either “Repair Database” or “Repair and Optimize Database” and let WordPress do the rest.
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.
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.
database
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.
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.
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.
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.
In the next section, we’ll outline the most common solutions that are known to work.
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.
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!
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.
Moving on, let’s create a new user.

Checking Your Database Login Credentials

editing-wp-config-using-the-code-editor-menu-item
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.
error establishing connection

  • ‘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.

You can usually find this folder inside the default home folder or “public_html”. Just login to your cPanel, and navigate to File Manager.
If any of these four credentials are incorrect, you will get the “error establishing database connection” error.
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:
create-a-new-php-file-in-wordpress-root-folder
Then navigate to your WordPress root folder:
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.
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.
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.
From this point, locating your wp-config.php file should be easy:
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.

list-of-databases-in-phpmyadminTo 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:

All you get when you try loading your site is:
cpanel-file-manager-in-bluehost
// ** 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’);

Locate your WordPress Root Folder Here
phpmyadm
mysql-databases-screen
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.
databases-tab-in-phpMyAdmin
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.
Once you’re logged into phpMyAdmin, click on the Databases tab to query a list of your databases:
At this juncture, all you have to do is counter-check these credentials against those stored in your database. It’s easy peasy work.
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.

Creating New Database User and Password

wordpress-database-repair-screen
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.
Go back to your cPanel and access MySQL Databases. It is also located under database tools, the same tab you found phpMyAdmin:
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.
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.
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:
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.
add-new-user-to-database
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.
To give you an insider’s understanding of the “EEDC” error, we first need to break down WordPress into its two fundamental components.
define ( ‘WP_ALLOW_REPAIR’ , true);

The four lines simply mean:
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?
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.
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.

Fixing Corrupted WordPress Files

That said, the “EEDC” error can be caused by a number of reasons including but not limited to:
phpmysql
Now, try accessing your website.
Note: Your setup could be different, but the procedure is much the same.
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.
This you can do easily by opening your WordPress root folder and clicking New File on the menu:

Final Thoughts

The second step involves navigating to yoursite.com/wp-admin/maint/repair.php. This should open the following screen:
Are you still getting the error? If that’s a yes, the problem could be corrupted WordPress files.
locating-wp-config-file-in-wordpress-root-folder
add-new-user
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.

Posted by WordPress Guru