Looking for ways to remove the “error establishing database connection” warning from your WordPress website?
As the name suggests, the “error establishing database connection” warning occurs when your site can’t connect with your database. The WordPress database is where all your website data is stored. From usernames to passwords, from posts to pages and comments, even the theme and WordPress configuration settings are stored in the database.
There could be several reasons why your site can’t connect to your database. In this article, we will take a detailed look at the causes and show you how to remove the warning from our WordPress site.
Let’s get started.
What is the “Error Establishing Database Connection” Warning?
A database is a space where all your website data is stored in an organized manner. All posts, pages, users, login information, meta information, site configuration, plugin and theme configurations, site title, widget layout, color settings, etc are stored in the database.
It’s worth noting that some data like images, wp-config.php, wp-login.php, and other core files are not stored in the database.
When a visitor tries to access a page on your site, the site first connects with the database and then requests data. The database responds with the requested data and the site shows the data to the visitor. The “error establishing a database connection” warning appears when your site is unable to connect to the database.
There are several ways to remove the error from your site. We’ll show you how in the next section. First, let’s take a look at the reasons why your site can’t connect to your WordPress database.
What Causes the “Error Establishing Database Connection” Warning to Appear
To connect with the database, WordPress needs the following information: Database name, username, password, and server. This information is stored in the wp-config.php file. If the file gets corrupted, WordPress shows the “error establishing database connection” warning on the frontend of the site.
Besides corruption of the wp-config.php file, other reasons that cause the error include accidentally changing the database login credentials, problems with your hosting provider like hardware failure, data loss, and power outages.
The database connection issue can also occur if the database has been manually tinkered with. That said, normal usage like installing and deleting plugins can also lead to errors.
Another reason you are experiencing a problem with your database could be a sudden spike in traffic. For instance, if one of your posts went viral or if your site was under a DDoS attack, your site will experience a sudden spike in traffic. This will cause the database to become unresponsive because it’s not used to handling a large amount of traffic.
No matter what the cause is, there are ways to fix the “error establishing database connection” warning on your WordPress website. We have covered the solutions in the next section.
How to Fix the Database Error on Your WordPress Site
The fix depends on the cause of the database connection failure. Since there are a number of reasons why a database connection fails, there are a number of solutions too.
It’s hard to pinpoint the exact reason so we recommend trying out all the solutions in this article.
IMPORTANT: Every time you try a solution, you will need to check whether it helped remove the error from your site. But even if the error disappears, you might still see it on your site’s front end because of the browser cache. Make sure you clear the cache every time you check the site.
Google Chrome allows you to clear cache via the setting option and it’s a time-consuming process. So we recommend doing this instead –
Add this to the end of your site URL: ?x=aojfkesj
The URL should look like this: https://yoursite.com/?x=aojfkesj
Whenever you want to clear the browser cache, just replace ‘aojfkesj’ with a random string of letters like this:
Now, let’s dive into the solutions.
1. Check if Your Database Server is Down
Your hosting server could be down due to reasons like power outage, data loss, or hardware failure. It could be down because it’s undergoing scheduled maintenance in which case you should have received an email about it. We recommend checking your emails for any information on scheduled maintenance.
If the tools show that the server is down, then your site should be back to normal within a few hours.
2. Restore Backup
One of the easiest and quickest ways to solve this issue is by restoring a backup of your website. Just be sure to select a copy made before the error appears on your site.
Also, check if your backup plugin offers a test restoring feature that will allow you to quickly restore backups on their server.
If your backup plugin does not offer a test restoring feature, then we recommend creating a temporary WordPress site on your computer and checking the backups on that site. Building a WordPress site locally is not as hard as it sounds but for a beginner, it will be a bit time-consuming for sure.
Alternatively, you can try some of the other solutions below.
3. Check Your WordPress Database Credentials
One of the most common causes behind “error establishing database connection” is changes in the WordPress database credentials.
The database credentials are stored in the wp-config.php file. When the credentials in the wp-config.php file don’t match with the real credentials, that’s when you see the database connection error. So let’s check if the credentials in your config file match with your real credentials.
Step 1: Open your hosting account and go to cPanel.
Then select File Manager and go to public_html.
In the public_html file, you should find the wp-config.php file.
Right-click on the config file and select the View option.
Scroll down in the wp-config file and you should be able to see the following credentials:
define('DB_PASSWORD', 'xxxxxxxxx'); define('DB_NAME', 'xxxxxx'); define('DB_USER', 'xxxxxx');
Make a note of the password, name, and user. Then we’ll check if these credentials are correct.
Step 2: Go to cPanel and select MySQL Databases.
On the next page, you should find the database name, username, and password. See if it matches with the one on your wp-config.php file.
Some hosting providers don’t show you the password on the MySQL Databases page. In that case, you will require help from your hosting support team to verify the database credentials.
If the credentials on the wp-config file are wrong, then replace them with the correct ones. And that should remove the “error establishing database connection” warning from your website.
Step 3: Database credentials don’t just change on their own. Someone must have modified them. We recommend using a security plugin to scan your site thoroughly.
If your site is hacked then the security plugin that scanned your site will help you clean it.
4. Check Your Database Host Information
Make sure that your wp-config file has the correct host information. Open the file and you should be able to see something like this:
It means that your database is hosted on a local host. Most hosting providers use the local host to host databases but some managed hosting providers use separate servers. So if you see a local host as your hosting server, reach out to your hosting provider and replace ‘localhost’ with the name of your actual database host.
Even if your site is not hosted on a managed hosting server, we still recommend checking the host information with your hosting provider.
5. Update Your Site URL
To remove the “error establishing database connection” warning from your WordPress site, try updating your site URL in the database.
Open your hosting account, go to cPanel, and select phpMyAdmin.
On the next page, select the database from the left-hand side of the screen, then go to SQL, add the following line, and hit the Go button.
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
Remember to replace YOUR_SITE_URL with the URL of your site.
This will update the URL of your site in your database. Afterward, check your website to see if the warning persists.
6. Repair Corrupt Database
The WordPress MySQL database is a robust system but it’s not perfect. Things do go wrong which is why the database has the ability to fix itself to some degree. Here’s how you can initiate the fix:
Step 1: Open the wp-config file by right-clicking on the file and selecting Edit.
Scroll down to the end of the page and place the following sentence right before ‘That’s all, stop editing! Happy blogging,’ as shown in the image below:
Remember to hit the Save button before exiting the page.
Step 2: Next, open this URL: http://site.com/wp-admin/maint/repair.php
Replace ‘http://site.com/’ with the URL of your website.
Step 3: A new page should appear with the Repair Database button. Select it and let the database repair itself.
Then, remove the line of code you added to the wp-config.php file and check if the error has disappeared from the site.
7. Repair WordPress Core Files
Another reason you could be seeing the error is because one of your WordPress core files is corrupted.
Perhaps someone manually modified the file which could be a sign of a hack.
The good news is, that you can fix corrupt core files by replacing the core version of WordPress on your website.
Don’t worry, this won’t affect any of your themes or plugins. You won’t lose any of your posts, pages, and site configurations.
Step 1: All you need to do is open this URL and download the latest version of WordPress.
Then Unzip the file on your computer and delete the wp-config-sample.php file.
Step 2: Now, upload the rest of the files and folders to your WordPress site. Just go to the File Manager and select the upload button.
Make sure you are replacing the existing files.
8. Reboot Your Server
This may seem very basic but a simple reboot might just fix the problem.
Reboots are only possible on VPS, local, and dedicated servers. So if you are using a VPS, local, or a dedicated server, then log into your hosting account and look for a Restart or Reboot button. Clicking on the button should reboot your server and fix the error.
9. Disable Plugins
Many problems on a WordPress website stems from a plugin. Let’s find out if one of the plugins installed on your site is causing the “error establishing database connection” warning to appear.
Open your hosting account and go to cPanel > File Manager > public_html > wp-content > plugins.
Right-click on the folder and select the Rename option. Rename the plugins folder into something else like plugins-old. This will disable all the plugins on your site. Now check your website.
If the warning still disappear then one of the plugins was the culprit. Now, all you need to do is find the culprit plugin. Here’s how: Rename the folder back to its original name. Next, open the folder and start renaming each plugin. Every time you rename a plugin, check your website. Keep doing it until the error disappears and you find the culprit plugin.
10. Seek Help
Are you still seeing the error? If yes, then it’s time to seek help from your hosting provider.
Once you connect with the hosting support team, they are likely to tell you to take the steps we have listed in this article. Just tell them that you have already taken these steps and are itching out to the support team because none of it worked.
The “error establishing database connection” is a common WordPress error and luckily it is very easy to fix.
To fix the error, one first has to find the cause. There are a number of reasons why the error could have appeared on your site. It could have been caused by a sudden spike in traffic, changes in the database credentials, constantly installing and deleting plugins, among other things.
It’s hard to pinpoint what exactly caused the error on your site. That’s why, in this article, we have covered 10 different ways of fixing the database connection issue. You will need to try them all one by one to see which one removes the error.
If nothing works, then you need to find a professional to take a look at your site.
That’s all for this one folks. If you have any questions about the “error establishing database connection” warning, let us know in the comments below.