Error Establishing a Database Connection? 7 Ways To Fix It Fast!

fix the error establishing a database connection

The white screen of death!

Have you tried loading up your website only to see a blank screen with the dreaded message ‘Error establishing a database connection’?

Error Establishing a Database Connection example

Admit it. Your heart just skipped a beat!

We’ve been there and know that exact same feeling.

Errors such as these, where all you see is a blank screen, look pretty scary. As a WordPress website owner, you know that you’ve got a crazy day ahead to get your site back up and running.

Fortunately, it’s not as bad as it seems.

Error establishing a database connection is one of the most common errors you can encounter in WordPress.

The reason for this is pretty straightforward. It’s when your website fails to communicate with your database due to incorrect login details, corrupted databases, and a few other issues.

This error brings down your entire website and has to be rectified immediately. The more it’s delayed, the more it will negatively impact your website traffic, sales, and user experience.

In this article, we’ll cover everything you need to know about fixing the error establishing a database connection in WordPress. From how it occurs to getting it fixed.

Let’s dive in.

What Is the Error Establishing a Database Connection Issue and What Causes It?

To better understand what the error establishing a database connection issue is, it’s essential to understand how WordPress works.

WordPress is a CMS (content management system) built using PHP, a programming language PHP, and MySQL, a database.

While all the functions on your WordPress site are created using PHP, all the data on your site, such as content, images, video, and media files, are stored in the MySQL database.

When a page request is made, PHP pulls the page content from the database and displays it in your browser. That will include all content, plugin information, images, videos, audio, and almost everything you see on the page

The next time you type your WordPress website URL in the address bar, you now know where the data is being pulled out from and how it all works!

So, when you see the dreaded ‘Error establishing a database connection’ message, you know that PHP can’t access your database for some reason and cannot show the page to the user.

Here are some common reasons why we encounter an error establishing a database connection in WordPress:

  • Incorrect login credentials: WordPress accesses your database with a specific login and password. If the credentials were changed or corrupted, WordPress will not be able to access the database and fetch anything from it.
  • Corrupted database: With so many elements within your WordPress site, such as themes, plugins, addition, and users, there are chances that the database can get corrupted.
  • Corrupted WordPress files: Like how the database gets corrupted, your core WordPress files can also get corrupted. This could result from a failed core WordPress update and an update failure in plugins and themes.
  • Database server down: In some cases, your website files are hosted on a machine that’s physically separate from your database. If the server running the database or the connection to it is down for whatever reason, you end up with an error.
  • Too much traffic: While this is an excellent problem to have, it could turn out to be a problem. If your web server is not up to the sudden spike in traffic, your database could become unresponsive.

Fix the Error Establishing a Database Connection

Ok, first things first.

We recommend you backup your WordPress site before you start troubleshooting. Many fixes involve accessing your database, so you don’t want to make things worse.

You could use a popular WordPress backup plugin such as UpdraftPlus to backup both your files and your database.

We have a step-by-step guide on how to back up your WordPress site that you should check out.

Here are the 7 ways to fix the Error Establishing a Database Connection

  • Check Your Database Login Credentials
  • Creating a Database User in WordPress
  • Repairing a Corrupt Database
  • Fix Corrupt Files
  • Replacing Core WordPress Files
  • Check With Your Hosting Provider
  • Restore to Your Latest Backup

Check Your Database Login Credentials

What do you do when you can’t log into your email account?

You’d probably check if you’ve accidentally got the caps lock on, misspelled the login credentials, or as a final option, you would click ‘Forgot Password’.

On similar lines, the first thing when you encounter an error establishing a database connection in WordPress is to ensure that your database login credentials are correct.

This is the most common reason for this error to occur.

Your database credentials are required to connect to your WordPress website. You can access these connection details from the wp-config.php file.

To have a seamless connection with your database, you need four primary pieces of information:

MySQL database name
define(‘DB_NAME’, ‘name’);

MySQL database username
define(‘DB_USER’, ‘username’);

MySQL database password
define(‘DB_PASSWORD’, ‘password’);

MySQL hostname (server)
define(‘DB_HOST’, ‘localhost’);

You can access the wp-config.php file either from the file manager within your cPanel or your site’s root folder using FTP.

Access WordPress wp cofig file using file manager

If you are accessing the file from the file manager, right-click on the wp-config.php file and click on edit.

update wp config file

You will notice the database details in the wp-config.php file.

At this point, all we need to do is check if the credentials in your wp-config.php file and those stored in your database manager are the same.

Let’s first check if you have the correct database name as in the wp-config.php file.

Head over to the phpMyAdmin from your hosting cPanel.

For most hosting accounts, phpMyAdmin will usually be located under the database tools tab.

For instance, in our SiteGround hosting account, phpMyAdmin is under Site > MySQL > MySQL Manager.

SiteGround mysql manager

Once you’ve logged into phpMyAdmin, click on the Databases tab. This will query a list of all your databases. Ignore the ‘information_schema’ database as the host uses this.

Can you see the database that appears after the DB_NAME in your wp-config.php file? Does it match the entry in wp-config.php?

WordPress database using phpmyadmin

If so, then this isn’t the problem.

If you can’t find the database, you have two options.

First, you could contact your hosting service provider as the problem could be from their end. The second option is to restore the database from your last backup or create a new one. Now you see the importance of taking regular backups!

Assuming you have the correct database. Now what?

We will need to check if your database credentials, i.e. the username, password and hostname are correct.

This involves a little more work.

You’ll have to create a new .php file and save it in the root folder of your WordPress site. Name your file test.php or anything you like.

The simplest way is to head to your file manager, select the root folder and click New File on the menu.

Add new file using file manager

Now, you should see your new file created in the root folder.

edit file in file manager

Right click on it and choose Edit. You’ll be led to a blank page. Add the following piece of code:

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

Replace ‘host,’ ‘user,’ and ‘password’ with the database host, username and password from your wp-config.php file.

Save the changes once you are done. Now type out the URL yourdomain.com/test.php. Change ‘yourdomain’ to your URL.

If you see a ‘Connected successfully’ message on the screen, your login credentials aren’t the problem behind the error establishing a database connection.

However, if you get an error message such as ‘Could not connect: Access denied for user yourusername@localhost,’ you know you have just found the reason behind the database connection error.

In such cases where the login credentials aren’t working, you’ll need to create new ones and then update your wp-config.php file.

Creating these is really simple.

Creating a Database User in WordPress

Head back to your cPanel and access MySQL Databases.

For instance, in SiteGround, you can create a new user from Site > MySQL > MySQL Manager.

Create user in SiteGround

All you’ve to do then is click on Create User.

New user created message

The username and the password will be displayed. You could choose to edit it now or later.

Once you’ve created the user, click on Manage Access. We need to assign a database to this user and grant the required permissions.

Add database for user

Select the database to be assigned to this user from the dropdown list. Then set the user privileges. Click Confirm once you are done.

Make sure the database privileges are sufficient for the task you want that user to perform.

Now that we’ve created a new username and password, we need to update our wp-config.php file.

Head back to the file manager and update the wp-config.php file with the new login details. Save all the changes once you are done.

And now, for the moment of truth. Check if your website is loading correctly.

If it’s working correctly, you’ve just identified the problem and fixed the error establishing a database connection.

If it isn’t working right, we need to consider other troubleshooting steps.

Repairing a Corrupt Database

If the database credentials were all good, we should check if the database is corrupted. This could happen due to reasons such as a server crash, a possible hardware failure, a bug in MySQL, or something else entirely.

Testing for a corrupted database is very straightforward.

To begin, navigate to the login page of your WordPress site, i.e., yourdomain.com/wp-admin.

If both your website front-end (yourdomain.com) and the back-end (yourdomain.com/wp-admin) throws up the error establishing a database connection error, then your database is perfectly fine. Your database isn’t corrupted and you can skip to the next fix.

However, if you’re getting an error similar to ‘Database tables unavailable,’ your database is corrupted and needs immediate repair. As mentioned earlier, solving this is really simple.

Your first step is to locate the wp-config.php file. As covered in our previous troubleshooting method, the wp-config.php file is in the root directory of your WordPress website.

From the hosting control panel, access your file manager. You will find the wp-config.php file inside the default home or the ‘public_html’ folder.

Access WordPress wp cofig file using file manager

Once you’ve located the file, right-click and select the Edit option. This opens wp-config.php in a new tab. Insert the line of code at the bottom of the file:

define ( 'WP_ALLOW_REPAIR' , true);

Save the changes once you are done.

The above code helps repair your database without you having to log into the phpMyAdmin manually.

The next step is to navigate to yourdomain.com/wp-admin/maint/repair.php.

If you didn’t add the above line of code, you would find the following screen,

WordPress repair

Because you added the code, you should see the following screen,

WordPress repair database

The final step is to click on either ‘Repair Database‘ or ‘Repair and Optimize Database.’

Once selected, WordPress does its job and repairs the database.

You must remove the line of code you just added to your wp-config.php file once you are done. Adding this line could grant hackers access to your database. We don’t want that now, do we?

Go back to your browser and check if your website is loading.

If your website is back up and running, then the corrupt database was the culprit.

If this didn’t solve the error establishing a database connection, let’s consider an alternative troubleshooting method.

Fix Corrupt Files

Your WordPress files can also be corrupted.

For instance, you may have installed or updated a theme or plugin. You may have edited some of the core files too. If you’ve carried out any of these, files may have been partially overwritten, incorrectly copied or something else.

If you haven’t added, removed or modified files or plugins, you could skip this step and look at the other troubleshooting methods.

If it’s due to a plugin or theme that you’ve just installed or updated, the simplest way is to undo those changes or delete the plugin. However, considering you’ve encountered the establishing database connection error, how can you undo or delete the plugin?

That’s precisely what we will cover now.

You will need to access the folders in your live site. This can be done by using an FTP client such as FileZilla or the File Manager from your control panel.

Connect to your server and navigate to the root folder. It’s where all the files and folders of your WordPress site are stored. It’s usually under the folder called ‘public_html.’

WordPress public html file

Once you’ve located the ‘public_html’ folder, head into the folder and locate the ‘plugins’ folder.

WordPress plugins folder

Rename the ‘plugins’ folder. You could name it ‘plugins_1’ or anything else.

Now, head back to your website and reload to check if the site is up and running. If the error establishing a database connection error is not there, you’ve identified the culprit.

Now, it’s one of those times when you wished you didn’t have to install so many plugins. The reason being, you’ll need to identify which specific plugin is the troublemaker.

To do this, you’ll need to create a new plugins folder and move each plugin one at a time. Check if the website is back up and running between each move.

When you move a plugin back to the proper folder and you see the database error message again, move that plugin back and retest. If your website loads, it’s that plugin.

Once you’ve narrowed it down to an individual plugin, you could either delete it or look at online forums or contact the plugin support team for solutions. Chances are that others may have encountered similar problems, and the solution may be documented.

If the error still exists, repeat the same process, this time with the ‘themes’ folder.

If this doesn’t help, the error establishing a database connection in WordPress could be caused by something else.

Replacing Core WordPress Files

For this, we could try reinstalling the core WordPress files.

Whether it is caused due to an issue while transferring the core files using FTP, a rogue plugin or theme, your core files can get corrupted. The simplest solution for this is to replace the core WordPress version on your site.

In this method, we will be replacing the core files of our WordPress site. We will not be touching the plugins, themes, or media. Only the core WordPress installation.

For this, head over to WordPress.org and download a fresh copy of WordPress.

get WordPress

You’ll need to be careful with this method. A small mistake, and you could wipe out your entire site. As mentioned early on, we highly recommend you backup your site before proceeding.

Once you’ve downloaded WordPress, unzip the folder on your local computer. Then delete the wp-config.php file and the wp-content folder.

Deleting those files means this process doesn’t overwrite your existing files and folders. That way, you retain your themes, plugins, media files, and above all, the wp-config.php file that has all your website configuration details!

Once you’ve completed all the above steps, upload the remaining files to your website root folder. You could do so using either the FTP client or the File Manager. This should replace all the existing files.

Now, clear your browser cache, cross your fingers (if you want to), and reload the page.

If the problem is solved, we know exactly what went wrong. If it hasn’t, on to the next troubleshooting step.

Check With Your Hosting Provider

If you haven’t worked on your WordPress site for a while now, and if your site was perfectly fine a while ago, the chances are that something went wrong at your server end.

Fortunately, most hosting providers offer a quick turnaround for your support queries.

In fact, this could be your primary go-to solution as soon as you encounter the error establishing a database connection.

Mention that you are experiencing an error establishing a database connection and check if there are any database server errors, explain your situation.

In most cases, the support team will also help fix the error for you.

Restore to Your Latest Backup

Another troubleshooting method that you can use is by restoring to an earlier backup of your WordPress site.

You can revert to an earlier version and then update the site for any changes that you may have carried out.

Backups can be carried out using popular WordPress plugins such as Updraft Plus, BlogVault, and many more.

Here’s a step-by-step guide on how to back up your WordPress site.

Most hosting service providers maintain a backup of your site too. You could check with them for the latest backup of your website if you don’t have your own.

Have You Faced the Error Establishing Database Connection in WordPress?

If you’re a WordPress website owner, you will encounter a wide range of challenges from time to time. It’s all part of running a website.

One such challenge is finding the right solution for the common error of establishing a database connection in WordPress.

It’s a process of elimination and of working the problem methodically and carefully. If you follow our guide, you should have all the information you need to effectively fix the error without damaging anything else!

We hope this article helped you understand what the error is all about, how it happens, and most importantly, know that it’s not as severe as it seems.

There’s more than one way to go about fixing the error establishing a database connection in your WordPress site and we have covered them all

Recommended Articles:

Over to you now…

Have you experienced this error before? Any idea what caused it? And how did you solve it? Do share your tips and solutions with us.

Disclosure: This blog may contain affiliate links. If you make a purchase through one of these links, we may receive a small commission. Read disclosure. Rest assured that we only recommend products that we have personally used and believe will add value to our readers. Thanks for your support!

2 comments
Leave a Comment

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

Astra is Free. Now & Forever.

We believe creating beautiful websites should not be expensive. That's why Astra is free for everyone. Get started for free and extend with affordable packages.

REWIND

YEAR IN REVIEW

Download is Just A Click Away!

Enter your email address and be the first to learn about updates and new features.

Download Free Astra Theme - Modal Popup Form
Scroll to Top