How to Fix WordPress’ Error Establishing a Database Connection
BLOG

How to Fix WordPress’ Error Establishing a Database Connection

Although WordPress is inherently stable and secure, you’ll encounter several errors while running a site. Fortunately, most—including WordPress’ Error Establishing a Database Connection’—have clear error messages. This means you can get to work fixing your site immediately.

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

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 appears 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 about 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 tells you that wherever your database gets its connection details, it needs maintenance.

The cause is usually incorrect database login credentials. However, there can be other reasons. Regardless, you’ll need to look 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 proper 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.
  • 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 don’t have a safety net if nothing else works. Once your site gets back online, your first task is to back it up regularly!

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

Once you have your tools and skills, it’s time 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, search 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 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.

Also, check whether your host’s name differs from your local host’s. 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. Once you’re done, remember to remove the line you added to wp-config.php.

3. Replace Potentially Corrupt Files

Our final step involves replacing a few core files on your server. You should 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, upload the rest of these files to your server. Usually, you can drag the files onto your SFTP client. This replaces all of your core files but leaves your media files and wp-config.php files intact.

Finally, check your site to see if the error is resolved. If you’re still getting WordPress’ Error Establishing a Database Connection, you may want to speak to the experts— 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 lurk within your site’s core files, narrowing 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. Repair 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 not putting his fingers on the keyboard, he can take photographs, write music, play computer games, and talk in the third person.

Table of Contents

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.