Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

WordPress Error Establishing a Database Connection

Last updated on:
WordPress Error Establishing a Database Connection

WordPress is a powerful content management system (CMS) by all means, but it comes with its fair share of debilitating errors.

Just like any other human-made system, it’s prone to errors that can result from many different things. Perhaps you installed a faulty theme or plugin and messed up your install.

Maybe a natural disaster struck and took out your servers. The recent update you made might also knock out your website, or maybe your host doesn’t care and is sleeping on the job.

Perhaps you overestimated your tech chops and went tinkering about and deleted something important unknowingly. Or maybe an innocent typo or extra space in one of your files is causing you undue stress.

Anything can cause errors on your beloved WordPress website without warning. It’s inevitable and happens to the best of us.

That said, one of the most confusing WordPress errors is the infamous “Error establishing a database connection” error. It’s a disconcerting error since it doesn’t tell you what went wrong.

All you see when you visit your WordPress site is:

wordpress error establishing a database connection error

Yeah, you don’t have much info with which to work.

Well, to be fair, it somewhat tells you what went wrong – your site cannot establish a database connection.

Worse still, many things can cause the error, which throws just about anyone a curveball, whether you’re a beginner or an experienced WordPress user.

In today’s post, we show you exactly how to resolve this error, so you can restore your site and go on with business as usual. And please share your thoughts, solutions or express your concerns in the comment section at the end, and we will be more than thrilled to help.

Without further ado, let us set sail because there is a lot to learn.

What is the “Error Establishing a Database Connection” Error?

It will make absolutely no sense to proceed if we don’t know what kind of error we are facing. So, ladies and gents, what’s the “error establishing a database connection” error?

WordPress consists of two main parts. In the background, you have a MySQL (or MariaDB) database that stores your content, be it posts, log-in details, comments, site options, and so on.

WordPress uses commands written in a programming language known as PHP to retrieve the content from the database and create the page you see in your browser.

Now, the error occurs when PHP cannot retrieve content from your database due to one reason or another.

What about your images, videos, and other media? Well, this kind of content is not stored in your database. It’s saved in your wp-uploads folder. Whenever an image is needed as part of a page/post, PHP kicks in and retrieves that particular image.

In other words, WordPress is simply a system that retrieves content from a database and other folders and brings everything together using PHP to build the pages you see in your browser.

If the connection to the database fails, you end up with the “Error establishing a database connection” error and no website to show.

It is a nasty error that impacts your conversions, revenue, and brand reputation negatively. It’s precisely why you should fix it immediately.

In the following section, we look at several causes of the notorious “Error establishing a database connection” error. Only then can you diagnose what’s ailing your site and fix things without wasting time.

Why the “Error Establishing a Database Connection” Error Might Happen

We’ve already established that this common WordPress error happens when your site cannot connect to the database.

But what kills the connection? Why does it happen? I mean, your site was working perfectly yesterday, and then boom – an error.

It’s baffling, but panic not because here are some of the common reasons why you’re seeing Error establishing a database connection when you visit your WordPress site.

Inaccurate Database Credentials

Having incorrect database credentials is one of the most common causes of the “error establishing a database connection” error.

Typos, wrong quotes, and spaces in your database username or password will leave you with the “EEDC” error. And that’s all because you (read, your website) must access the database securely to retrieve content.

If the credentials in your wp-config.php (more about this in a minute) file don’t match those in your database, the database won’t let any connection through, which results in the error.

Usually, confirming you have the right database credentials in your wp-config.php file is the first thing you need to do.

Your Database Server is Down

Unknown to many, WordPress runs on multiple servers, commonly known as a WordPress Server Stack. If any of the servers go down, you will have some error, obviously.

The server receives requests from the browser and relays the requests to various servers and services. Behind it, you have an HTTP Cache server that ensures that repetitive requests don’t execute the same PHP commands over and over, saving you page load time.

Also, you have a web server where your images, videos, website files, and other media are stored. And then you have the database server, which – as you guessed it – hosts your database.

If the database server goes down due to a traffic surge or many concurrent connections, you’ll experience the “error establishing a database connection” error.

Now, you can’t do anything when your database server goes down because that’s your host’s responsibility.

However, you can decrease the likelihood of your database server going down by investing in better WordPress hosting.

If you’re on shared hosting and your website starts generating high volumes of traffic, you’re likely to experience the “error establishing a database connection” error.

If your website becomes popular, it’s advisable to upgrade your hosting. Consider managed WordPress hosting or cloud hosting for seamless scalability when you need it.

Corrupted WordPress Files

Corrupted files can cause the dreaded “Error establishing a database connection” error. A corrupted file could be from the WordPress core, but it is easy to diagnose with the right instructions.

At times, a well-intended update doesn’t execute successfully, and it’s nobody’s fault. Perhaps, there was a blackout during the update, or something else happened.

Maybe you added some code to your footer.php file to get rid of the Powered by WordPress credit, and now things have gone haywire.

Perhaps you dug into your WordPress code and tried to add some custom functionality but left out a semi-colon or something else.

The point is, your error could be the result of corrupted WordPress files. However, don’t worry since fixing corrupted WordPress files is as easy as A, B, C.

A Corrupted Database

A corrupted database won’t connect to your WordPress website no matter what you try.  If it’s corrupted, you will see “Error establishing a database connection” every time you load your website.

Remember, your database stores all the vital information PHP needs to build the web page you see in the web browser.

If the database is corrupted, there is no way to retrieve the needed information, which leaves you with the “Error establishing a database connection” error.

The only way to fix the error is by repairing the database or restoring a working version from a previous backup.

Keep in mind that many things can corrupt your database. Some plugins and themes can leave your database short of tables. Data transfer interruptions might render your database useless.

You might even delete database tables by accident, especially if you don’t know your way around servers. I have done it severally but nowadays, I treat each important database like a fragile egg. And I keep regular backups.

So what now?

There are some of the common causes of the perplexing “Error establishing a database connection” error. If you know any other causes, please share in the comments, and I will update the article with your suggestions.

Moving on swiftly, let us now fix that error that’s been causing you a lot of undue stress!

How to Fix the “Error Establishing a Database Connection” Error

Basically, the problem is your website is not connecting to your database. For this reason, we shall approach the problem from the top. First, we shall check your database and move on from there.

1. Corrupted Database

To begin, let us check if your error is the result of a corrupted database. If your database is corrupted, it would be a fruitless endeavor to try all the other solutions.

Don’t sweat; checking if your WordPress database is corrupted is easy peasy work. Navigate to yousite.com/wp-admin/, and don’t forget to replace yoursite.com with your domain name.

If you get the “Error establishing a database connection” error, your database is as healthy as it can be. However, if you get a different error such as “One or more database tables are unavailable…,” your database is broken.

If yoursite.com/wp-admin shows a different error, your corrupted database is the problem. Don’t worry, however, because repairing your database is simple.

What to do?

Log in to your cPanel and click File Manager:

Things might look different from where you sit, but the procedure is essentially the same.

Once you’re in your File Manager, navigate to the root directory of your WordPress site.

Aside: The root directory is typically named public_html, www, Home or after your website e.g., yoursite.com (that is, if you have multiple WordPress websites on the same server).

Inside your WordPress root directory should look something like this if you’re using cPanel (I’m working with Namecheap shared hosting for the article):

Here, you want to locate the wp-config.php file. Next, right-click wp-config.php, and click Edit as shown below.

error establishing a database connection

Doing so opens the wp-config.php file in a new tab. At the very bottom, add the following code and save your changes.

define ( 'WP_ALLOW_REPAIR' , true);

At the very bottom:

With that in place, point your browser to yoursite.com/wp-admin/maint/repair.php, and don’t’ forget to replace yousite.com with your domain name.

If you followed every step carefully, you’d land here:

On the above page, you can either choose to Repair Database or Repair and Optimize Database. Your call – repair the damn database.

WordPress will kick in and fix the database for you. You don’t have to do anything else.

N/B: After the repairs are complete, eliminate the “define(‘WP_ALLOW_REPAIR’, true);” line from wp-config.php to prevent unauthorized users from using the page. Errm, unauthorized users = attackers.

Are you still seeing “Error establishing a database connection?”

If not, well and good. You can go back to business as usual. If the problem persists, perhaps your database credentials are inaccurate.

2. Check Your Database Credentials

To establish a connection with the database, your WordPress site must provide accurate log-in details via the wp-config.php file. If that doesn’t happen, you’ll see “Error establishing a database connection.”

Open your wp-config.php file and locate this part:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'vistxxx_wpxxx' );

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

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

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

Your database credentials live in this part of the wp-config.php. Here, you can see and edit your database name, username, password, and hostname.

If any of the above credentials don’t match those stored in your database, you will get the “error establishing a database connection” error.

Let’s check what you have.

Firstly, let us check if you have the correct database name in your wp-config.php file. Log in to your cPanel and navigate to phpMyAdmin:

After logging in, click the Database tab to see available databases:

From the list, can you see the database name that is in your wp-config.php file? If that’s a yes, you’re good to go. If you can’t see your database, there is a serious cause for concern, and you should talk to your host immediately. Alternatively, you can try restoring the database from a backup.

If you found your database name, let us check if your database username, password, and hostname are accurate.

3. Check Database Username, Password, and Hostname

For the following section, you will create a .PHP file that you will save in your WordPress root directory (you know, the same folder where you found the wpconfig.php file).

Open your favorite code editor (I love Notepad++), and add the following code into a new file:

<?php
$test = mysqli_connect('localhost', 'db_user', 'db_password');
if (!$test) {
die('MySQL Error: ' . mysqli_error());
}
echo 'Database connection is working properly!';
mysqli_close($testConnection);

Please save the file as check.php or anything you want; make sure it’s a PHP file. Also, ensure you replace localhostdb_user, and db_password with the respective values in your wp-config.php file.

Save and upload the above check.php file to your WordPress root directory.

And upload…

You can use FTP (or SFTP) if that’s what you like, but the File Manager should suffice for this part.

Next, point your browser to yoursite.com/check.php. Your database credentials are correct if you get “Database connection is working properly!”

If you get a different error message, such as “Could not connect: Access denied for user ‘yourusername’@’localhost’ (using password: yourpassword),” you need to create new database credentials and update the wp-config.php file accordingly.

Pro Tip: Your error could be stemming from the last credential DB_HOST. Most web hosting companies use localhost by default, but you can try changing that to an IP address (127.0.0.1) or a URL, depending on your web host. The WordPress Codex offers you a list of possible DB_HOST values for several web hosts. Can’t find your web host on the list? Contact them for the information.

If your username and password are not working, it’s time to create new ones, which is incredibly easy a task.

What to do?

4. Creating New Username and Password

Log in to your cPanel and click MySQL Databases, as shown below.

wordpress error establishing a database connection

On the Databases page, scroll down to MySQL Users. Under Add New User, enter a new username and password and click the Create User button:

Keep note of the new credentials because you’ll add them to your wp-config.php file later.

After that, you must assign the new user you created to your WordPress database. This is the database that is shown after DB_NAME in your wp-config.php file.

To do that, go back to the MySQL Database page and scroll down to Add User To Database. Choose the new user you created, your WordPress database, and click Add, as we highlight below.

On the next page, select All Privileges and click Make Changes at the bottom of the page.

Now that you’ve added a new user to your WordPress database, it’s time to update your wp-config.php file with the new credentials.

Go to your wp-config.php file inside File Manager and update it with the new username and password. Ensure there are no spaces between the quotes. For instance, ‘username’ is good but ‘ username ‘ is bad. Additionally, use single quotes as opposed to double quotes. Save your changes and refresh your website.

Is the error gone? If so, you’re back in business. If you’re still seeing the error, the problem could be corrupted WordPress files.

5. Fix Corrupted WordPress Files

Many users get the “Error establishing a database connection” error due to corrupted WordPress files. If you added some files to your website and got the error, eliminate the problematic files, and you’re good to go.

However, it is hard to know exactly which files are corrupted, especially as a beginner. Don’t worry, all the same, because replacing corrupted WordPress files is simple.

At the same time, you need to be extra vigilant because a tiny mistake here can wipe out your entire website. As such, make sure you back up your entire website, i.e., files and database, before proceeding.

After that, download a fresh copy of WordPress from WordPress.org. Then unzip the WordPress package on your computer and delete the wp-content folder as well as the wp-config-sample.php file. This ensures you don’t overwrite your themes, plugins, and the existing wp-config.php file.

Next, upload the remaining files to your WordPress root directory using an FTP program such as FileZilla. Doing so replaces all problematic WordPress core files.

After that, clear your browser cache and try reloading your website. If all goes well, you won’t have the “Error establishing a database connection” error anymore.

If the error results from a problematic plugin (which is unlikely, but still possible)…

Open your File Manager and access your wp-content folder. Inside the folder, you should see the plugins folder, among other things.

Next, right-click the plugins folder and rename it to plugins_old:

Renaming your plugins folder deactivates all plugins on your website. Now, try refreshing your website. Is the error gone? If so, then one of your WordPress plugins is the problem.

To isolate the problematic plugin, log in to your WordPress admin and activate one plugin at a time, each time checking if the error reappears.

Once you locate the plugin that’s causing the error, delete it, find an alternative, and let the developer know. Rename plugins_old back to plugins and pat yourself on the back.

If the error persists after trying the above solutions, please let us know in the comments, and we will come to your aid. Additionally, don’t forget to clear your browser cache as the error could be a false positive caught in your cache. Also, don’t hesitate to contact your web host because the problem could be on their end.

Final Words

When all is said and done, you shouldn’t stress when you encounter the dreaded “Error establishing a database connection” error. As we’ve learned here today, it’s relatively easy to solve, plus your host is always happy to help.

We are always glad to help as well, so don’t hesitate to ask away in the comment section. Perhaps we won’t solve the error right away if the problem is with your host, but we can help you fix the other issues quickly.

We hope today’s guide helped you fix the horror that is “Error establishing a database connection.”

Article by Freddy WPExplorer.com guest author
Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

1 comment

  1. Robert Gavila

    When setting up a localhost system on your own computer, I would add two points:

    First, add the WITH mysql_native_password parameter to your user’s sign on in MySQL:
    CREATE USER ‘username’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘user password’;

    Second, do not use ‘localhost’ for the host. Use 127.0.0.1 for the host.

    Thank you.

Leave a Reply

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

Learn how your comment data is processed by viewing our privacy policy here.