Article read time - 6 minutes

Although WordPress is inherently stable and secure, there are several errors you’ll come across while running a site. Fortunately, most – including WordPress’ Error Establishing a Database Connection’ – have clear error messages. This means you’re able to immediately get to work fixing your site.

In addition to the clear error message, the steps to resolve the issue don’t need too much technical knowledge. If you can open a text editor and browse a directory, you’re in good shape.

This post will run through WordPress’ Error Establishing a Database Connection and show you how to fix it. Before that, let’s talk about where the error comes from.

Explaining WordPress’ Error Establishing a Database Connection

WordPress’ Error Establishing a Database Connection appears seemingly ‘out of the blue’. It’s anxiety-inducing because it shows up on the front end of your site – for you and your users.

However, compared to errors such as the ‘White Screen of Death’ (WSoD), this error gives you plenty of clues as to what’s happened. In a nutshell, WordPress gets practically every piece of information from a connected database. Without this, a site isn’t able to load. Therefore, the error is telling you that wherever your database gets its connection details needs some maintenance.

Usually, the cause is due to incorrect login credentials for your database. However, there can be other reasons too. Regardless, you’ll need to poke around in your WordPress core files.

What You’ll Need to Resolve WordPress’ Error Establishing a Database Connection

Before you get to work, there are a few tools you’ll need, as well as some sharp skills. Here’s what we recommend:

  • A Secure File Transfer Protocol Client (SFTP). A tool such as Cyberduck or FileZilla lets you access your server through a Graphical User Interface (GUI).
  • Your SFTP login credentials. Of course, you’ll need to access your server with the right credentials. These are usually found within your hosting control panel.
  • The skills necessary to use SFTP. If you need to tune up your skills, the official WordPress website has a great primer on doing so.
  • A suitable text editor. Any text editor will do, but an app such as Atom will present the code more clearly and be an all-around pleasant experience.
  • Confirmation that your database server is working. This can be as simple as checking a dedicated status page or potentially getting onto your host’s live chat support.

Also, it’s a good idea to see if you have a current clean backup to hand. If not, you, unfortunately, don’t have a safety net if nothing else works. Once your site gets back online, your first task to make sure you regularly back it up!

How to Fix WordPress’ Error Establishing a Database Connection (In 3 Steps)

Once you have your tools and skills, it’s time to get to work! The steps below are linear, although if you’re 100 percent sure the issue is not related to a particular element of your site, feel free to skip to later steps.

1. Check Your Database Credentials and Host Name

Wrong database credentials are the most common reason for WordPress’ Error Establishing a Database Connection. As such, it’s the first step you need to take.

To begin, open your SFTP client and log into your server. Once it’s connected, you’ll see a list of your site’s files in the public_html or www directories or an explicitly-named directory.

website files

Next, hunt around the root directory for your wp-config.php file – the place to find your WordPress configuration settings. Once you’ve found it, open it in your text editor and scroll to the MySQL settings section (it’s usually around line 20 in a ‘clean’ version of the file):

wp-config

Note the DB_NAMEDB_USERDB_PASSWORD, and DB_HOST values, and head to your hosting control panel. Once you log in, find the MySQL Databases panel:

my sql link

Click through, and find your list of databases:

databases

One of them will be for your site, and you’ll also note a username here. Both should match your wp-config.php file. If not, you’ll need to change them using your text editor. While you’re here, change the user password and enter this into your wp-config.php file too.

Also, check whether the name of your host is different from localhost. Some hosting providers use a different hostname, so you’ll also need to change this if it’s different:

hostname mysql server

At this point, check your site. If the error has gone, you can celebrate. However, read on if you’re still not up and running.

2. Repair Your WordPress Database

If updating your credentials doesn’t work, repairing your database might. To do this, open your wp-config.php file again within your text editor and head to the line that reads, “That’s all, stop editing! Happy publishing.”:

wordpress database

On the above line, add define(‘WP_ALLOW_REPAIR’, true); and save your changes. Next, head to the example.com/wp-admin/maint/repair.php URL, replacing our placeholder with your site. You’ll see a diagnostics screen:

wordpress repair database

Here, click Repair Database, and when WordPress has finished, check your site. If you still have the error, you may need to take drastic measures. Regardless, remember to remove the line you added to wp-config.php once you’re done.

3. Replace Potentially Corrupt Files

Our final step is essentially replacing a few core files on your server. You should rightly be wary, so we recommend double-checking your actions at every point.

To begin, download a fresh WordPress ZIP file from the official website:

wordpress download

Once it’s finished, unzip the file and remove the following:

  • The wp-content folder.
  • The wp-config-sample.php file.

Next, take the rest of these files and upload them to your server. Usually, you can drag the files on top of your SFTP client. This essentially replaces all of your core files but leaves your media files and wp-config.php file intact.

Finally, check your site to see if the error is resolved. You may want to speak to the experts if you’re still getting WordPress’ Error Establishing a Database Connection – they’ll likely have the expertise to get you back online.

Conclusion

Comparing all of the issues you could have on your site, WordPress’ Error Establishing a Database Connection is straightforward to solve. In most cases, the fault will be lurking within your site’s core files, which narrows down the fix to a handful of possible solutions.

There are three steps you could take to resolve the error:

  1. Confirm that your database credentials are correct.
  2. Carry out a repair on your database.
  3. Replace corrupt core files as a last resort.

Has WordPress’ Error Establishing a Database Connection ever affected your site, and if so, how did you resolve it? Let us know in the comments section below!

Author Bio:

“Tom Rankin is a quality content writer for WordPress, tech, and small businesses.

When he’s not putting fingers to keyboard, he can be found taking photographs, writing music, playing computer games, and talking in the third-person.”

Leave a Reply
Comment policy: We value comments and the time that visitors to our blog spend to give feedback. Please note that all comments are manually moderated and any deemed to be spam or promotional will be deleted.