5 Best Ways to Fix Error Establishing Database Connection on WordPress or WooCommerce

5 Best Ways to Fix Error Establishing Database Connection on WordPress or WooCommerce

WordPress is one of the most popular and one of the most used content management systems in the world. It has many of its pros and cons. But sometimes it does become very difficult to handle, due to different errors. It is something you just can’t avoid. Error Establishing a Database Connection is one of the most common error for WordPress or WooCommerce self hosted websites. We will discuss in detail, why this error occurs and what ways you should opt to fix it.

These errors really give you tough time, though they are not too hard to handle. It is just because many wordpress web masters do not know the basics of how world wide web and wordpress work. Today, we will talk about one of the wordpress errors, which haunts many webmasters. It is error establishing database connection. We will talk in detail about the reasons of error establishing database connection and how to fix it?

How To Fix Error Establishing Database Connection on WordPress or WooCommerce Website?

5 Best Ways to Fix Error Establishing Database Connection on WordPress or WooCommerce

None of the wordpress errors is as confusing as error establishing database connection. It does not give you any clue of what went wrong, and you are left confused what to do and what not to do. You just saw the white screen with only five words written.

Best WordPress Migration Clone Service - Move New Hosting Cloud Server

“Error establishing a database connection.”

To get deeper insight, we must know about the basic building blocks, wordpress is built upon. First is PHP, and second is MySQL.

PHP is a programming language. It is the language in which the core wordpress’ files are written. What PHP handles is the functions and working of WordPress.

MySQL is basically database technology. It is responsible to manage database. It has to store all of your website content. Even the smallest piece of information about your site is stored in PHP.

Now lets explain their working in conjunction. When the user enters the URL, PHP starts working. It fetches the data stored in MySQL and displays on the page. When PHP fails in doing so, the cursed message of ‘error establishing a database connection’ shows up.

There can be number of reasons behind that. Let us take a quick look at all of them.

1. DATABASE SERVER GETS DOWN The web server which stores the files of your site may be down. The reason can be anything.

2. CORRUPT DATABASE Sometimes database gets corrupt. Reason can be bad plugin, hacker attack or failure of wordpress theme.

3. INCORRECT LOGIN DETAILS You need a specific  login and password to use the database. If those have changed wordpress would not be able to access the database.

4. PLUGIN/THEME Sometimes all the havoc is caused by bad plugin or theme.

5. WORDPRESS FILES GET CORRUPT Failed update and numerous other reasons can corrupt core wordpress files.

6. TRAFFIC LOAD EXCEEDS THE LIMITS Even though traffic load is what we all web master wants, but sometimes it comes with its own problems. Your database might stop responding due to exponential increase in traffic.

That’s all with the detail you needed to know before starting to fix the error establishing database connection. Now that it is over, lets get your hands dirty. We just told you the main reasons which cause the errors. In all these cases, we will apply the different fixes. Lets start one by one.

1. Server Down Scenario

It always feels safe to think that there is no problem at your end. Also the procedures of removing the errors caused by your site are lengthy. If you start working on them, and later you realize that problem was from your web hosting server, you will just feel terrible. So its the first scenario you should consider. Just get in touch with your web hosting provider and make sure there is no problem at there end and then go further. We have covered Top 5 Best Managed Hosting Providers for WordPress Blogs & WooCommerce Stores. Make sure to choose top quality hosting providers to avoid downtime and revenue loss.

All quality hosts provide 24 hours online support. You can chat them live. Tell them that you are having error establishing database connection. Ask them if their database server is working correctly. Also ask if there is any spike in activity or traffic on the server right now. In case there is a problem at there end, just relax and wait for them to resolve. If they assure that their servers are perfectly fine, proceed to next step.

RELATED:   How To Install WordPress on Google Cloud (Ubuntu or Debian) with EasyEngine?

2. Corrupt Database Scenario

It is not a big deal to fix the corrupt database, but first you need to make sure that the reason of error really is corrupt database, not any thing else. So lets check if our database is OK or not.

The procedure is very simple. It is important to check if the error remain same at both front end and the back end.

Navigate to yoursite.com/wp-admin/. Make sure you replace your site with whatever is your own domain name. You know that when you opened yoursite.com it showed EEDC(error establishing database connection). If you see the same when you entered yoursite.com/wp-admin/, then your database is perfectly fine. Just skip this step. If it shows some thing else, than you have to follow along.

If you see “One or more database tables are unavailable”, or any thing other than EEDC error, than your database has got corrupted. It desperately needs to be fixed.

If you see picture below, just pay attention to what WordPress is saying you. You might encounter a good piece of advice there.

But if you see the message like “One or more database tables are unavailable”, you have to apply another strategy, because it means you have to do it at your own.

First of all, locate wp-config.php file. It lives in your WordPress root directory. Root directory is the folder, where your WordPress site is installed. Login to your cPanel and than to File Manager. In file manager, visit your wordpress root folder. It should not be difficult to locate wp-config.php file, in your wordpress root folder. Open this file in editing mode. Right click to choose the editor.

There is a piece of code, you have to add at the bottom of code. Here it is:

define ( 'WP_ALLOW_REPAIR' , true);

Adding the code written above would allow you to repair your wordpress database. Plus is that you would be able to do it without manually logging in to phpMyAdmin manually.

Now navigate to this link.


It would open up the following screen.

Now choose either of these two options. Repair database and repair and optimize database. Now just wait and watch. WordPress will do the rest. After you resolve the error, and things go back to normal do not forget one thing. Delete the code you have added in wp-config.php file. Do it instantly after the problem is solved. Otherwise, hackers can easily access your database and make you suffer more than ever.

Congratulations, if you have solved the problem. If not, keep reading. It is just because the problem is not caused by corrupt database, but something else.

3. Incorrect Login Credentials Scenario

We know that WordPress uses a specific username and password to connect to the database. These credentials normally do not change by themselves. But sometimes any changes you make the files of your site causes them to change, and you literally have no idea. If both points mentioned above have failed, than you should check if login credentials have changed.

Once again, open your wp-config.php file. 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’);

These lines have all the code WordPress needs to connect to database. The code is totally self explanatory and even the most lay person can understand that the names of database goes in the line of DB_NAME.

There are total four parameters. The name of database, its username, its password and web address of your data server. Now you have to counter check these credentials, with those stored in your database.

RELATED:   Gutenberg Block Editor for WordPress New Features & Ease of Use

Go to phpMyAdmin from your cPanel. It must be located under the database tools tab.

After logging in to phpMyAdmin, click on the databases tab.

From the list of databases, can you figure out the one that appears after DB_NAME in your wp-config.php file.

If you can see there is no problem with database name. Go and check database username and password. If you cant, contact your web hosting provider and ask them to re-check every thing. May be your database does not even exist. You will either have to create a new one or restore the one from backup.

The database username and password might require some extra work. Create a .php file with any name you want and store it in your wordpress root folder. After you have created the file and it starts appearing in root folder, right click on it and choose edit. Add the following code.

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

Replace the ‘localhost’, ‘root’, and ‘password’ with database host, username and password in wp-config.php file. Save changes and visit your site. If it has started working, the credentials are fine now.

If the problem still persists, your login details are not working, and you have to get the new ones. It can be easily done by cPanel. Just click on MySQL databases tab. This tab would also be found under database tools just like phpMyAdmin.

In the this screen, scroll down to MySQL users. Enter the details and click on create user. Afterwards you will have to go to the same tab to add user to database. You will also have to go back wp-config.php file and update the changes.

Now, try to access the site.

4. Issues With Plugins Or Themes

There might be any issue with one or more plugins. Login to wp-content folder and disable all plugins. This can be done by renaming plugins folder into something else. It will cause all plugins to stop working.

Now check if the site loads. If it does, it is sure that error was caused by one or more plugins. Now rename the plugins folder. It should be ‘plugins’ once again. Now start renaming the main folder of each plugin one by one, and then check if the site loads. In this way, you can find the culprit plugin.

If messing with plugins does not work, there might be some problem with theme. You can check it exactly the same way you checked plugin.

5. Corrupt WordPress Files Scenario

If the problem is still not solved, check your core wordpress files. There can be multiple reasons, like hacker attack or something went wrong during transferring files via FTP. Anyways, its better to take backup of your site before as you are going to replace the core version of wordpress.

Download a fresh copy of wordpress from wordpress.org

Unzip it on your computer. Delete the folders of wp-content and wp-config-sample.php file. Now upload remaining files to your site via SFTP. Your existing files will be overwritten and hence all the problematic files would disappear.

How To Avoid Error Establishing Database Connection?

There are two things you must ensure if you never want to see it again. First is that you never compromise on quality hosting. Besides, it will save you from a lot of other errors too. Second thing is that you regularly take backups. Prevention is always better than cure. It would also be good to consider using any proper backup plugin.

Final Thoughts

So in the end we hope, now you will not panic if you see error establishing database connection. It is not the rocket science to tackle it. As we have summarized all the basic reasons and remedies for you. Your feedback would be appreciated.

Develop SEO Optimized Business WordPress Website

If you are looking for Complete WordPress Website Development Packages that include domain name, web hosting and website developmentMT Web Sol Quick Shop is here to set up everything for you. Our team has 5 years of experience with Latest Web Technologies and we are proud to serve our valuable clients globally. Get in Touch to discuss your project today!

Leave a Reply

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