How to Fix Error Establishing a Database Connection in WordPress

Error Establishing a Database Connection in WordPress

Fixing the Error Establishing a Database Connection in WordPress

Many newbies who are trying to install WordPress will often face this error. It may be frustrating when you see this kind of errors even though if you think you have done everything correctly. With the growing popularity of WordPress each day many newbies are trying to install WordPress and still face this error on their screens. I still see many newbies banging their head against WordPress support forum. After seeing many newbies bombarding with questions about this error, I thought of writing a blog post about its fix with all the available solutions.

About the error:

This is how your error would look like

(Error establishing a database connection)

So what exactly happens?

When a user browses your WordPress site, it connects to your database and fetches the data from the database and displays it in your WordPress site. If your site is unable to connect to your database or if your database server is having any issues while fetching the data from it, you will be shown with the message “Error establishing a database connection.”

This is the simple illustration showing what will happen with WordPress when someone access the site.

Solution #1: (wrong database login details in wp-config.php)

While you were installing your WordPress, you will be asked to enter database details for your WordPress installation. If you supply with correct details, it will connect to your database and it will install your WordPress. Sometimes if your database login details were changed or reset, you might get this error. So, check your database credentials in wp-config.php file in your WordPress installation.

Make sure, you have supplied with correct database name, database user name, database password and database host.

If any of the above details were wrong means, you cannot able to connect to your database and therefore you will be unable to access your WordPress site and which is why you will be shown with error message saying “Error Establishing a Database Connection in WordPress”

If you think you have supplied with correct database credentials in your wp-config.php file and still facing the issue, then read solution #2.

Solution #2:

Make sure, your supplied database user has sufficient privileges to the supplied database. Sometimes people forget to assign privileges to the newly created database user for the newly created database. In order to check whether the database user has sufficient privileges or not, just go to your CPanel, and under Databases tab,

Click on MySQL Databases as shown in the above.

After that, if you scroll down a bit, you can see the option “Add users to Database

Now, select the supplied user name from the drop down box and select the supplied Database and click on add the user to the database. In the next screen, you will be shown to choose the privileges you want to offer to the user for that database. In that, assign all the privileges to that user, so that, WordPress can be accessed without any limitations.

And now click on “Make Changes”.

Now, the created user will have full access to the created Database, use this login info in your WordPress wp-config.php file. It should be able to connect to the database and should be able to access your WordPress site without any problems.

If you still facing the same problem, then continue reading with Solution #3

Solution #3:

Sometimes your database server might not work properly or your tables might get corrupted. This occurs usually happens when you receive tons of traffic or when you post too much content. People who face issues with corruption of their database tables are the people who have very big databases say like in Giga bytes. Your database tables might also get corrupted if you have different servers for web and database. Or sometimes, when you submit a post, data will get submitted from your WordPress site to its database, and while it is happening, if there is a connection failure between your webserver and database server, your table gets corrupted due to incomplete entry of the data. There are a lot of reasons why your tables gets corrupted. So, you will get the error “Error Establishing a Database Connection in WordPress” when your table got corrupted.

In order to check whether your tables got corrupted or not, try opening your WordPress admin panel and see if you are still getting the same error?

Open your http://www.yourwebsite.com/wp-admin/index.php

If you are getting a different error say like “One or more database tables are unavailable“. It means, your tables were corrupted. Follow the procedure to fix your corrupted tables.

In order to fix your corrupted tables, open your wp-config.php file and add the following line to your wp-config file.

define(‘WP_ALLOW_REPAIR’, true);

Lets add the above line to the bottom of your wp-config.php file something like the below.

It should appear something like the above.

After adding that line, just visit the following url

http://www.yourwebsite.com/wp-admin/maint/repair.php

You should replace yourwebsite.com to your domain name.

You should be able to see the following screen.

Now, Click on Repair and Optimize Database, in order to fix your corrupted tables. This will fix your corrupted WordPress tables.

It will not ask you to log into your admin panel, as its main intention is to fix your broken WordPress site. So you can simply visit the repair page without logging and repair your database tables.

You can also repair your database tables by logging into your PhpMyAdmin.

Select your WordPress database in your PhpMyAdmin, and select the tables you want repair, and in the bottom you can see the option “With Selected” and under drop down menu, you can select Repair Table. Choose that option to repair your corrupted tables.

Now, if you still can’t resolve your issue, then continue reading further.

Solution #4:

In your wp-config.php file, there is a field called

 

 

 

Usually, most of the hosting providers support using “localhost” as its value. But some hosts, do not support that, so thereby you will get an error saying “Error Establishing a Database Connection in WordPress”

In this case, make sure you ask your hosting provider for database host value. Sometimes it will be different from other than localhost.

For example yahoo hosting uses mysql for its DB_HOST value.

It will not be the same for all hosting providers. If you are still facing this error and have already tried all the above solutions, then check with your hosting provider whether your database host value is different from localhost.

Facing this error in localhost?

If you are getting this error in your localhost, then there is a solution for it. Usually you will get this error when you were using MAMP. Because, many people have reported that using localhost address (127.0.0.1) instead of localhost as DB_HOST has solved their problem.

In that case, the value should be like the following.

/** MySQL hostname */

define(‘DB_HOST’, ‘127.0.0.1’);

Solution #5:

If you recently moved your WordPress site from one domain to another domain and after that, you are facing this error means, it is something to do with your domain name. Maybe your new domain was not got updated correctly in your database. So try to update your new url in your database with the following sql query.

Open your PhpMyAdmin, and click on your WordPress database and then click on SQL


Now, enter the following query

UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl’

Make sure you replace YOUR_SITE_URL with your url.

This might solve the issue for people who have changed their domain recently.

If the above solution does not apply to you and still facing the error, then continue reading further.

Solution #6:

Sometimes, you might experience this problem when you have installed an awful plugin. So try to disable all your plugins and see if you can able to access your site. To disable your plugins, read how to disable WordPress plugins when you can’t access admin panel.

Conclusion:

Finally, I have included all the possible solutions for the error “Error Establishing a Database Connection in WordPress.” If you are still facing this issue means, please comment below with your site address, I would be glad to help you. Or if this article has helped you resolve your problem, share your story with which solution has worked for you.