WordPress: Error Establishing a Database Connection (Solution)

Updated on By

WordPress is easily one of the most popular Content Management Systems out there. According to Kinsta, more than 60.8% of the websites in the world (and about 14% of the top websites) use WordPress.

It’s quite a popular CMS platform, but it doesn’t come without its own set of problems though. The “error establishing a database connection” problem is one of the major ones that can send even some shivers down the spine for WordPress users, often getting close to the severity of the dreaded WordPress White Screen of Death.

It’s not just the developers and WordPress pro users who come to depend on the platform. More than 500+ websites are built on WordPress every day and a bulk of those WordPress sites are created by the “not-so-tech-savvy” crowd.

For the average user, any error — including the “establishing a database connection” error — could almost mean the end.

With some basic understanding of why “error establishing a database connection” occurs, you can easily solve this issue easily.

Let’s see how it’s done:

What is the Error Establishing a Database Connection?

WordPress is built using HTML/CSS and relies on a MySQL database to store and retrieve information. In all its simplicity, the reason why you get “Error establishing a database connection” is because your WordPress website isn’t able to connect with your server to help save or extract information stored on your database server.

Your WordPress site is actually a complete, ever-growing set of information and data, typically stored on a server. The information on your WordPress site is arranged as post data, page data, meta information, plugin settings, login information, and more. If you have multiple users who log into your website, all the information pertaining to each of the users is also included in the database.

So, if you had a real estate directory website running on WordPress, your MySQL database will have all of the information pertaining to directory listings (images, descriptions, prices, agent details, and more), user information (each with pertinent login info and user account information), and more.

The MySQL database

Technically, the bulk of this information is stored in your MySQL database (except media such as images, theme files, WordPress core files, and a few others).

When someone visits your website, PHP executes the code on the page and queries the information from the database, which then displays it to the visitor in their browser.

If for some reason this isn’t working properly, you are left with the error establishing a database connection message, as seen below.

 

The entire page is blank because no data can be retrieved to render the page, as the connection is not working properly. Not only does this break the frontend of your site, but it will also prevent you from accessing your WordPress dashboard.

However, visitors might not see this error on the frontend right away. That is because your site is most likely still serving from cache until it expires.

With most WordPress hosts WordPress sites are cached for one hour by default. Therefore, if a site is still serving from the cache it might appear fine to a visitor.

Why Does The Error Establishing a Database Connection Occur? 

Sometimes, it could be the simplest of the issues that could cause the error establishing a database connection such as your database login credentials are incorrect. Your WordPress site uses separate login information to connect to its MySQL database. So, if you misplaced or forgot your password, that could be a reason as to why you couldn’t connect.

But if that’s not the case, then here are a few common reasons why it happens:

1) Mismatched/Incorrect database credentials

To connect your WordPress core to the database on the server, a special wordPress file known as the wp-config file carries the login information. The actual database name, the username as well as the password used to create the database on the server should match with the details in the wp config php file for a successful connection. If for any reason, the credentials don’t match – the error will appear. This is one of the most common reason and the easiest to fix.

2) A corrupt database:

There’s a chance that your database is corrupt. Remember that WordPress has several moving parts such as themes, child themes, plugins, and several users. Also, if there are instances of a plugin being removed, deactivated, or deleted by mistake, sometimes databases get corrupted. This can be due to a missing or individually corrupted table, or perhaps some information was deleted by accident.

3) Outdated WordPress themes/plugins

If you haven’t taken the time to update your WordPress Core, themes, or plugins, it can be the problem. It may have corrupt files or conflicting versions in your WordPress installation. And that can cause the database connection problem.

4) Compromised security

Has your site been hacked recently? Was there an instance of a brute force attack? One of the other reasons why there’s an error establishing a database connection is due to security breach. It could be the fact that your WordPress installation has been hacked into or attacked.

5) Faulty Database that needs repair

There could be Issues with your database server — a number of things could be wrong with your web host, such as the database are overloaded from a traffic spike or unresponsive from too many concurrent connections. This is actually quite common with shared hosts as they are utilizing the same resources for a lot of users on the same servers.

The one’s mentioned here are few of the most common reasons why you may encounter the error. But there can be other reasons as well as other fixes too. The error happens because the connection with database is not successfully made. Therefore the WordPress resources cannot fetch the data to display your website.

Therefore, any problem – that can hinder this connection can cause the error.

How to Fix the Error Establishing a Database Connection 

It could be scary — to say the least — when you see absolutely nothing on the screen when you visit your website and only see the Error Establishing a Database Connection message. If you face this as a WordPress user, don’t panic. There are a few instant fixes possible and hopefully, you should be able to fix the error.

Here are some ways you can quickly fix the issue:

Check out your Database connection credentials

This is one of the most common reasons why the error may appear. Sometimes, when you use incorrect credentials for connecting the database, the WordPress error establishing a database connection may appear. So when such an error occurs, you should first of all check the database credentials for your website.

In WordPress, this information is stored in the wp-config file. You can access this file on your host using an FTP Client to see that correct login credentials are used.

In order to access the WordPress files through FTP, you will first need the FTP access credentials. If you already have them, you can login and proceed to find wp-config.php file in the root folder of your WordPress website. If you don’t have the FTP credentials, you can ask you hosting company to provide you with one.

The wp config php file

The wp-config file, as the name suggests, is your WordPress website’s configuration file and contains the database credentials.

The following line defines the name of the database, where “mydatabase” will be your database name:

define( ‘DB_NAME’, ‘mydatabase’ );

For example: If your database name is “emporiumstoredb” the line in your website’s wp-config file should look like:

define( ‘DB_NAME’, ’emporiumstoredb’ );

Similarly, the following line defines the username for accessing the database. Of course, it should be your database username:

define( ‘DB_USER’, ‘username’ );

And the same for password will be:

define( ‘DB_PASSWORD’, ‘pwd’ );

Now, to connect your WordPress website with the database, this information stored in the wp-config file is used. So you need to check and correct the information based on your actual database name, username, and password.

Checking your WordPress database credentials

To do so, login to your hosting control panel. Whatever hosting service you may be using, you can find the database section and then MySQL databases. Click to view your MySQL databases.

This will show you your website’s database. Clicking on your database name will show you all the database servers created under your account. And you can choose the one that you use for your current website.

The database management page will show you the username, as well as password. Therefore, you can check all the three details: the database name, the username and the password. If you find anything mismatching, that is exactly the cause of the WordPress error establishing a database connection.

So you can find the database related details from your host, and update your WordPress website’s wp-config and solve the error.

Once you check and correct, try visiting the website again to see if the error is solved or not.

Reach out to your web host support

If the quick fixes or self trials don’t work, you can ask for assistance from your web host support team.

Thanks to the several moving parts when it comes to WordPress, there’s a chance that you might not know what exactly is causing the problem. Especially if you are an average WordPress user or if you don’t want to further do any damages to your WordPress website. The best thing to do then is to quickly use the WordPress hosting support or to reach out the support team of your hosting provider.

Some of the major web hosts such as managed WordPress hosting providers. And WordPress specialist hosting providers pride themselves on delivering exemplary, instant, 24 x7 support — and this is really a good time to use that to your advantage.

Since most of the support staff are trained WordPress specialists as well. So there are improved odds that they’d be able to fix your issue immediately.

If nothing, your web host support staff will be able to pinpoint the exact problem with your WordPress site.

Recheck WordPress Themes, Plugins, & Files 

Look for any pending updates from your WordPress backend. First, deactivate or remove any of the plugins or themes you don’t seem to need or use at all. These themes and plugins add to the server load, slow your website down, or could potentially be causing the error establishing your database connection.

These unused plugins and themes are also hacker magnets — open invitations to hackers who can place malicious code or scripts into your WordPress site.

Do any of the themes need an upgrade? How about any of the plugins you might be using regularly?

Take the time to do your updates one by one and see if your WordPress website loads normally.

Check if your WordPress website works now. If not, here are a few more advanced ways to fix your error establishing database connection.

Note: Do this only if you know what you are doing

Repair Your Database 

To fix the error establishing your database connection issue and also if you are getting a different error on the wp-admin, for instance, something like “One or more database tables are unavailable”. Then you need to repair your database.

As difficult as it may seem, repairing database is a function in WordPress and you do not need to manually do it. You can simply add a line in your wp-config file and easily repair your database.

Look for your wp-config.php file by logging into your web hosting account (you might have to use your FTP logins, if necessary).

Open the file, and then add the following line in your wp-config.php file. Make sure to add it just before ‘That’s all, stop editing! Happy blogging’ line in your wp-config.php file.

define(‘WP_ALLOW_REPAIR’, true);

Once you have done that, you can see the settings by visiting this page

 

Click on “repair database” and wait for the repair to happen. Hopefully, WordPress tries to establish a database connection or to optimize your connection, your website should work.

Make sure to remove this code from your wp-config.php and reload it back to your servers after you are done optimizing or repairing your database.

Note: You can load this repair wizard even without you having to log into your WordPress backend by just visiting the URL mentioned above.

How to Avoid Error Establishing a Database Connection? 

Once you’ve survived and come through unscathed with the WordPress error establishing a database connection issue, you’d want to not only prevent this in the future but also ensure that your WordPress website is safe, secure, and infallible.

Here are a few simple ways to help you do that:

Use High-Quality Hosting 

If you are using cheap WordPress hosting, it’s time to upgrade to a better, high-quality web host. There are several major players in the web hosting industry such as Bluehost, Kinsta, WPEngine, 10Web, or Google’s Cloud, and several others that you can choose so that:

  • Your WordPress host can handle traffic spikes and allow you to scale at any time.
  • You have reliable, automated backups (up to 30 days or 60 days) of your WordPress (including all the data such as users, content, media, theme files, and plugins). Without these backups (or if you do backups manually), you’ll be at the risk of losing everything you’ve built up so far.
  • You can get access to highly-trained and experienced customer support. And WordPress specialists who can fix these issues for you instantly (without you having to get your hands dirty and further harm your WordPress install, if at all).

Hosting your website on good platforms is very vital for the smooth and efficient performance of your site. It will ensure that your website is available to the visitors and performs smoothly. Moreover, when you rely on a reputable host, they will always ascertain that your website never runs into a problem due to their server problems.

Also, high quality host means access to better support, experienced enough to quickly spot the problem and instantly fix it for you. Good, well-known hosts are also good as they offer you the best security too.

Don’t Hoard Plugins & Themes 

There’s a simple rule when it comes to WordPress: Always update your WordPress core, themes (including child themes), and plugins when updates are available. It takes a few minutes and it really helps you stay safe and secure. It’s not just the database connection errors that you are likely to face.

Outdated themes and plugins can be the cause of many WordPress problems including the White Screen of Death, DDOS issues, hacking attempts, malicious script injections, etc.

To start with, run a cleanup of your website and get rid of unnecessary themes/plugins from your WordPress. If you no longer use them, then you should probably remove them from your site.

A notification will be shown in your WordPress dashboard whenever a new version of the theme, plugin or WordPress is available. And most of the WordPress tools let you update your site very easily, with just one click. However be cautious while proceeding to update, backup your website before you start. If you use a backup tool like BlogVault, it will automatically draw timely backups for you.

So keeping your WordPress themes & plugins updated and getting rid of the extra, unused ones will actually help keeping the error at a bay.

Note Your Credentials, Implement User Management 

First, keep your password secure, change the default login page from “https://yourwordpresswebsite.com/wp-login.php” to something else that only you and your team is aware of. Such as “https://yourwordpresswebsite.com/mysecretlogin.php”.

This will help you to avoid security breach by unauthenticated login attempts or a possible brute force.

If your WordPress website is built like a blog and if you and your team are the only folks who use the WordPress site, just limit sign-ups and access to these users with better password control and access management. Don’t allow any regular subscribers or sign-ups on your WordPress site.

When you do build a WordPress website that allows user access and sign-ups (such as a directory site or a community-based site), implement better user management practices.

Most of the times, hackers change the database name or password to disable the functioning of your website.

Conclusion

This is one of the most common errors that you may face while maintain a WordPress website and there’s nothing to be worried about.

There may be any reason for a problem in database connection in WordPress, there are always different causes and solution. However, trying the simple fixes like the ones mentioned above can help you get rid of the error easily.

As a last resort, if you have secure and latest backup of your website, one of the fixes can be restoring the latest backup of your website.

How did you manage to solve your error establishing a database connection issue? Did we miss out on any potential fixes that you found by yourself? Please do let us know in the comments.

Share This Post:
Disclosure: This page contains external affiliate links that may result in us receiving a commission if you choose to purchase mentioned product. The opinions on this page are our own and we don't receive additional bonus for positive reviews.