Website Guider

The Ultimate Resource for WPadmins and Geeks

How To Fix Error Establishing a Database Connection In WordPress?

Let me tell you a few things before showing you how to fix an error establishing a database connection in WordPress?

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

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

Error Establishing a Database Connection 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 the future also. So it is best practice to look for the root cause of the problem. So whenever you meet the issue in the future, you should be able to solve it independently.

Understanding “Error Establishing a Database Connection”

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

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

  • PHP: PHP(Hyper-Text Pre-processor) is the programming language used by WordPress.
  • MySQL: MySQL is used to store the website data like your posts, settings, users, etc.

So the question arises “Why WordPress throws this Error? “.

Well, there are few reasons which are listed below.

  • Incorrect Login Credentials: If you entered the wrong database login credentials, the error would 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 the 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.”

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

I have faced this issue many 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.php file.

Step #2: Now starting editing it. Don’t write 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 a different error message this time but with a point source of this error.

As mentioned earlier, If you have a 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, and 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.

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

But if it is opposite to above, then your database requires repair.

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

Let’s start repairing the database.

Step #1: Go to the 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.

define ( 'WP_ALLOW_REPAIR' , true);

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

database-repair-1

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 the first option also. I am not binding you with this option.

Step #5: This step is just checking. 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 the next section.

Checking the Database Login Credentials

Entering the 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 note down your database credentials in Notepad.

If you don’t remember the login details approach towards the 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.

// ** 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' );

Now, your database login credentials are stored. If they are incorrect, then it will produce an “error establishing database connection” error.

Step #3: If you followed 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.

Recheck the website. 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 your query “How To Fix Error Establishing a Database Connection In WordPress.”

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

Please do share the post with your friends.

Raashid Din Dar

Raashid Din Dar is a web developer and designer. He specialises in WordPress Theme and Plugin development. Also, Web Designing is what he loves.He loves to read books and play with friends.

3 thoughts on “How To Fix Error Establishing a Database Connection In WordPress?

  1. Aw, this was an incredibly nice post. Spending some time and actual effort to generate a top notch article…
    but what can I say… I put things off a lot and don’t seem
    to get nearly anything done.

  2. Wow, fantastic blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your
    site is magnificent, let alone the content!

Leave a Reply

Your email address will not be published.

Back to top