How To Fix Error Establishing a Database Connection In WordPress?

Let me tell you few things before showing you how to fix ‘Error Establishing a Database Connection‘.

Errors are unpredictable. It can happen any time but without reason it wouldn’t happen.

Sometimes error may cause from your side or from any other source. So the main focus of the website owner should be at fixing the error as soon as possible.

This is the most common error the WordPress website owners face. Some guys get panic when they see this error and start to look for the solution.

You shouldn’t be among the panicking guys instead you should take a cup of coffee and start looking for the “cause and solution“.

You might ask Why are you saying “cause and solution”?

The answer is straightforward, you might face this error in future also. So it is best practice to look for the root cause of the problem. So whenever you face the issue in future you should be able to solve it by own.

Understanding “Error Establishing a Database Connection”

The sentence “Error Establishing a Database Error” is self-explanatory.

The error is related to the database. The database WordPress is most compatible with is MySQL because this content management system is built primarily with two technologies.

  • PHP: Programming language in which WordPress is built with is PHP(Hyper-Text Pre-processor).
  • MySQL: This piece of technology is used for storing the website data like your posts, settings, users, etc.

So the question rises is “Why WordPress throws this error?“.

Well, there are few reasons which are listed below.

  • Incorrect Login Credentials: If you entered wrong database login credentials then the error will be thrown.
  • Corrupted Database: If your database is corrupted this might be the cause.
  • Server Memory: This may be another cause of this error.
  • Corrupted WordPress Files: If the core WordPress files are corrupted, this might be cause then.

Looking for the Cause of Error Establishing a Database Connection

My personal way to look for the cause of the error is to set debugging to “ON”.

This is the easiest way to look for the root cause of almost any error.

Until now I have faced this issue many a times, that is why I am listing this method here.

Where To Set Debugging to “ON”?

It isn’t a big deal. But before you start, please keep a fresh backup of the website.

Another thing I want to mention here is that you should know “Little programming“. If you don’t have any programming back then jump over the “Fix section” directly.

Step #1: Go to your core WordPress files. There you will find a wp-config.phpfile.

Step #2: Now starting editing it. Don’t edit anything inside the file yet. By editing I mean look for an option in your web host panel to edit the file.

Step #3: Inside the file look for this line define("WP_DEBUG", false);. Set the value “false” to “true” just like this define ("WP_DEBUG", true);. Now hit Save and visit your site. You will see different error message this time but with point source of this error.

As mentioned earlier, If you have programming background then do this step otherwise not.

How To Fix Error Establishing a Database Connection In WordPress?

As said earlier, you should first look at the cause of the error then you would be able to present a proper solution.

Repairing the Database

If your database is corrupted then there is no way to solve this except repairing.

Another point you should keep in mind is that if your website’s front-end and back-end is throwing the error, then your database is healthy and you just need to check the login credentials.

But if it is opposite to above, then your database is in need of repair.

Repairing a database in WordPress is pretty easy. You just have to follow these steps carefully.

Let’s start repairing database.

Step #1: Go to root files of your website. Inside the files look for the wp-config.php and start editing it.

Step #2: Inside the file look for this comment /* That's all, stop editing! Happy publishing. */. Before this comment add this line.

  1. define ( ‘WP_ALLOW_REPAIR’ , true);

Step #3: Now navigate to this URLyoursite.com/wp-admin/maint/repair.php. Be sure to change the yoursite.com with your domain and you will see this screen.

Step #4: Now click any of the two options but the recommended is second which is “Repair and Optimise Database“. You can head forward with first option also. I am not binding you this option.

Step #5: This step is just check. Navigate towards your domain and see if the error is gone.

If it is gone then the database was corrupted otherwise the problem is with login credentials which we will discuss in next section.

Checking the Database Login Credentials

Entering wrong database details can throw this error. You might check the login credentials first.

Let’s start and check how this method can help us.

Step #1: First thing you have to do is to note down your database credentials in Notepad.

If you don’t remember the login details approach towards web host support team.

Note: Checking the database login details is pretty simple. But I am not including that here because the users reading this post might use different hosts. And almost every host has different version of its back-end. So it is better to approach to customer care.

Step #2: Now open the wp-config.php file. Inside it locate these lines.

Step #2: Now open the wp-config.php file. Inside it locate these lines.

  • // ** MySQL settings – You can get this info from your web host ** //
  • /** The name of the database for WordPress */
  • define( ‘DB_NAME’, ‘db_name’ );
  • /** MySQL database username */
  • define( ‘DB_USER’, ‘db_username’ );
  • /** MySQL database password */
  • define( ‘DB_PASSWORD’, ‘db_password’ );
  • /** MySQL hostname */
  • define( ‘DB_HOST’, ‘db_host’ );


The above four lines of code store your database login credentials. If they are incorrect then you will get ” error establishing database connection ” error.

Step #3: If you followed the Step #1 then move onwards. Compare the login credentials you noted down on Notepad with these login credentials. If you found anything wrong, change it accordingly.

Check the website again. If you are still facing the issue, head towards the web host and ask for help.

Note: Sometimes the error is thrown due to reason of memory. If your server is running out of memory then this error may happen. Try increasing your server memory.

Conclusion

I tried my best to keep this tutorial as short as possible and to the point. I hope these methods answered you query “How To Fix Error Establishing a Database Connection In WordPress“.

If you are still facing the issue, please let me know in comments. Might be I will tell you other solution.

Please do share the post with your friends.

Leave a Reply

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