WP Latest

WordPress Troubleshooting: Error Establishing a Database Connection

database_error.jpg

WordPress Troubleshooting: Error Establishing a Database Connection

In this post, we’ll quickly review the common causes of the WordPress error establishing a database connection. Then we’ll provide you with a step-by-step guide on how to troubleshoot and repair it. 

WordPress errors are always frustrating when they arise. The good news with this particular one is this: The Error establishing a database connection tells us where the issue occurred. This will significantly cut down on how much troubleshooting you’ll have to do to fix it.

Why Won’t WordPress Connect to the Database?

When you encounter an error code that reads “Error establishing a database connection”, it means your website can’t fetch the data from your website. Since WordPress uses a database to store all of your website content and data, it needs to connect to it in order to run the site. 

Fortunately, you’re likely to catch this error before it affects your website visitors. So long as you have caching installed, your website should continue to display your web pages without issue until you modify something. 

However, you will lose access to the backend WordPress dashboard until this error is resolved. 

So, what causes it? There are a number of reasons why you’ll see this error:

  • Your database login details are wrong.
  • The website URL is incorrect.
  • The database tables have been corrupted. 
  • WordPress files have been corrupted.
  • The server is down or overloaded. 
  • Your site has been hacked.

This kind of error — regardless of the source of the problem — needs to be fixed immediately. And not just because you won’t have access to your site. 

When your site inevitably goes down and visitors see the message “Error establishing a database connection”, it’s going to scare a lot of them away. It’ll cause others to question the security and/or legitimacy of your site. 

To protect your brand integrity as well as your website’s traffic and sales, fix the error as soon as you spot it.

How to Fix the WordPress Error Establishing a Database Connection

In order to fix this WordPress error, you’ll need access to your server and files. It’s okay if you’ve never made modifications to your files or database before. We’ll show you where to go to implement each of these solutions.

We’ll be using SiteGround web hosting to demonstrate these repairs. While your hosting dashboard may look a little different, the basic steps will be more or less the same.

Solution #1: Update Your Database Login Details

In order for your WordPress site to connect to the MySQL database, WordPress needs the following information:

  • database name
  • database username
  • database password
  • database host name

WordPress stores these login details in the wp-config.php file, which is located in the document root of your website. You can edit the wp-config.php by connecting to your site with an FTP client or you can use cPanel if it is supported by your host.

In SiteGround (or whatever your hosting provider is), go to Site Tools > Site > MySQL first. 

Your database name is under Name. Click the # User link to reveal the username. Note these down somewhere.

Go to File Manager (or FTP Accounts, if you have one) next. 

Click on public_html to view the documents stored at your website’s root. Scroll down the list until you find wp-config.php

Click the Edit button or double-click it to open the file. Your login details will look like this: 

1
/** The name of the database for WordPress */
2

3
define( 'DB_NAME', 'fakedbname' );
4

5
/** MySQL database username */
6

7
define( 'DB_USER', 'fakeusername' );
8

9
/** MySQL database password */
10

11
define( 'DB_PASSWORD', 'fakepasswordpassword' );
12

13
/** MySQL hostname */
14

15
define( 'DB_HOST', 'localhost' );

Start by comparing the database name and username to the ones you just copied from MySQL. If they’re wrong, that’s likely why WordPress can’t connect to the database. 

Before you make any changes to this file, save a backup of your website. Even if the website is down due to the connection error, the issue could be exacerbated if the error isn’t properly fixed. 

So it’ll be useful being able to roll back to this point before you’ve altered anything. Here’s what you’ll do to back up your WordPress site.

Back to the wp-config file. If the first two values are incorrect, edit them and then click Save

Return to your website to see if this fixed the issue. If it did not, the password could be the issue. 

Instead of trying to track down the encrypted password, go back to the MySQL Manager and click on the Users tab at the top. Click on the vertical ellipsis on the far right and select Change Password

With your password updated, you can return to your File Manager or FTP and update the password in the wp-config file to match. 

Save your changes. Then return to your WordPress site once more to see if this fixed the issue.

Solution #2: Fix the Site URL

If the issue remains unresolved, the next thing to check in the database is your site URL. 

For this one, go to MySQL > PHPADMIN and click the ACCESS PHPMYADMIN button.

Click on the Databases tab at the top. If you have more than one database, select the primary one for your site. 

The table you’re looking for is commonly called wp_options. However, yours might have a different prefix. Use the search bar at the top of the page to find tables with the word “options” in it to find yours. 

Select the _options file. Click to open it. At the top of the list of details, you should see siteurl.

Confirm that the option_value matches your domain name. If it doesn’t, click the Edit button on the left to change it. 

Reminder: If you haven’t made a backup of your site yet, do so now before editing this table. 

When you’re done, save your changes. Then return to your website to see if the error is gone.

Solution #3: Activate Repair Mode

In some cases, the database has been corrupted. This can happen if you add and remove a lot of themes or plugins and the data doesn’t get completely cleared out or an error occurs along the way. 

To repair the damage, you can use WordPress’s repair mode. 

Go back to Site > File Manager or FTP Accounts. Open and edit the wp-config.php file once more. 

Locate the following:

1
/* That's all, stop editing! Happy blogging. */

Directly above it, add this line of code:

1
define('WP_ALLOW_REPAIR', true);

Save your changes. 

Open a new tab in your browser and enter the following: 

https://yourdomain.com/wp-admin/maint/repair.php

Replace yourdomain with your actual website’s domain name. Then click the Repair Database button that appears. 

Once repair mode has completed, visit your website once more to check for the connection error. 

Whether or not the error has been removed, you’ll need to go back to the wp-config.php file to delete the repair mode string. Save your changes when you’re done to take your site out of repair mode.

Solution #4: Check For and Replace Corrupted Files

Theme and plugin conflicts and file corruptions can happen. So too can issues with the WordPress software. So if nothing else has worked until now, it’s time to see what happens when you put these files to the test.

Test Your Theme

From your File Manager or FTP, look for a folder called wp_content. This is where both your themes and plugins are stored. 

Locate the folder called themes

Right-click the folder name and click Rename. Change it to old-themes. This will force WordPress to deactivate your current theme and enable the default one in its place. 

Check your website to see if that fixed the issue. If it did, then the latest version of your theme is conflicting with a plugin or with WordPress. 

To see if it’s a plugin conflict, rename the folder back to themes. Then proceed with the plugins test.

Test Your Plugins

Go to the plugins folder and do the same thing. Change the name to old-plugins. Again, this will deactivate all your WordPress plugins. 

Check your site once more. If the issue has been resolved, that means that one of your plugins (or a recent update of one of them) is conflicting either with another plugin, the theme, or WordPress itself. 

Since you can now log into WordPress, go to the Plugins panel. Reactivate each plugin one at a time. Refresh your site to see if it remains live. If it does, then you can cross that plugin off your list. 

Deactivate that plugin and then move onto the next one. Repeat this process until you’ve tested each plugin on its own. 

If there are no issues at this point, the issue isn’t a plugin/theme conflict. So it might be two plugins conflicting. You’ll need to work your way through different pairings to see where the issue lies. 

To more effectively hone in on the issue, contact your web hosting support to see if they can tell you which of your plugins were most recently updated (in case you don’t remember). That’ll help you pinpoint the conflict. 

Once you’ve found the issue, you can either roll the plugin back to the previous version that works or find an alternative solution. It’s also a good idea to notify the plugin developer and/or theme developer of the issue.

Test WordPress

If nothing else has worked, then the problem could be WordPress or the latest version of it. 

Unfortunately, in order to fix this kind of error, you’re going to have to replace the core version of the software. While it won’t affect the main content of your site, it will erase any custom coding you’ve done or changes you’ve made in the wp-config.php or .htaccess files. 

If you’re uncomfortable with doing this step, that’s not a problem. Move to the next solution.

To replace WordPress, Get WordPress from the WordPress.org site. Download the latest version of the software. 

Unzip the file. Delete the wp-config-sample.php file along with the wp-content folder. 

You’ll need FTP access to your core to upload the new WordPress file. If you don’t have it yet, you can create an FTP Account in SiteGround. Then use FileZilla to upload and replace the WordPress core files. 

When you’re done, check your WordPress site to see if the error is resolved. If it is, you can restore all of your themes and plugins to where they were previously.

Solution #5: Restore Your Last Backup

If WordPress still fails to establish a database connection, then you’ll want to roll it back to the last saved version of the database. 

There are different tools WordPress users use to capture website backups. You might be using WordPress backup plugins like UpdraftPlus and Jetpack. Or you could be using your web hosting’s backup service. 

For this example, we’ll use SiteGround’s backups to roll your database back. 

In SiteGround, go to Site Tools > Security > Backups. Scroll down to the list of backups. 

Click the vertical ellipsis to the right of the most recent backup (i.e. not the one you saved while troubleshooting today). Select the Restore Databases option.

Select the database or databases you want to restore. Check the box stating that you understand the action you’re about to take. Then click Confirm

After the backup restoration process is done, visit your website to see if the error is gone. If it is, then something that took place between now and that last backup caused the issue. 

If you’re unsure of what that might be (like a plugin update, a team member poking around, etc.), get in touch with your hosting provider. They should be able to take a look in your logs and figure out what went wrong.

Solution #6: Contact Your Web Host

If you have come this far, then the last thing you can do is to contact your web host’s support team. There are additional issues that can cause the Error establishing a database connection to appear and only they will be able to help. 

For example: 

  • The servers are down or undergoing maintenance. 
  • Your database reached or exceeded its limit due to excessive traffic.
  • Your website has been hacked. 

When you contact support, let them know the issue you’re seeing when you try to access WordPress or your website. Send them a screenshot of the error message if possible. Also explain the solutions you’ve tried up until this point. 

This will save time as they narrow down the issue and work to get it resolved for you.

Conclusion

It’s stressful whenever an error appears on your website. One day, everything’s working fine. The next, you’ve lost access to WordPress and your visitors are being shown a blank page and error message. 

Thankfully, we know where the source of this WordPress error establishing a database connection lies. This will make it a whole lot easier to find what’s causing it.

Time is of the essence when it comes to WordPress errors, especially one like this that may scare away visitors. Once you identify what the error is, work your way through the six solutions above so you can get your website restored as soon as possible. 

Leave your thought here

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

Enable Notifications OK No thanks