Advertising disclosure

Hosting Canada is community-supported. We may earn a commission when you make a purchase through one of our links. Read Disclosure.

Error Establishing a Database Connection

errorAs a large portion of professional websites is run on WordPress, many site visitors and owners have seen the message “Error establishing a database connection”.

The message comes with a plain white page—no website behind it or anything.

If you’re not savvy with WordPress—a blogging and CMS system— or programming, you likely don’t know what this means or if it’s a problem that will take a lot of time to fix. Furthermore, this error does not mean that permanent damage has been done to your site.  

Take a Look at Our Guide:

What Does the Error Mean?

question iconTo understand what the error means, you need to understand how your WordPress website works. All of the data on your site—photos, blogs, videos, metadata, etc.—is stored in a MySQL database.

A database is essentially a large store of information that powers your website. When someone visits your website, and they search for something, PHP code runs that tells the browser to search for the desired content in the database to then display on the browser.

Sometimes this process doesn’t go smoothly—there can be an error in “talking” to the database. Essentially, the browser is trying to reach the database through PHP commands, but it is having trouble finding the database.

When the browser cannot connect to the database, for one reason or another, the information needed to create the webpage you’re searching for will not be found either.

Overall, this WordPress error is quite common, and the fixing is pretty straightforward.

Additionally, if you’re trying to access the WordPress dashboard, you won’t be able to while the error exists.

Causes of the Error

Several factors may cause the error in connecting to a database. The most common reasons include all the following:

Login Credentials 📝

To access the database associated with your WordPress site, there must be specific credentials. These credentials are already stored elsewhere, so you and anyone else can access your website without entering credentials into the database. However, if these credentials have changed, WordPress will no longer be able to access the database.

Web Server Down ⬇️

A web server is a system that receives HTTP requests and serves those requests. As a WordPress site owner, you may have chosen your own web host, which is an example of a web server. While getting into the weeds of servers can get too complicated, all you need to know is that web servers are what provide people with web pages.

However, sometimes these web servers go down; that’s to say that there may be times where the web server fails to operate and serve web pages. The best WordPress web hosts will have a high uptime which means they do not go down often. Nonetheless, they are not perfect, and they can fail.

Corrupt Database ❌

Another reason that you receive the error establishing a database connection could be that the database itself is corrupted. This may occur as the result of an internal issue from a plugin, a theme failing, or something else. 

Corrupt WordPress Files  🛑

Just like a database can be corrupted, WordPress files themselves can be corrupted as well. There may have been an update that failed to download properly causing the failure of a theme or a plugin. Even WordPress itself may try to update its underlying software and fail.

These scenarios could all cause a database connection failure.

Too Much Site Traffic 🚗

The last reason you could experience an error in connecting to your database is that there simply may be too much traffic to your website. Websites can experience spikes in visitors which means there is a rush of requests to your database.

If the database is not adept at handling that number of requests, it can fail.  

Fixing the Error Establishing a Database Connection

Now that you know what can cause this error, you’ll be able to better understand how to fix the issue. You can take the following steps to fix your error.

Step 1: Check Your Login Credentials ✏️

Theoretically, your login credentials shouldn’t change, yet they still do sometimes. For example, if you’ve changed hosting providers, your credentials may change. Luckily, the fix is relatively straightforward.

login credentials

First, you need to find your “wp-config.php” file and then find the following lines

define(‘DB_NAME’, ‘some_db’);

define(‘DB_USER’, ‘some_username’);

define(‘DB_PASSWORD’, ‘some_password’);

define(‘DB_HOST’, ‘localhost’);

Once you’ve found this information, the easiest fix is to copy all four lines and contact your web hosting provider. Assuming your provider has a live chat option, you can simply paste the copied lines in the chat and ask if they are correct.

If they are incorrect credentials, you’ll receive the correct ones. From there, replace the old credentials with those that you have just received from your hosting provider and upload the file back to WordPress using FTP.

Step 2: Talk with Your Web Host Provider 📞

customer support hostingerSimilar to the previous step, contacting your web host provider can always help resolve your database problem. If your site was working earlier in the day or the day before, and you haven’t changed anything on your part, there’s a good chance the issue lies with the hosting provider.

Talk to your provider by chat or phone and tell them what’s happening. They may be able to tell you right away if there was a problem, or they need to take some time to figure it out.

The hosting company may tell you that the problem came from a surge in site visitors. In this case, there may not be anything to do except waiting.

If they do not find any other issues on their side, they’ll tell you they cannot help. In this case, move on to the next steps!

Step 3: Check If Your Database is Corrupted ❎

While it may not happen often, your database can be corrupted. The first thing you need to do is go to your WordPress dashboard by going here:

If you see “One or more database tables are unavailable. The database may need to be repaired” that means your database is corrupted, and it needs fixing.

Luckily, WordPress has made this easy. Find your wp-config.php file and go to the very bottom of the file. On a line at the bottom, add the following:

define('WP_ALLOW_REPAIR', true);

Once added, saved, and re-uploaded to WordPress, go to this domain:

You’ll have two options—repair the database or repair and optimize the database. It’s probably best to select the option that is repair only.

Once complete, make sure to go back to your wp-config.php file and delete the line you previously added.

Step 4: Check If You Have Corrupted Files 📂

If the problem is persisting, the reason is likely corrupted files. This can occur due to an issue with a file transfer, someone gaining access to your files and changing them, or another reason. Before taking any action to fix the corrupted files, take a backup of your WordPress site.

The goal of this step is to replace the core WordPress files; you won’t replace any themes, media, or similar files.

To replace these files, you’ll need to download the latest version of WordPress. Unzip the file you downloaded. Find the wp-content folder and delete it. Find the wp-config-sample.php file and delete it as well.

Now that you have a clean set of WordPress files, you can reupload this to your site via SFTP. All this does is replace the prior WordPress files and clear out any of the corrupted files you had.

Once done, you should clear your cache and check if your site is working now.

The Bottom Line

WordPress is a powerful CMS platform, yet just like any other website, it is susceptible to failures. One of the most common is an error in connecting to the database. In the future, when you receive this error, you’ll know the steps you can take to fix your site

You Might Also Like: