How To Fix Error Establishing A Database Connection In WordPress

122Shares
Error Establishing A Database Connection
  • Save

The “error establishing a database connection” message is an especially frustrating WordPress error where your entire site gets replaced by a message that looks like this:

error establishing a database connection
  • Save

If you’re reading this post, there’s a good chance that you’re experiencing this error right now. If not, you are in luck.

And even if you’re not staring at this message, learning how to fix the error establishing a database connection problem is still something that every WordPress user should know.

In this post, you’ll learn three things:

  1. What the “error establishing a database connection” message means
  2. Some of the most common reasons why this message appears
  3. How to fix this error in WordPress

What Does The “Error Establishing A Database Connection” Actually Mean?

If you’re not familiar with how WordPress works, there are two essential parts to a WordPress website:

  • Your files – this includes the WordPress software, your themes, your plugins, your media uploads, etc.
  • Your database – this is where the actual content of your posts and pages are stored (as well as lots of other important information).

Whenever a visitor lands on your WordPress site, the PHP files in WordPress reach out to your database to determine exactly what information to display.

Without a working connection to the database, the PHP-file part of your WordPress site can’t get the information it needs to render a working page. That’s why it displays the “error establishing a database connection” message instead.

What Causes The “Error Establishing A Database Connection” Message?

There are multiple potential reasons why you’re seeing this message.

Most of the time, these causes boil down to:

  • A corrupt database
  • Incorrect database configuration details in your wp-config.php file
  • Corrupt WordPress core files
  • Problems with your web host

Because you can’t be sure what’s causing the message for your specific situation, the best way to fix the problem is to take a diagnostic approach.

That is, you need to systematically follow a series of tests to isolate (and then fix) the problem.

Here’s what to do:

How To Fix “Error Establishing A Database Connection” in WordPress

Potential Fix 1: Check If You Can Still Access The WordPress Dashboard

When you see this message on the front-end of your site, the first thing to do is check whether or not you get the same error when you try to access your wp-admin dashboard.

If you still see the same exact error message when you try to access your wp-admin area, skip ahead to the next step. You’ve already eliminated this problem as a potential cause.

If you see a different message mentioning something like “The database may need to be repaired”, continue with this section to learn how to repair your WordPress database.

Step 1: Turn on the repair utility in the wp-config.php file.

To get started, you need to edit the wp-config.php file for your site. You can do this by either:

File Manager tool
  • Save

Your wp-config.php file is located in the root folder of your WordPress site. That’s the same folder that contains wp-admin and wp-content:

edit-wp-config
  • Save

Once you’re able to edit your wp-config.php file, add this short code snippet to the bottom of the file:

define( 'WP_ALLOW_REPAIR', true );

Make sure to save your changes before continuing.

Step 2: Run the automatic database repair utility.
Once you’ve added the code snippet, you can access the WordPress database repair tool by going to:

YOURDOMAIN.com/wp-admin/maint/repair.php

On that page, click Repair Database to run the tool:

Repair database WordPress
  • Save

Once the tool completes its repairs, your site should hopefully work again. If it does, all you need to do is return to your wp-config.php file and remove the same line of code that you added to enable the repair utility.

If your site still isn’t working, continue onto the next step.

Potential Fix 2: Check Your Database Login Credentials

If your database isn’t corrupt, the next thing you should check is whether your database login credentials are working.

Your database credentials are defined in your wp-config.php file:

wp-config.php file
  • Save

If this information doesn’t match the database credentials that you created via your host’s dashboard, WordPress won’t be able to access your database.

To test whether your database credentials are working, you can use the mysql_connect() function.

Use a text editor like Notepad to create a file called databasecheck.php.

Then, add this code snippet to the file:

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

Make sure to replace “mysql_user” and “mysql_password” with the actual username and password from your wp-config.php file.

Then, upload that file to your website via the cPanel or by FTP and access it by going to:

YOURDOMAIN.com/databasecheck.php.

If you see a message that says “Connected successfully”, your database credentials are working and you can skip to the next step.

But if you see a message like the one below, you have a problem with your database credentials:

Access denied for user 'root'@'localhost'
  • Save

The easiest way to fix the problem is to create a new set of database credentials to use for your WordPress site.

Step 1: Go to MySQL Databases in cPanel.
Assuming your host uses cPanel, you can create a new database user by going to the MySQL Databases option in your cPanel dashboard:

MySQL Databases
  • Save

Step 2: Create a new user.

Scroll down to the MySQL Users area and find the box to Add a New User.

Enter a new:

  • Username
  • Password
MySQL Users
  • Save

Make sure to remember these details as you’ll need them in a second.

Step 3: Add a new user to the database.
After you create a new user, go to the Add a User to a Database option and add the new user to your WordPress site’s database:

Add a User to a Database
  • Save

On the next screen, make sure to give the user ALL PRIVILEGES:

All Privileges
  • Save

Step 4: Update the wp-config.php file with the new database user.

To finish, you need to go back to your site’s wp-config.php file and update the DB_USER and DB_PASSWORD fields to the new user:

wp-config
  • Save

Once you save your changes, your site should hopefully be working again!

Potential Fix 3: Reupload Core WordPress Files

If neither of the above methods fixed the error, some of your core WordPress files may be corrupt.

To ensure this isn’t the issue, you can upload a fresh copy of WordPress to your site. Done right, this won’t affect any of your existing content, plugins, or themes.

Go to WordPress.org and download the latest version of WordPress:

wordpress.org
  • Save

Then, extract the ZIP file and delete the wp-content folder and the wp-config-sample.php file:

delete the wp-content folder and the wp-config-sample.php file
  • Save

Upload the remaining files to your WordPress site via FTP. When your FTP program asks you what to do about duplicate files, make sure to choose the option to Overwrite duplicate files.

This will ensure that none of your core WordPress files are corrupt, and it will hopefully fix the issue.

Potential Fix 4: Speak To Your Host

If none of the above solutions have worked, you may just be dealing with a hosting issue.

There are a couple of potential hosting problems:

  • Your database server is down. Some hosts use a separate server for databases, which means your database might be down even if your regular server still works.
  • Your database has met its quota. Some shared hosts put a cap on your database usage. If your site has met its quota, that might explain why your database is down.

To work through these problems, you’ll need to talk to your host’s support team.

Final Thoughts

By this point, you’ve hopefully been able to fix the error establishing a database connection message and have gotten your site working again.

There are still some smaller issues that might also cause this error. These issues are often unique to your site and are therefore hard to diagnose in a post like this.

If you continue seeing the error message after going through these fixes and talking to your host, you might want to consider reaching out to a WordPress developer to further investigate the issue.

Or, you can always leave a comment and hopefully we can all fix the problem together!

Here are a few hand-picked article that you should read next:

Was this helpful?

Thanks for your feedback!
  • Save
122Shares
Authored By
A Blogger, Author and a speaker! Harsh Agrawal is recognized as a leader in digital marketing and FinTech space. Fountainhead of ShoutMeLoud, and a Speaker at ASW, Hero Mindmine, Inorbit, IBM, India blockchain summit. Also, an award-winning blogger.

21 thoughts on “How To Fix Error Establishing A Database Connection In WordPress”

  1. Shafi Khan

    I’ve faced this issue one time and the first solution did the job for me.

    It can be a confusing task for some non-technical users. The good part is you’ve added proper images of each step and explained it in detail.

    Thanks Harsh!

  2. PUBG

    Excellent tutorial.

    I used to get this quite a bit, then I changed hosts and things cleared up. Since my host changed servers last month things seemed to speed up even more, which is awesome.

    This error can be annoying and confusing. So happy to tweet it to my followers to give them clarity.

    Thanks for sharing Harsh.

  3. Dora

    Well, thank you so much because this literally solved all my problems. I was having same “error establishing a database connection” problem. But this method solved it really. Thank you very much.

  4. Alex

    Thank you so much for sharing this step by step tutorial. Actually, I have installed WordPress first time, it shows me “Error Establishing A Database Connection” and I don’t know what to do. Then I have searched on Google and I found your this article and really it is
    very helpful for me and also many people who have no idea about this things.

  5. awais qureshi

    My site was down due to establish the database. I have contacted my provider, they solved my problem. Thank you for sharing this. Next time, I don’t need to contact my provider, I can solve this problem myself. Thank you for sharing the article.

  6. Vijay Pratap Singh

    Great informative article. It’s a best guideline for who doesn’t know about database setup on WordPress platform. Even I am very thankful to this post for know about wordpress database connectivity. Thanks.

  7. pat kerr

    thank you so much, I was able to get my site back & saved $$$ to get my hosting company restore it, which I had already pay them to restore b4. Great videos, clear to listen. Keep up the great job. Also tks to people like you that post free tutorials.

  8. arup

    Thanks, sir,
    This article is very informative. It’s the best guide for beginners who don’t know about database setup on WordPress.
    You discuss this step by step… That’s why I love your articles all time.
    Keep writing…

  9. sanjeev

    Hi Harsh,

    Nice guide. For some of the managed hostings or Amazon AWS, we do not get a cPanel but your method of updating WP-Config and doing the repair online will definitely help.

    I have faced this situation once or twice but most of the time it boils down to wrong credentials than anything else. Especially if we are using WP-Optimize plugin to manage the WordPress database.

    – Sanjeev

  10. Anita Chandel

    What a very helpful tip man!

    This is an easy thing to do to fix that database problem – you just have to know exactly how to do it. And you just showed us the way!

    Thank yo very much!

  11. Ravi Raushan

    when i started wordpress with free blogging , i got this problem always . but after transfering to premium hosting , i have never faced this problem . This post is informative . Thanx

  12. Ryan Biddulph

    Hi Harsh,

    Excellent tutorial.

    I used to get this quite a bit, then I changed hosts and things cleared up. Since my host changed servers last month things seemed to speed up even more, which is awesome.

    This error can be annoying and confusing. So happy to tweet it to my followers to give them clarity.

    Thanks for sharing Harsh.

    Ryan

  13. Shaad

    Thanks sir,
    This article is very imformative. It’s a best guide for beginners who doesn’t know about database setup on WordPress.
    You discuss this step by step… That’s why I love your articles all time. ❤
    Keep writing…

  14. Ashutosh Kumar

    Prevention is always better than cure. All though I mainly use blogger. But I have scheduled to migrate it to wordpress. Considering this, it is always good to know about the problems that I can facec and their possible solutions. Specially when these things come from your blog, one can always believe on in blindly and follow those steps.
    Thanks a lot for being with us.

  15. Freddy G. Cabrera

    Hey Harsh!

    What a very helpful tip man!

    This is an easy thing to do to fix that database problem – you just have to know exactly how to do it. And you just showed us the way!

    Thank yo very much!

    Cheers! 😀

  16. Susana

    Hello, Harsh.

    This article of yours is really very informative. Even someone with just a little to basic programming knowledge can understand and follow it. Plus, you accompanied the steps with pictures which makes it really easy.

  17. Rajnish Kumar

    Hello Sir,
    This is a very helpful article for the begginner to start with debugging the error. Thank you so much Sir as it will definitely help me if I face this error in my website.

  18. Rajinder Verma

    Howdy Harsh Sir,
    Learnt a lot from this, I had paid a handsome money to a developer for fixing a database error on my blog! At that time, I Googled many website to get thing fixed…

    Wonder why i don’t search on your site for a database solution!!

    This will help many bloggers for sure… thanks for sharing!

    – Rajinder

  19. ANAND

    I have faced this problem many times. I didn’t know what to do. I removed the existing installation and reinstalled. This is disgusting. I hope your post helps me it happens again.
    Thank you. Still If I have doubts can I ask you ?

  20. Sanjeev

    Hi Harsh,

    Nice guide. For some of the managed hostings or Amazon AWS, we do not get a cPanel but your method of updating WP-Config and doing the repair online will definitely help.

    I have faced this situation once or twice but most of the time it boils down to wrong credentials than anything else. Especially if we are using WP-Optimize plugin to manage the WordPress database.

    – Sanjeev

    1. Ankit Mishra

      Yes.. This can be helpful if I get this issue. Thanks Harsh for sharing such a information..

Leave a Comment

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

Scroll to Top
122 Shares