Table of Contents
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 on your WordPress site. Before that, let’s talk about where the error comes from.
Error Establishing a Database Connection on a WordPress Site Explained
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 most common 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 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 FTP 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. The official WordPress website has great tutorials to improve your skills.
- A suitable text editor. Any text editor will do, but an app such as Atom will present the code more clearly and provide a pleasant overall experience.
- Confirmation that your database server is working. This can be as simple as checking a dedicated status page or potentially getting onto your web 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 troubleshoot! 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 connected, you’ll see a list of your site’s files in the public_html or www directories or an explicitly named directory.
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):
Note the DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST values, and head to your hosting control panel. Once you log in, find the MySQL Databases panel:
Click through and find your list of 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 config file: wp-config.php.
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:
Check your site to see if it’s working and connected to the correct database. If the error has gone and there is no database connection issue, 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.”:
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:
Here, click Repair Database, and when WordPress has finished, check your site. You may need to take drastic measures if you still have the error. 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:
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 from the WordPress installation to your server. Usually, you can drag the files onto your SFTP client or use the file manager in cPanel. This replaces all your core files but leaves your media 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:
- Confirm that your database credentials are correct.
- Repair your database.
- 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.