The Ultimate Guide To Fix Error Establishing A Database Connection In WordPress

Today, I will be discussing how you can fix error establishing a database connection in WordPress.

Getting error on our WordPress blog is quite frustrating.

Agree?

During my blogging journey, I had come across many types of errors.

When we talk about these errors, the most common error a user get is:

"Error establishing a database connection in WordPress."

Honestly, I have faced this error many times and Every time I faced this issue, I try to found out its possible causes and solutions to fix them.

So in this article, I will be showing you how to fix error establishing a database connection in WordPress.

You can also check out my article on how to fix to increase PHP memory limit to fix memory exhausted error in WordPress.

Why and The Reason For Getting Database Connection Error

Getting this error means WordPress is unable to establish a database connection.

There can be lots of reasons if you are getting this error varying from your unresponsive database and wrong or changed database login credentials to a corrupted database.

Sometimes you can get “error establishing a database connection” error due to server error.

How To Fix Error Establishing Database Connection In WordPress

Now we shall look at various ways for fixing this error.

But before making any changes make sure to backup your database.

Try one solution first and if it doesn’t work then you can move on to another solution.

Now let us proceed with database fixing:

Fix Database Connection Error by Repairing Database

If you are getting the same error message that is “Error Establishing Database Connection” on wp-admin as well on your site. Then your database needs to be repaired.

Add these following lines to your site wp-config.php file:

define("’WP_ALLOW-REPAIR’, true);

repair database connection using wp-config

After you have added these lines in your wp-config.php, you need to visit this page:

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

Note: Replace www.sitename.com with your site name.

fix database connection error by database repair

You must remove above lines from wp-config.php after you have successfully repaired your corrupted database as to access this particular functionality user doesn’t need to be logged in.

So removing define("’WP_ALLOW-REPAIR’, true); from wp-config.php is must.

If your issue is still not resolved then proceed to next solution.

Fix Database Connection Error by Changing Database Details And DB_Host Value

If you have changed your database user password or root password, then you must make changes to these changes to your wp-Config.php

file also.

Make sure everything is same.

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

If this issues still persists then try to replace localhost with IP in DB_Host.

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

Note: IP varies for different hosting services.

fix database connection error by MySQL

If the error is still not fixed move on to next solution.

Fix Database Connection Error by Checking MySQL Server

You can also get this error establishing a database connection when your site get loaded with a lot of traffic. This mostly happens if your site hosted on shared web hosting as their servers can not handle the load.

So, in this case, you should check if your MySQL server is responsive or not.

If you have multiple WordPress site running on same host server you should check if you are getting the same error on all the sites.

If you are getting this error on all the sites then there must be due to MySQL server.

But if you don't have any site on the same hosting account means if you have only one site.  Then to check MySQL responsiveness.

Go to cPanel > phpMyAdmin and connect the database. If you are able to connect then you need to check if your database has sufficient permissions.

To check create file testconnection.php and paste the following code:

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

You should replace the password. And if it get connected without any issue then it means you have sufficient permissions and it means MySQL is fine. Now it means something else is wrong. And in that case you need to check your wp-config .php file for typos.

If you are unable to connect to database then it means you don't have sufficient permissions or your MySQL server is down. Then you must follow the next solution. (Try this last solution and it will better assist you)

Contact Your Hosting Provider 

If you had tried all the fixes and still you are not able to fix database connection error, then you must try contacting your hosting provider.

You can phone or live chat with your hosting provider and ask for a possible issue with your site. They will probably tell you the main issue and also assist you in solving the error.

This is what I recommended to the newbies.

Remember it is better to ask for help instead of complicating the issue. All top hosting companies like Hostgator, BluehostSiteground provide quality customer support.

Before trying any of the fixes remember to backup your wp-config.php file and database. I hope this guide will help you to solve database connection error and get your site live.

If you enjoyed the article please share it over the internet.

For any questions and feedback let me know via comments. For updates please Subscribe.

Prince Sumberia
 

Hey Incredibles, Welcome to BroughtMeUp. I'm Prince Sumberia an incredibly passionate blogger and entrepreneur from India and. I write about Blogging, SEO, WordPress, Make money online & tech. I started BroughtMeUp as a passion and now it's helping readers to make money with their blog. Here at BroughtMeUp I write about Starting & managing a blog, WordPress, SEO, Social media, and Making Money online. You can read more about me at About Me Page.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: