WordPress is a very user-friendly platform, but in some cases, you may run into an error that’s difficult to fix. For example, the Error establishing a database connection problem can be tricky to troubleshoot, because there’s more than one possible cause for it.

Fortunately, each of those causes is simple enough to fix if you follow the right instructions. With a little help, you should have your WordPress website up and running again in no time, so you can get back to working on its content.

In this article, we’re going to talk about what databases are and why they’re so important for your WordPress experience. Then we’ll discuss the causes behind WordPress database errors, and teach you how to solve them using three distinct approaches. Let’s get to work!

What WordPress Databases Are (And Why You Need Them)

An example of a WordPress database.

WordPress stores all of its information in a database, which makes it pretty important.

Databases are essentially collections of tables that include information. If you’re running a website involving any interactive elements, it’s definitely using a database to store them. The thing is, databases are background elements, so most users don’t normally interact with theirs directly.

When it comes to WordPress, you need a working database if you want the platform to run at all. In fact, if you’ve ever installed WordPress manually you probably remember the platform asking for your database details. Without those details, the installer can’t connect to your database and configure it so WordPress can use it.

Here’s some of the information every WordPress installation stores in the database:

  • All of your user data. Passwords, usernames, social media details, and more. Every time someone registers on your website, it’s added to the database.
  • Your posts. Each post you create on WordPress is saved to your database, including the post’s body, its title, metadata, and even the publishing date.
  • Comments on your website. Every comment made on your site goes into your database, including who made it and when.
  • Your website’s taxonomy. All of the categories and tags that you set up for your pages and posts go into your database as well.

Every time you load a page in WordPress, the platform needs to call to your database to find the data needed to render it. In other words, if your website can’t establish a connection to its database, it won’t work at all.

What Causes the Error Establishing a Database Connection in WordPress

The error establishing a database connection.

This error in particular can be quite scary, since it hardly displays any details.

The error in question is self-explanatory, thanks to the message that pops up when it happens. When WordPress can’t connect to its database, your website will stop loading altogether. This means you won’t be able to access either your pages or your dashboard. It’s quite a hassle, to put it mildly.

WordPress is renowned for being an easy-to-use platform. However, much like any other modern Content Management System (CMS), there are a lot of moving parts that go into any website built with it. To put it another way, several of its most common errors can have different causes.

Let’s talk about some of the usual suspects when it comes to WordPress database errors:

  • Your server is down. In some cases, your website won’t be able to connect to its database because of problems with your server. It may be that there’s too much traffic, or an issue with permissions. Either way, there’s a simple fix if the error isn’t on your end, and we’ll talk about it in a minute.
  • A corrupted database. Sometimes, the term ‘corrupted’ is used when a file suddenly ceases to work. For example, if you try to open a text file but it’s suddenly full of weird symbols instead of whatever was there before, it may be corrupted. The same thing can happen to databases, and can make it impossible for WordPress to decipher the information within. As for what causes corruption, it’s often a byproduct of errors while saving a file or making changes to it.
  • Using the wrong login credentials. Databases tend to store a lot of sensitive information, so they usually require you to use the right credentials to gain access to them. WordPress, for example, needs full access to its database to work. If someone were to delete your user or change its password, you wouldn’t be able to connect to your database anymore.

The good news is that if you can’t connect to your database for any of the reasons above, you can fix the problem with a few minimal tweaks. However, there are different approaches you can take, which is where the next section comes in.

How to Fix the Error Establishing a Database Connection Problem in WordPress (In 3 Ways)

In this section, we’re going to explain how to troubleshoot WordPress database errors for the three causes we described earlier. For efficiency’s sake, we recommend you try these solutions in the order they’re presented.

1. Use WordPress’ Repair Database Feature

WordPress repair database feature.

Fortunately for us, WordPress includes a hidden feature to help you repair your database.

If you aren’t able to access a backup, you’ll need to move on to a different solution. WordPress includes a Repair database feature out of the box that can help you fix corrupted files. However, the option is turned off by default, so we need to enable it before we can see if it fixes your problem.

To do this, you’ll need a File Transfer Protocol (FTP) client, such as FileZilla. This type of tool enables you to connect to your server and transfer files back and forth securely. To use your FTP client, you’ll need to log into your server using credentials your host will provide. They’re entered into the four fields underneath the toolbar: Host, Username, Password, and Port. In most cases, they’ll send them as soon as you sign up for their services, so check your inbox until you find them. Alternatively, log into your 000Webhost panel and go to the Settings > General section. You’ll find your credentials right at the top of the screen:

Your FTP credentials.

Once you have them, open your FTP client and log into your website, then follow these steps:

  1. Go to your WordPress root folder in the right-hand panel.
  2. Look for the wp-config.php file, right-click it, and choose the View/Edit optionwhich will open the file using your local text editor.
  3. Within the file, look for the That’s all, stop editing! Happy blogging line.
  4. Paste this code right above the previous line: define( ‘WP_ALLOW_REPAIR’, true );
  5. Save the changes to the file and close it.

Now that the feature is enabled, navigate to www.yourwordpresswebsite.com/wp-admin/maint/repair.php (making sure you’re replacing the placeholder with your own website’s URL) and click on Repair Database. WordPress will now attempt to fix the corruption, and if it does, you should be able to re-access your website. On the other hand, if the process fails, move on to step number three.

2. Check and Update Your WordPress Database’s Login Credentials

If neither of the previous steps worked, the problem most likely lies with your credentials. As we mentioned earlier, WordPress won’t be able to establish a connection if it’s using the wrong username and password, or if it’s trying to access the wrong database altogether. Let’s fix that now.

First off, return to your 000Webhost control panel and go to the Manage database screen. There should only be one database available and its name will appear under the DB Name field:

Detailing your WordPress database.

Take note of the name (in our case id3549409_wp_93df1afcc431c1fdf9b4bed86949ec65) or just keep the window open for the time being. Next to it, you’ll find a field with your database’s admin username under DB User. You’ll need both these values in a minute, but for now, click on the Manage button and look for the option that reads Change password then pick a new one for your database:

Changing your database's password.

As always, keep note of your new password. When that’s done, open your FTP client once more, and run through the following steps:

  1. Go to your WordPress root folder.
  2. Look for the wp-config.php file.
  3. Right click it and choose the View/Edit option.
  4. Once the file is open, look for the line that reads define(‘DB_NAME’, ‘yourdatabasenamegoeshere’);, making sure the placeholder instead reads as your database.

The line we mentioned above should be followed by two others that define your database username and password respectively. Here’s what they should look like, complete with placeholder text:

define('DB_NAME', 'yourdatabasenamegoeshere');
define('DB_USER', 'usernamegoeshere');
define('DB_PASSWORD', 'passwordgoeshere');

If any of those values doesn’t match those you saw earlier in your control panel, replace them, and make sure to add your new password as well. Finally, save the changes to wp-config.php, close it, and access your site. It should be working properly now, so you can pat yourself on the back for a job well done!

3. Restore Your WordPress Website From a Backup

If everything else fails, backups are your last line of defense when troubleshooting any website. If you have a recent working backup, you can just roll your site back to that state, and you’re back in business.

The real problem comes if you can’t access your WordPress website, which makes this process a bit more complicated than it should otherwise be. In most cases, WordPress backup tools require you to restore the site from your dashboard. Since that option isn’t available, you’re left with two potential solutions. Number one is using your web hosting control panel to restore a backup if your provider offers this feature (and you’ve set it up). Not all of them do, but it can’t hurt to check. Just log into your control panel, look around for any option saying Backup or similar:

An example of a backup option.

Sadly, the option is disabled by default for our free hosting accounts. If your web host doesn’t offer this feature, you might consider migrating to one that does, such as Hostinger. However, there’s no reason to despair yet – there’s another alternative we’ve yet to explore, and that’s restoring a backup manually. This method assumes you have access to your backup files, which could mean you’ve stored them locally or in the cloud.

Example of local backup files.

If that’s the case, you’ll also need a File Transfer Protocol (FTP) client, such as FileZilla, to complete the next steps. This type of tool enables you to connect to your server and transfer files back and forth securely.

The FileZilla FTP client.

To use your FTP client, you’ll need to log into your server using credentials your host will provide. They’re entered into the four fields underneath the toolbar: Host, Username, Password, and Port. In most cases, they’ll send them as soon as you sign up for their services, so check your inbox until you find them. Also, re-download the latest version of WordPress from the official website, then unzip its contents wherever you want.

Next, navigate to your backup storage location using your computer’s file system, copy the backup you want to restore to a folder you can locate quickly, and unzip it. You should see three directories within: uploadspluginsand themes. Copy those directories to the wp-content folder of the fresh WordPress installation you downloaded earlier:

The wp-content folder.

You should now have all of the folders you need to run a WordPress setup, so it’s time to upload them to your server. To do so, follow these steps:

  1. Log into your server using FileZilla.
  2. Locate your WordPress site’s root folder (usually named either public_htmlwww, or after your website) in the right-hand file directory within FileZilla.
  3. Access that folder, select all the files and directories within, right-click on them, and erase everything.
  4. Copy the WordPress installation you’ve just downloaded and amended to your server’s root folder.

In a nutshell, we’ve just deleted the existing files on your server, and replaced them with a clean WordPress installation – including your themes, plugins, and uploads. All that’s left to do now is to replace your database. Most WordPress backup tools provide you with database copies as well, so locate the most recent one from your backups folder on your computer or cloud service. Now, access your web hosting control panel using your login credentials:

Accessing your hosting control panel.

Once inside, return to the Manage Database tab, then look for the Manage button next to your default database’s DB Host section and click on the phpMyAdmin option:

The phpMyAdmin button.

This tool can help you make modifications to your existing databases and create new ones:

The phpMyAdmin tool.

While you can do many things here, we’re going to wipe our existing WordPress database and import new tables from our backup file to replace them. Here’s a step-by-step guide on how to do it:

  1. Look for your WordPress database on the left side of the screen (it should include the term wp as a prefix or part of its name).
  2. Click on it, and once you’re in, select all the tables within and use the Drop option to delete them.
  3. Right now, your database should be clear of any tables, so go into the Import section at the top of the screen and click the Choose file button.
  4. Select your database backup file on your local computer, which should be in the .xml format.
  5. Click on the Go button at the bottom of the Import tab.

That’s it! phpMyAdmin will import the tables from your database backup, and since we’ve already restored your WordPress files, you should be able to access your website normally again. It’s a good thing you had a backup!

Conclusion

Troubleshooting WordPress isn’t as scary as you might think. Any error you run into has a fix, and in most cases, they’re relatively straightforward. Plus, the platform is so popular that you can find detailed tutorials on how to tackle any problems – including some of the most complex WordPress database errors.

If you run into an error establishing a database connection to WordPress, here are three ways to fix it:

  1. Use WordPress’ repair database feature.
  2. Check and update your WordPress database’s login credentials.
  3. Restore your WordPress website from a backup.

Do you have any questions on how to fix the WordPress error establishing a database connection? Ask away in the comments section below!

Submit a Comment

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