This content is reader-supported, which means if you click on some of our links that we may earn a commission.
Website

5 Easy Steps to Fix an Error Establishing a Database Connection in WordPress

Every WordPress site owner’s nightmare begins with one message: “Error seeing database connection.”

What makes matters worse is it’s also very confusing. There’s no design or element—just an ambiguous plain text.

No need to be scared. You’ve got this.

Seeing an error establishing a connection in WordPress is very common, and fixing it isn’t overly complicated. All you need is the proper guidance, which is where this guide comes into the picture.

Why Fixing an Error Establishing a Database Connection in WordPress Is Important

All the information on your WordPress site, including page and post data, login information, and meta information, is stored in your MySQL database. 

Whenever you get a visitor, PHP executes the code on the page to query the information from the database before displaying it to the visitor in their browser. It’s when this process doesn’t work due to whatever reason you see the error establishing a database connection message. It looks like this:

As you can see, the whole page is blank. No data. None. 

No visitor wants to see this error. Instead, they want instant access and information and quick service, and when they don’t get that, they will move on to your competitors, and you’ll lose business. 

Moreover, in addition to your website’s front-end being broken, you cannot access your WordPress dashboard too. 

Your website is no longer communicating or has to access your database, which may end up taking your whole site down. You may not be able to restore your website, making matters worse for website owners who don’t have a WordPress backup as they will have to start from scratch.

Yes, the situation is bad

It’s to avoid all this chaos that you must work on fixing the database connection error ASAP. The faster you solve the problem, the faster things will go back to normal.

The Investment Needed to Fix an Error Establishing a Database Connection in WordPress

Fixing a database connection error is a hands-on job. 

There can be several possible causes for the error, ranging from network connection errors, WordPress file corruption, incorrect database credentials, or a corrupted database. Your job is to work around them to identify the problem and then resolve it. In other words, you have to patiently try fixes for each one of them until you stop seeing the error.

Disclaimer: This may get very time-consuming.

You can contact your customer support and have the issue fixed within 5 to 10 minutes. Or you can check your WordPress themes and plugin files, the WordPress database, connection credentials, and several other things, resolving which might take a couple of hours.

On the other hand, there’s no actual monetary investment involved when fixing an error establishing a database connection. That doesn’t mean it isn’t costly.

Website downtime is always expensive for website owners. When you don’t get traffic, you won’t get business, have lesser views, experience lower, or worse, no sales, and more. Bad stuff happens to your business. 

Luckily, you can solve the problem in a few simple steps. Here’s what you have to do:

  • Step 1 Call Your Web Hosting Service Provider
  • Step 2 Find Out if Your WordPress Database Is Corrupted
  • Step 3 Check the State of Your Plugin or Theme Files
  • Step 4 See Whether You’re Entering the Correct Database Credentials
  • Step 5 Restore Your WordPress Files to Default

Let’s discuss them in more detail below.

5 Steps to Fix an Error Establishing a Database Connection in WordPress

We’ll discuss a bunch of steps that can potentially solve the connection problem. However, if you stop seeing the error after a specific step, you don’t have to proceed forward.

Step 1: Call Your Web Hosting Service Provider

You can solve the problem by simply contacting your web hosting service provider. You see, the error may be on their side, especially considering your website was fine a day or even a minute ago.

Go to your web host’s website, and use the live chat to talk to the support team. After explaining that you’re facing a database connection error, ask them whether the database server is facing problems, or whether it’s down, or if it’s experiencing sudden traffic or activity spikes.

This will help you understand whether your database is working correctly and detect any suspicious activity on the server. 

The good news is your hosting provider can fix the problem for you. But there can be situations where they may politely decline your request.

To avoid this situation, you should try to select a good web host, preferably one that offers personalized hosting services for WordPress websites and assures quick and efficient support.

Nevertheless, if you find yourself having to fix your problem manually, there are other ways to resolve the error.

Step 2: Find Out if Your WordPress Database Is Corrupted

Your website database is like a complex machine, complete with lots of tiny parts that must work together harmoniously to avoid complications. 

At times, though, some part(s) may go out of place, causing you to see the error establishing a database connection message. 

Open the WordPress admin dashboard. If you continue seeing the error establishing a database connection message, skip this step, and move onto Step 3. 

If you see something else, try to analyze what WordPress is trying to tell you. The CMS has recently expanded its problem-solving solutions, so you might pick up good suggestions that could help your case.

If you see a message starting with “One or more database tables are unavailable…,” you have to “repair” your database. Here’s how to go about it:

  1. Use your account details to connect to your host via FTP.
  2. Go to the WordPress root folder (This is generally called public_html or www) and download the wp-config.php file to your desktop.
  3. Open Notepad to edit this file. Add one extra line at the end: define(‘WP_ALLOW_REPAIR’, true);
  4. Overwrite the old version by uploading the file to your website again via FTP.

Once that’s done, go to yourwebsite.com/wp-admin/maint/repair.php. You should see something like this:

You can select either of the buttons to start the automatic repair process. Then go back to your website to check whether the error is still there.

No error? Fantastic! 

Don’t forget to revert your wp-config.php file to its previous version by removing the line we’d added and then re-uploading it. 

Still seeing the error? Move onto Step 3.

Step 3: Check the State of Your Plugin or Theme Files

Unfortunately, it isn’t uncommon for your WordPress files to get corrupted. 

You may have updated a plugin or a theme that caused the whole thing to collapse. Or you have made an error when hand-editing the file. Maybe you hooked your website up to an external service.

The possibilities are endless. 

Note: Haven’t made any of these changes? Skip this step, and move on to Step 4.

Connect to your host via the FTP. Once you’re connected to the server, go to the WordPress root folder. You may already be in it after establishing the FTP connection.

If you can see folders like wp-content and wp-admin, you are at the right place. From here, click on wp-content, and rename the folder called plugins. You can choose whatever name you like as long as it isn’t plugins

Now go back and check whether you can still see the error. If the error message has disappeared, a plugin is causing the problem.

Just follow the steps below to rectify this:

  1. Open the plugins folder.
  2. Change the folder names of all the plugins individually. The easiest way to do this is by adding a ‘full stop’ or an ‘underscore’ at the end of the plugin names.
  3. Keep opening a website to see if you can still see the error. Once the message isn’t visible, go back to the plugin folder you just renamed. You now have the culprit.
  4. You can either delete the plugin folder or go online to see if its developers have documented a solution.

If you find the error is still there even after renaming all the plugin folders, repeat the exact process with the themes folder. If you’re still seeing the error establishing a database connection, rename both folders back to plugins and themes, and move on to Step 4.

Step #4 See Whether You’re Entering the Correct Database Credentials

You require a specific username and password before connecting to the WordPress database. Although the access credentials don’t change on their own, there can be exceptions. 

Your credentials might change after you modify your website files, hosting environment, or something similar. If that’s the case, open your wp-config.php file. You’ll see four lines of code there:

define(‘DB_NAME’, ‘some_db’);

define(‘DB_USER’, ‘some_username’);

define(‘DB_PASSWORD’, ‘some_pass’);

define(‘DB_HOST’, ‘localhost’);

As you can see, all four hold information that WordPress needs to connect to the database. Naturally, the platform won’t be able to establish a connection if any of this is wrong.

Copy the four lines before contacting your web host’s customer support through live chat. Paste the four lines in the chat window, and ask the customer support representative whether the details are correct.

The representative will let you know whether the credentials are correct, and if not, you’ll get the new details.

If your account credentials have indeed changed, you’ll have to replace your old account credentials with the information you just received from the customer support representative. 

It should look something like this:

define(‘DB_NAME’, ‘yournew_db’); /* this is the name of your WordPress database */

define(‘DB_USER’, ‘yournew_username’);

define(‘DB_PASSWORD’, ‘yournew_pass’);

define(‘DB_HOST’, ‘localhost’); /* this is the web address of your database server */

After updating the details in the wp-config.php file, re-upload it through FTP.  

Return your website to see whether you can still see the error. If yes, proceed to Step 5.

Step 5: Restore Your WordPress Files to Default

You may have corrupted core WordPress files due to either a recent hacking attempt on your website or accidental file modification. Whatever the reason, things will get back to normal once you restore the default, native WordPress files. 

  1. Go to www.wordpress.org.
  2. Download the newest version of WordPress to your desktop, and unpack it.
  3. Open the unpacked archive, and delete the wp-config folder. Bear in mind that you have to delete the wp-config file and not the wp-config.php file.

You have to upload the new files once you’ve restored the native WordPress files. Go to the main WordPress dashboard after connecting your WordPress website via FTP. Upload the recently downloaded WordPress files. 

Doing this will allow the new files to overwrite the previous versions.

Next Steps

Any one of the above five should (hopefully) fix your database connection error problem. 

If you’re still facing a problem, you really have no other choice than to contact the web hosting provider again and ask for help.

Once you get the error is sorted out, here are a few other guides focused on helping you improve your website performance:


Make your website better. Instantly.

Over 300,000 websites use Crazy Egg to improve what's working, fix what isn't and test new ideas.

Free 30-day Trial