WordPress Tutorials

How to Fix Error Establishing a Database Connection in WordPress

Did you just try to access your WordPress site only to be met by a scary message saying Error establishing a database connection instead of your WordPress site?

The Error establishing a database connection WordPress error is a somewhat common error that routinely pops up after changing hosts or migrating a WordPress site, though you might see it in other situations, too.

It can be scary because it seems like your site has completely disappeared and all of your data is gone.

First – don’t worry. Even though things look bad now, all of your site’s data should be safe and secure in the database. We’ll talk about this more in a second, but the issue here is that your WordPress site isn’t able to access your data, not that the data doesn’t exist.

Second, there are some troubleshooting steps that you can take to quickly fix the issue and get your site working again. So you should be able to fix the problem with minimal effort.

In this post, we’ll do two things:

First, we’ll explain a little more about what the Error establishing a database connection WordPress error means and why you might be seeing it.

Second, we’ll show you how to fix the WordPress Error establishing a database connection message for good. The exact troubleshooting steps that you’ll need to take often depend on when you’re encountering the message, so we’ll share different troubleshooting steps for two scenarios:

  1. Seeing this error on a newly created site or right after migrating a site.
  2. Seeing this error on an existing site where you didn’t change anything.

By the end, you should have a fully working WordPress site again!

What Does “Error Establishing a Database Connection” mean?

The Error establishing a database connection error message should look something like this on your WordPress site:

To help you understand what this error message means, we first need to break down WordPress into its two fundamental components.

There are roughly two parts to your WordPress site:

  1. Your files – these are your site’s PHP and HTML files, your CSS stylesheets, your JavaScript scripts, and so on.
  2. Your data – this your blog post content, your site settings, your users, your comments, and so on.

All of your site’s files sit on your server, but all of your site’s data lives in the database (hence the name).

If your site’s files can’t “talk” to the database, your site has no way of accessing all of the content, settings, and so on that it needs to render your site to visitors.

Because it’s impossible to display your site without accessing the database, your WordPress site will display the Error establishing a database connection message instead.

What can cause your site to have issues with connecting to the database? Well, we’re going to dig into that in detail in the troubleshooting steps in the next section, but some of the most common issues are the following:

  • Incorrect database credentials – just like you need a username/password to log into your WordPress dashboard, your site needs a username/password to connect to the database. If these credentials are wrong (or if the database name is wrong), that will trigger the error. This issue can happen a lot when you migrate a site to a new host.
  • Unresponsive database server – The server where your database lives could down, which is an issue with your hosting rather than your WordPress site.
  • A corrupted database – Bad plugins, add-ons, themes, and data-transfer interruptions might leave your database short of tables or completely corrupted at worst. Thankfully, WordPress includes its own database repair tool.
  • Corrupted WordPress files – issues with core WordPress files can also cause this error in fringe situations. The fix is to re-upload a clean copy of the core WordPress software.

In the next section, we’ll outline the most common solutions that are known to work.

How to Fix Error Establishing a Database Connection in WordPress

There are two high-level situations when you might see the Error establishing a database connection message in WordPress:

  1. After migrating a site to a new host/server or trying to create a brand new WordPress install.
  2. On an existing site where you haven’t made any recent changes.

These situations typically have different fixes, so we’re going to divide them into different sections. You can click the links above to jump straight to the specific section with your scenario.

With that being said, this can be a tricky error. So if the tips in one section don’t work, we recommend also trying the tips in the other section. Basically, we’re just trying to direct you to the troubleshooting steps that are most likely to fix your specific situation so that you don’t waste time.

1. Error Message After Migrating a Site or Creating a New Site

If you see the Error establishing a database connection message after migrating your WordPress site to a new host or creating a brand new WordPress site, the problem is almost always incorrect database credentials.

In order for your WordPress site to connect to the database, it needs to know the following information about the database:

  • Database name
  • Database user username
  • Database user password
  • Database location

When you migrate a site, sometimes the database credentials at the new host can be different than they were at the old host, which will trigger the error.

Here’s how you can fix that.

Check Your Database Login Credentials

To begin, you’ll want to find out which WordPress credentials your WordPress site is configured to use.

To do this, you’ll need to access your site’s wp-config.php file, which you can do by connecting to your server via FTP or using something like cPanel File Manager or your host’s file manager tool.

The wp-config.php file should be located in your site’s root folder. Open the file and look for the following lines of code:

// ** MySQL settings - You can get this info from your web host ** //

/** The name of the database for WordPress */define('DB_NAME', 'database_name');

/** MySQL database username */define('DB_USER', 'database_user');

/** MySQL database password */define('DB_PASSWORD', 'database_password');

/** MySQL hostname */define('DB_HOST', 'localhost');

The details in the second pair of parentheses, where we have ‘database_name’, ‘database_user’ etc, will not appear as shown in the code above. These details are specific to your website and host.

The four lines simply mean:

  • 'database_name' – The name of your database goes here e.g. ‘wordpress_vista’
  • 'database_user' – Your database username goes here e.g. ‘wordpress_new’
  • 'database_password' – This is where you put the password for your database user e.g. ‘test!@#!@#’
  • 'localhost' – This is where you input your database host. The default is ‘localhost’ for most web hosts.

If any one of these four credentials are incorrect, you will get the Error establishing a database connection message.

At this juncture, what you need to do is counter-check these credentials against those stored in your database.

Check the Database Name

First, let’s check if you have the correct database name in your wp-config.php. Complete the following steps:

To get started, open phpMyAdmin. In most hosting accounts such as HostGator, GoDaddy, and Bluehost, phpMyAdmin is usually located under the Databases or Database Tools tab in cPanel or something similar:

Your host should automatically log you into phpMyAdmin. Or, you might be prompted for a login screen in rare cases.

Once you’re logged into phpMyAdmin, click on the Databases tab to query a list of your databases. From the list of databases, can you see the database that appears after DB_NAME in your wp-config.php file?

Example of what to expect.

If you can see it, you’re golden – the problem does not lie in the database name. If you can’t see the database in phpMyAdmin, you need to contact your WordPress hosting provider, as the problem might be their server.

If the database doesn’t exist, you will need to restore one from backup (if you have a backup in place) or create a new one. See why it’s super-important to back up your WordPress site?

Let’s assume you’ve found your database and you have the correct name in wp-config.php. Now, let us check whether your database username, password, and host are correct.

Check Your Database Username and Password

Checking your username and password involves a little work. You need to create a .php file and store it in your WordPress root folder. This is the same folder where you found the wp-config.php file. Create a new .php file, and name it check.php or whatever you want.

You can create the file using FTP or via something like cPanel File Manager:

Once you create the file, it should appear in your root folder in a moment. Right click on it and choose Code Edit, or select it and click Code Editor on the menu. This should lead you to a blank page. Add the following code:

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

Be sure to replace 'localhost''root' – and 'password' with the database host (this will almost always be the default, localhost), username, and password in wp-config.php respectively. Save the changes and go to the following URL in your web browser – yoursite.com/check.php

Make sure to replace yoursite.com with your actual domain name.

If you see the message, “Connected successfully”, your login details are correct.

If you see “Could not connect: Access denied for user ‘yourusername’@’localhost’ (using password: yourpassword)” or some other error, your database host, username, or password is the culprit here.

If your login details are not working, you need to create new ones and then update your wp-config.php with the new details.

Oftentimes, the easiest option here is to just:

  1. Create a new database user.
  2. Assign that user to your WordPress site’s database.
  3. Update your wp-config.php file to use that new database user.

Aside: Your problem could be as a result of the last credential, DB_HOST. By default, most web hosts use ‘localhost’, but you can try changing this to an IP address or URL as per your web host. You can get a list of hosts and their DB_HOST values on the WordPress Codex. If your host is not on the list, please contact them for this information.

Moving on, let’s create a new user.

Create New Database User and Password

Go back to your cPanel and access MySQL Databases. It is also located under database tools, the same tab you found phpMyAdmin:

Clicking on MySQL Databases should open something like this:

Once the MySQL Databases screen loads, navigate down to where you have MySQL Users. Under this, you will notice Add New User:

Enter a new username and password, and click Create User. Keep note of these details for your wp-config.php file.

Note: Your setup could be different, but the procedure is much the same.

Once you create the new user, you need to assign the user to your  WordPress database. This is the database that is shown after DB_NAME in your wp-config.php file.

To assign the user to your database, go to the MySQL Databases screen once again, and scroll down to where you will see Add User to Database:

Select the user you just created and your database, and click Add. On the next screen, make sure to give the database user all privileges and save those changes:

Now, go back to wp-config.php and update the file with the new username and password information.

Make sure there are no spaces between the quotes. For example, ' your-username ' is bad, but 'your-username' is great. Also, use single quotes as opposed to double quotes. Save all changes you’ve made to your wp-config.php.

Now, try accessing your website. If everything went well, it should be working again.

Are you still getting the error? If that’s a yes, you can try the other fixes as well.

2. Error Message On an Existing Site That You Haven’t Migrated

If you see the Error establishing a database connection message on an existing site where you haven’t made any big changes recently (like migrating to a new host), it can be a little trickier to troubleshoot the problem. However, there are still some steps that you can take.

Make Sure Your Host Isn’t Experiencing Any Issues

Before you dig too deeply into troubleshooting your site, you’ll first want to make sure that the problem isn’t caused by your web host.

Some web hosts configure their environments in such a way that your site’s database server is separate from the file server. In these configurations, it’s possible for the database server to be down but the file server to still be functioning. When that happens, you’ll see the Error establishing a database connection message.

A good host should be monitoring for this and quickly fix the problem. However, it’s worth sending a quick message to support or checking your host’s uptime status just to rule out any issues on their end.

Check for Corrupted Database (and Repair If Needed)

Another problem that can suddenly affect a WordPress site is a corrupted database. Thankfully, it’s pretty easy to test if this is the case.

To test, try to open up your site’s backend admin dashboard.

If your backend dashboard loads fine but the frontend of your site shows the Error establishing a database connection message, you might have a corrupted database.

Or, you also might see a different message like “One or more database tables are unavailable” in the backend, which is another sign of a corrupted database.

On the other hand, if you see the exact same Error establishing a database connection message in both the backend and the frontend, this is not the issue and it’s pointless to continue with this step. Instead, just skip ahead to the next step.

So – how can you fix a corrupted WordPress database?

Thankfully, WordPress includes its own tool to do this – all you need to do is enable it. Here’s the quick version for enabling the WordPress database repair tool – we’ll show you more detailed instructions below:

  1. Access your server’s files by connecting via FTP or using cPanel File Manager (or your host’s custom file manager tool if applicable).
  2. Edit the wp-config.php file.
  3. Add the following code snippet – define ( 'WP_ALLOW_REPAIR' , true);
  4. Visit yoursite.com/wp-admin/maint/repair.php in your browser, making sure to replace yoursite.com with your actual domain name.
  5. Run the database repair tool by clicking Repair Database.
  6. Once the database repair tool finishes, remove the code snippet that you added to the wp-config.php file.

Here’s what it looks like to add the code snippet to your wp-config.php file – add it above the line that says “/* That’s all, stop editing! Happy publishing. */”

define ( 'WP_ALLOW_REPAIR' , true); 

Once you’ve done that, you can go to yoursite.com/wp-admin/maint/repair.php to launch the database repair tool. Click the Repair Database button to start the process and WordPress will handle the rest:

Once the database repair process finishes, your site should hopefully start working again.

If it does, make sure to go back to your wp-config.php file and remove the code snippet that you added earlier. With this code snippet added, anyone can access the database repair tool, even if they’re not logged in to WordPress.

You don’t want that, which is why it’s disabled by default and why you need to make sure to disable it again when you’re finished.

Re-Upload a Clean Copy of WordPress to Fix Corrupted Files

In addition to a corrupted database, another common cause of the Error establishing a database connection message can be corrupted core WordPress files.

Diagnosing the specific problem is tricky, but you’ll find lots of situations where some weird tweak in a certain file is causing problems.

Thankfully, you don’t have to find the specific error. Instead, you can just re-upload a clean copy of the core WordPress software to make sure that all of your core files are exactly how they should be.

Don’t worry – re-uploading the core WordPress software will not overwrite any of your data. It’s basically just reinstalling the WordPress software, which is the same process your site undergoes every time you update WordPress.

However, because you probably can’t access your WordPress dashboard, you’ll need to do this via FTP.

To start, do the following:

  1. Go to the WordPress download page at WordPress.org
  2. Download the latest version of WordPress – it should be a single zip file that’s around 15 MB
  3. Once the download completes, extract the zip file on your computer

Now, you should have a folder that contains all of the core WordPress software.

Open this folder and delete the following files/folders:

  • wp-content folder
  • wp-config-sample.php file
  • wp-config.php file – this file shouldn’t actually be there, but if you do see it for some reason make sure to delete it.

Once you’ve prepped the clean copy of WordPress by deleting those files/folders, you’re ready to upload it to your site:

  1. Connect to your WordPress site’s server using FTP
  2. Upload all of the remaining WordPress files/folders to your root folder. You want everything to match up so that the new files are getting uploaded to the same location as the existing files.
  3. When your FTP program asks you what to do about duplicate files, tell it to overwrite all files with the new version. If you don’t see a warning about duplicate files, make sure that you’re uploading the new files to the correct location. You want to overwrite all the existing core WordPress files on your server.

Once the upload finishes, you should have a clean copy of all your core WordPress files and that will hopefully fix the Error establishing a database connection message.

Nothing Working? Try Restoring a Recent Backup

If nothing has worked so far, your best bet might be to just try restoring your site from your most recent backup.

If you or your host take regular backups of your site, you should be able to restore your site to working condition with little to no data loss.

WordPress Error Establishing a Database Connection FAQs

To finish out this post, let’s go over some common questions you might have about the Error establishing a database connection WordPress problem.

Why is WordPress not recognizing my database?

If WordPress isn’t recognizing your database, you’ll first want to make sure that the database name, user, and password are set properly in your site’s wp-config.php file.

If you have the correct credentials, another problem could be that the database user doesn’t have full permission to access the database. Make sure that you attached the database user to the database in cPanel.

Why is the WordPress error establishing a database connection intermittent?

If the Error establishing a database connection message is intermittent (i.e. it comes and goes), you can be confident that the problem is not incorrect database credentials. Instead, it’s likely that your database hosting server is being overwhelmed and failing during high-load periods.

You can consider reaching out to your host for help or upgrading to more powerful WordPress hosting.

How do I fix a corrupted database in WordPress?

To fix your corrupted database, you can try using WordPress’s built-in database repair tool. You’ll need to activate this tool in your site’s wp-config.php file. Then, you can launch the repair tool by going to yoursite.com/wp-admin/maint/repair.php.

How do I check my database connection in WordPress?

To test your WordPress database connection, you can upload a simple PHP file with a short code snippet to your site. We show you how to do it at the beginning of the troubleshooting steps in this post.

Why am I seeing the error establishing a database connection after migrating my WordPress site?

If you started seeing the Error establishing a database connection WordPress message after migrating your site, the problem is almost certainly an issue with the database name, username, or password that are defined in your site’s wp-config.php file. Make sure that all of these details are correct for the new host and also that the database user has full permission to access the database.

Fix This WordPress Error Today

The WordPress Error establishing a database connection error can be frustrating. However, in almost all situations, your site’s data is still there, you just need to find a way to fix the connection so that WordPress can access the database again.

In this post, we’ve shared some troubleshooting tips to help you solve the problem.

If you recently migrated your site or created a fresh WordPress install, it’s probably an issue with your site’s database credentials. If not, try the other solutions to fix the problem.

We hope this article helped you learn how to fix the “Error Establishing a Database Connection” error in WordPress. For some other tips on troubleshooting WordPress, you may also want to see our article on how WordPress beginners can dodge these 7 common mistakes.

Now you…

Have you experienced and resolved this error before? What caused it, and how did you solve it? Please share your tips and solutions with us, and make this post more resourceful. Thanks in advance!

Colin Newcomer

Colin Newcomer is a WordPress writer for WPKube. He uses WordPress more often than any sane person should.

View Comments

  • Very nicely done, very detailed and logically set. I've had this issue a few times and every time I've been able to figure it out but it's taken time because I'd never written down the process. I certainly should have though, but I'm glad you did. Great job!

    • Thank you Mitch. Such a great and inspirational comment deserves a gold medal. Thanks for the compliment and passing by :)

  • Just keep in mind that it can be a hosting issue as well. If the host computer can't talk to your site/database you will get this error too. We learned this on Black Friday, two years ago. The host server was running out of memory.

  • I notice that mine would do the same on some occasions and would fix itself after a few minutes. I wonder what that is all about! Its great to know that you can fix such issues. Great post Freddy!

    • I am guessing the site is on shared hosting.
      If it does that often then increase php memory limit from 64 to 128 or even as high as 256.

      You can also increase WP Memory limit too.
      Other than that, you've got no issues.

  • I'm getting this error quite frequently - same message on front and back end, and no issues with credentials etc. It seems to happen regularly when I'm adding a lot of posts to my site. I'm hosting on a dedicated server, and when I look at the service monitor in Direct Admin I can see over 100 ID's listed under mysqld - which is a little bit odd because it happens at times of the day when I'm not getting a whole lot of traffic. If I stop MySQL and restart it, the problem goes away, and the number of ID's in the service monitor generally drops down to about a dozen. If I wait about half an hour the problem also goes away. It makes me wonder if Wordpress is somehow establishing connections but then failing to let them go in a timely manner when it's done with them or something like that. Thoughts?

    • Hey Cat,

      You might want to check with your web host, as you might have memory throttling issues. If you're operating with insufficient memory on your server, you're like to experience this error. Let us know how it goes, and thanks for passing by :)

  • hi there
    frequantly i m getting this error message 5-10 min
    "Error establishing a database connection" after then working nice my website load fast i m using little bit plugin also why i m getting this error message frequantly please help me
    i m using godaddy deluxe wordpress hosting plan
    godaddy customer care very poor

    • Hey Nifras

      Thanks for posting your comment. Which plugin are you using? I have experienced this problem myself, and I resolved it by removing the problematic plugin. Let me know. Thanks once again for passing by, your comments are highly appreciated.

  • I am facing the same problem since two weeks. I read all of it and tried.
    What I did is I tried to restart my apache web server, but the command failed, then I tried to restart my mysql server , this command also failed and then I tried to login to mysql but it also failed. I couldn't understand what was happening. I tried these commands thrice and on the third time the mysql restart command worked and the mysql server restarted. After that the error went away and the site started functioning normally.

    The problem I am facing right now is that this thing is happening after every 3 to 4 days. After evry 3 to 4 days I am getting the same error when I check my site.

    How to remove it permanently ? NEED HELP !

    There is no problem with login credentials as I remember them very well.

    I am using Amazon AWS EC2 with uBuntu.

    • Hey Ryan,

      Thank for you passing by. I am sorry you're facing this problem. You will need to contact your web host seeing that the problem keeps recurring. Something could be amiss with your server configuration. Please respond with more details about the error (for instance: did you make any changes i.e. install a plugin, add-on, theme, changed code etc when the error started?) We will be glad to look further into it and help :) Cheers

    • Hello Ryan,

      Please, i´m getting the same problem on my ec2 aws, several times a day...

      All credentials ok, database ok (was well optimized these days ago).

      Service comes back only when restart mysqld service...

      Did you solve your problem?

      Thanks and Regards,

      Tony Franco

        • Hello Shaikh,

          I´m sorry because i don´t remember how i fixed it.. that time a lot of things were giving errors... now i´m not at aws anymore...

          Hope you find the solution!

          Regards,
          Tony

  • Hi Freddy
    Thanks for the detailed steps to fix this. Unfortunately I didn't get the same results you described.
    When I go to "/wp-admin/" I get an EEDC, but it is not the same look or page if I go to the WP index.php. The admin one gives me many more options under the EEDC, telling me the info in the config file is bad.
    Then I went to cpanel and did a DB check and the web hosting site said all was good.
    Then I created the check.php file as you suggested and it didn't tell me i succeeded or failed instead in just displayed the entire code in the file.
    I have not tried the step you talked about repairing the DB.
    Any suggestions as to what I should do next?

    • Hey Brendan,

      Sorry to hear about problem. I think the best option is to contact your host and explain the situation, as I cannot pinpoint the exact issue.

      If you can share the screenshots & site url, I may be able to give you a better answer.

      • Hi Dev Thanks for your interest in trying to fix this

        Here are the Screen shots, sorry you didn’t get them the first time I sent them

        [cid:image001.png@01D1AEE8.71CD9840]

        [cid:image002.png@01D1AEE8.71CD9840]

        [cid:image003.png@01D1AEE8.E42F8420]

        [cid:image004.png@01D1B06F.D9FF9840]

Recent Posts

How to Start an Online Course Using WordPress and LearnDash

Trying to figure out how to start an online course so that you can share…

1 month ago

LearnDash Review 2024: The Best WordPress LMS Plugin?

Considering using LearnDash to create online course content with WordPress? LearnDash is a popular WordPress…

2 months ago

WordPress XML Files: What They Are and How to Open Them

WordPress XML files see a lot of use for me as a content creator, and…

2 months ago

6 Best WordPress LMS Plugins – Detailed Comparison & Review for 2024

If you’re looking for a way to deliver an online course, complete with all the…

2 months ago

The Top 8 SEO Tools And WordPress Plugins For Maximum Visibility (2024)

Search Engine Optimization (SEO) is crucial for any website that wants to maximize its traffic…

2 months ago

Kinsta Hosting Review 2024: Is This WordPress Host Worth the Investment?

Kinsta is a notable brand in the WordPress hosting space. The standout aspect of the…

4 months ago