February 17, 2021

Error establishing a database connection

When viewing your website are you seeing the error “Error Establishing a Database Connection” message on your WordPress site? This fatal error will make your site inaccessible for viewers.

The error itself occurs when a connection is not made to the database, you’re connecting to either from the wp-config.php file itself or for a few other reasons.

In this post, we will go through the basic steps of troubleshooting and how to easily fix this pesky error and get your site back up and running.

What does error establishing a database connection mean?

The root cause it usually down to incorrect database credentials that have been entered in the configuration file, missing tables or the database server itself fails to allow connection.

As WordPress is dynamically driven, it stores a large percentage of data within the database itself, which is called upon through out your pages, posts, plugins, theme etc…

WordPress needs the following to connect to the database:

  • Database name
  • Database username
  • Database password
  • Database host

If anyone of these fields are incorrect this will cause your WordPress database to ultimately fail in connecting to your site. It is one of the most common mistakes, a typo can easily be made and leave you scratching your head. Always double, triple or even quadruple check they are CORRECT.

Check Your WordPress Credentials

The most common reason you’re seeing this error is usually down to that you’ve recently switched hosting providers.

How do I check my WordPress Configuration File?

These details are stored in the wp-config.php file which can be found in the root folder of your WordPress installation:

cPanel DB
Error establishing a database connection 8

When you edit this file, you should see the following lines from the config file:

WP CONFIG
Error establishing a database connection 9

You will want to cross check the credentials entered in the file against MySQL database. If you’re on a cPanel host, this will be found in the control panel under the heading ‘Databases’:

mysql DB
Error establishing a database connection 10

This should direct you to the database overview page where you can see existing databases:

current db
Error establishing a database connection 11

Further down the page you should see the current users that are associated to your database:

change password
Error establishing a database connection 12

Once you have confirmed the above fields i.e. database name, username, password and host (This is usually localhost, however, this can vary with hosting providers. If you are unsure, it’s best to get in touch with your hosting provider to confirm the hostname) – You can also change the password for the database user if needed, just make sure to use the updated password in the database password field in the wp-config.php file.

After you have done the above, try visiting your site again to see if the error has abated.

If doing the above and you’re still seeing a database connection error message, this will most likely mean something else is at fault here.

Repair Your WordPress Database

If you’re seeing the following error when visiting /wp-admin “One or more database tables are unavailable. The database may need to be repaired.” It would indicate that within your database, a table is missing:

db unavailable
Error establishing a database connection 13

If you have migrated the site to another host, the cause will usually be that during the export of the database, a few tables may have not been brought over. It is best to always double-check and grab another export, however, you can repair the database by selecting the hyperlink on the page.

You will need to add the following line in the wp-config.php file prior to repairing the database itself:

define(‘WP_ALLOW_REPAIR’, true);

allow repair
Error establishing a database connection 14

Please be aware that this page can be accessed when you’re not logged in, so, remember to remove that line from the config file after you have finished.

Check If Your Database Host is Down

If you’ve done all of the above and still are experiencing issues with connecting to the database, then your MySQL host may be the root cause. This usually happens during busy periods and the server simply can’t handle the amount of traffic, which causes the server to cause intermittent connection problems to the database itself.

If you have multiple hosting accounts on the same server running WordPress, check to see if they’re having issues connecting to the database.

The next step would be to contact your hosting provider or check their network status page to see if any existing problems are ongoing or to directly contact them for a further update or assistance on the matter.

Finally

We would love to hear your suggestions on the matter. Drop us an email at [email protected] and we will update this blog post with your suggestions to hopefully help other users. If you’re a direct customer with ourselves and would like help on the matter, get in touch and one of our technical experts will be happy to assist.

crossmenu