How To Fix “Error Establishing A Database Connection” In Your WordPress

Isn’t it quite alarming to see the message “Error establishing a database connection” when you try to open your WordPress site?

No formatting, no fancy elements, just that serious message written across your screen in plain bold letters.

It connotes something serious going on in your WordPress site.

So, what do you do about it?

Panic might settle in because you fear your WordPress site is going down and cannot be fixed.

But don’t worry; this error message is a common WordPress glitch that has several possible reasons and solutions.

This article will look into the common causes of the error message and how to fix them accordingly.

What’s the Main Problem?

What really happens when the “Error establishing a database connection” message appears on your site?

WordPress is built upon two different kinds of technology: PHP and MySQL. These two platforms work together to let you create and display your website contents.

  • WordPress core files are all written in the PHP programming language. These files contain commands that let the main database know which contents and information it must display on your site.
  • Your site’s database is collected and stored using MySQL. This is where all your contents are stored, starting from the smallest to the largest elements. The MySQL database hold everything including your site’s posts, images, layouts, titles, media, and even color settings.

Now, WordPress will use PHP commands to access the needed files from the MySQL database.

When WordPress’s PHP commands successfully connect to the MySQL database, it displays the appropriate files that comprise your website on the screen.

But if not, WordPress cannot access the MySQL database and doesn’t display anything except the error message “Error establishing a database connection”.

Basically, the error message means that WordPress is having a hard time connecting to the MySQL database. That’s why it cannot display any element of your site on your screen.

However, the error message itself doesn’t state the exact reason why this all happened.

Common Causes of the Database Connection Error

There are a number of reasons why WordPress displays the “Error establishing a database connection” message:

  • Your web host’s server is down.

Your web host could be experiencing problems with your database server.

Perhaps different machines separately run your server and your database, and the server is facing some issues.

Or maybe your website experienced a sudden spike in traffic, which leads the web host’s server to crash with the large number of people trying to simultaneously visit your website.

  • Your WordPress database and/or files might be corrupted.

WordPress might be having a hard time connecting to your MySQL database because of corrupted files.

It could be your core WordPress files, your entire site’s database, or both.

The corruption could be a result of failed updates from the WordPress software itself or from plugins and themes you’ve installed.

Another possible reason is that you could have installed a dysfunctional plugin or theme that messed up your database internally.

Your site’s security might also have been compromised with a hacker attack, leading to the loss of your database and core WordPress files.

  • Your MySQL login credentials are incorrect.

You will need a set of login credentials for MySQL in order to access your database. However, you must update your PHP codes to reflect the same credentials.

If you changed the username or password of your database but forgot to update them on your PHP codes, WordPress will really be unable to create a connection between the two platforms, leading to an error message.

Perhaps you also use database usernames and passwords that are different from that of your hosting account. This could also lead to the same error message.

These three error causes are common, but you cannot instantly pinpoint which one is the problem whenever the database connection error message pops on your screen.

You’ll need to figure out the causes on your own first so that you know what to do to fix the problem.

Identifying and Fixing the Source of the Database Connection Error

To find the root cause of your database connection problem, you’ll need two things:

  • Basic background on WordPress file structures
  • Access to your web hosting account’s control panel

Here’s a step-by-step guide on what to do to find and fix the database connection error on your WordPress site.

Note that each step here consists of several sub-steps that might either immediately solve your problem or not.

Hence, read through and follow each step thoroughly to ensure that you properly identify and fix the cause of your site’s error messages.

Find out where the error is happening.

You must check if the error is occurring both on your site’s front-end (yourwebsitename.com) and back-end (yourwebsitename.com/wp-admin).

Visit both pages and check if the same error message appears on them. Replace yourwebsitename.com with your own site’s URL.

You can proceed to the next step if both pages display the same error message.

There are instances when these two pages will display a slightly different error message. You’ll likely see the following message:

  • One or more database tables are unavailable. The database may need to be repaired.

This indicates that your WordPress database needs repairing. No need to panic, since WordPress has a built-in feature for doing just that.

  • Access your root WordPress directory and search for the wp-config.php
  • Open the file and locate this line: ‘That’s all, stop editing! Happy blogging.’
  • Add this code before the aforementioned line: define(‘WP_ALLOW_REPAIR’, true);
  • Save your changes.
  • Go to this page: http://www.yourwebsitename.com/wp-admin/maint/repair.php. Replace com with your site’s URL.
  • A screen appears informing you of two ways in which WordPress can repair your database. Choose from Repair Database and Repair and Optimize Database. Click the corresponding buttons and WordPress will take care of the rest.
  • Once repair is done, go back to the wp-config.php file and remove the line of code you previously added earlier. The database repair page is not secure. Hence, anyone can open that URL without having to log-in to your WordPress account. Hence, it is vital that you remove that code line. Save your changes, but keep the wp-config file open.

Check your site now to see if the database connection error message still appears. If not, then your issue is resolved. If it’s still there, proceed to the next step.

Check your database login credentials and settings under the wp-config file.

If you changed your WordPress root password or your database user password, then you must access the wp-config.php file once again and change the credentials in this file as well. To do this:

  • Open the wp-config,php file once again by going to the File Manager or FTP account, locating your WordPress core files, and clicking the wp-config.php file.
  • Check the top part of the file. You must see the following information:
  • DB_NAME – The database name
  • DB_USER – Username for logging in
  • DB_PASSWORD – Password for logging in
  • DB_HOST – Name of database host
  • table_prefix – Database prefix
  • Check of all the information on these parts of the file is correct.
  • You can also compare these values against your MySQL Database details. Just open the control panel section of your web hosting provider and look for the MySQL Database section. This step will vary depending on the web host you are using, so refer to the respective help manuals or tutorials for your particular web host provider for this.
  • If the values are not the same, you can enter the updated values in your wp-config.php file and save the changes. These must now match with your MySQL database values.
  • Once done, visit your WordPress site. It should fix the problem.

But if the error message still persists, your MySQL password and your wp-config file password might be mismatched.

You can change your MySQL database password under your web host provider’s user panel, then update your wp-config file to match the new passwords together.

Save your changes and refresh your WordPress site.

If the error message still appears, your table prefix might be the problem. Check which prefix WordPress is using by going to your phpMyAdmin interface and opening your database.

Find the table prefix and compare it against the one on your wp-config file. If it matches, refresh your WordPress site.

If the error message still persists despite this, go to the next step.

Check the Database Host Value

DB_HOST is another vital value in your wp-config file.  The default value for this is typically localhost. This is also the value that most web hosting providers require.

  • Go back to your wp-config file and ensure that localhost is entered as the value for DB_HOST.
  • Refresh your WordPress site to check if the error message has gone.
  • If it still persists, you can refer to this list of DB_HOST values on the official WordPress.org site. This page contains all possible host values for different host providers. Select the one that matches your web host provider and update your wp-config file.
  • Refresh your site again. Still seeing the error message? It’s time to contact your web host provider to ask for the DB_HOST value you need to input to your wp-config file.
  • Contact your Web Hosting Provider

If all the above-mentioned steps failed to resolve your site’s database connection problems, it’s time to get in touch with your web hosting provider.

It’s also a sure-fire way to check if the database connection problem is something from their end, such as a server-down issue or a heavy traffic to your site that they can’t temporarily handle.

Most hosts offer some sort of fast support today. Live chat support is often the first line of contact you can use to get quick answers and assurance from your web hosting provider.

Consider these tips when contacting your web host provider via live chat:

  • Tell them exactly what the error message is. Copy and paste the “Error establishing a database connection” message from your website.
  • Ask them the possible reasons why you keep on seeing this error message.
  • Tell them any steps you’ve done to check the error by yourself.

Support teams usually reply with the results of their investigations on your site. They can pinpoint the cause of the error is.

At times, they may even be willing to fix the error for you. They will also let you know if there’s any suspicious activities going on in your server, and if your database is intact and working well.

The Takeaway

Getting that dreaded “Error establishing a database connection” message can be anxiety-inducing and scary at times. But this is actually a common error in the platform.

WordPress is an amazing site filled with lots of functionalities, making it extremely prone to errors such as this.

We have provided several steps in checking what the problem in your site is, and the corresponding actions to fix them.

If the suggested solutions fail to help you and your site, that’s the time to contact your web host’s support team. They can find the error for you and help you fix it as well.

You don’t have to be afraid of the database connection error message in WordPress.

We hope this guide can help you fix your WordPress site by yourself before you have to resort to contacting support teams of your web host provider.

Here’s to hoping your site gets up and running smoothly once again!

Leave a Reply

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

error: Content is protected !!