Few things are as frustrating as finding that your website is down – particularly if you rely on it for business. The “Error Establishing a Database Connection” message represents a serious issue and prevents all access to your site, so fixing it is a top priority. However, if you’re not familiar with how WordPress works, it can be a confusing problem. Although this error is serious, it’s also highly fixable. With a few troubleshooting steps, you can have your site back online in no time.
What is the “Error Establishing a Database Connection”?
Nearly all website information – including post data, page data, meta information, plugin settings, login credentials, and more – is stored and organized in a MySQL database. The only data that isn’t stored there is media content such as images and your theme/plugin/core files such as index.php, wp-login.php, etc. Your website is reliant on a server-side language called PHP.
When a visitor comes to your site, WordPress uses PHP to query the database and pull the correct information, which is then displayed as the complete page. If, for whatever reason, WordPress can’t access your site’s database or it isn’t working properly, the result is an “Error Establishing a Database Connection” message.
The “error establishing a database connection” means your website is no longer communicating or has access to your WordPress database, and thus your entire website goes down. This error prevents the entire page from loading. 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.
If your site uses caching, visitors may still see stored copies of your pages. Therefore, if you catch the error early and resolve the problem before your site’s cache refreshes, you can avoid too many interruptions to your site and business. Fortunately, as WordPress errors go, a database connection error is usually pretty simple to resolve.
What causes “Error Establishing a Database Connection”?
The error establishing a database connection arises because MySQL-PHP connection is severed for some reason. Any of the following scenarios may cause this error:
- Incorrect database login credentials: Possibly the most common cause of the ‘Error Establishing a Database Connection’ is simply that WordPress has incorrect login credentials for your database. This could be either the database name, username, or password. Remember, these login details are different from the ones you use to access your site.
- Database corruption: A WordPress MySQL database contains a lot of information. If any part is deleted or corrupted, the result can be the error in question. Corruption can result from manually tinkering with the database, but it can also just happen as a consequence of normal use. Although MySQL is quite robust, nothing is perfect, and errors do occur.
- WordPress core file corruption: Similarly, corruption can occur in the core WordPress files that make up your site. Even though these files are outside the database, they may sometimes result in the same error.
- Problems with the web server or hosting provider: If problems arise with your host or server (if you’re self-hosting) and WordPress can’t reach it to query the database, it will throw this error. These problems can include outages, data loss, and hardware failures.
How to fix “Error Establishing a Database Connection”
Although this is a serious error, it’s fortunately fairly easy to resolve. Here are instructions for troubleshooting and fixing the problem.
1. Check your WordPress database credentials
Since this is the most likely cause of the error, it should also be the first step in your troubleshooting. The first thing you’ll need to do is locate the credentials WordPress is currently using to access your database. This information is stored in your site’s wp-config.php file. You can use cPanel or FTP/SFTP to access it. Here, look for three pieces of information – the database name, username, and password.
With this information in hand, head to your MySQL Databases. Make sure the database name matches the one you pulled from wp-config.php earlier. If it doesn’t match, go back to wp-config.php and update it with the correct database name. Make sure the username or password matches the ones on wp-config.php file, if not, then update them with the correct details.
2. Check your database host information
If you’ve checked the database login credentials and fixed any errors, but you’re still getting the “Error Establishing a Database Connection” message, the next thing you should check is your database host information. The hostname for your database can be found in wp-config.php, right alongside the database name and login credentials.
Note this information, then navigate to MySQL Databases using cPanel or FTP/SFTP, make sure the hostname in your wp-config.php file is listed on this page. If it’s not, you’ll need to add it. When you’re finished, click on “Create this MySQL hostname” now. Note that it can take a few hours for this new hostname to propagate through the DNS, so feel free to take a break here. When some time has passed, come back and check if your site is working.
3. Repair your WordPress database
If you’re still receiving the error message, you can try repairing the database to fix possible corruption. You can use the built-in WordPress database repair tool for this. To access it, open up your wp-config.php file and add the following code at the end:
Next, open a new browser tab and navigate to https://yoursitename.com/wp-admin/maint/repair.php, replacing “yoursitename” with your website’s actual domain. This will bring up the database repair tool. Click on ‘Repair Database’ and let it do its thing. You can choose ‘Repair and Optimize Database’ if you like, but it takes considerably longer. Either way, when the tool is finished, load your website again and check for the error.
If it’s gone, then you’ll know a corrupted database was the cause. However, if you’re still seeing the error message, head to the next step. Before you do, make sure to head back into wp-config.php and delete the code you added to turn on the tool. If you leave it there, someone with ill intentions could gain access to your site easily.
4. Check if your database server is down
If all the above steps have failed, one possibility is that your database server has gone down. At this point, it’s a good idea to verify with your hosting provider that everything is working correctly. There are a number of reasons why your database host or server might be experiencing issues:
- Too many simultaneous connections to the database: Some providers have limits on how many connections a server can have at one time.
- Problems with another site on your shared hosting server: If you’re on a shared hosting plan, you’re splitting resources with other sites. If one of them has problems, it can spill over to your site. This is one of the reasons dedicated or managed WordPress hosting plans can be beneficial.
- Hardware troubles: Hardware eventually fails, and it’s possible that the server your database is stored on has done just that. Ideally, your host will have redundancies in place so that if one server goes down, there’s another copy of your data available, but this isn’t always the case – especially if you’re hosting your own database on a home or office server.
Your best bet here is to reach out to your web host and inquire about outages or other known issues.
5. Update your WordPress site URL
If you’ve recently moved your WordPress installation for any reason (such as moving to a new domain name or migrating to HTTPS), you may need to update your site URL in the database.
6. Reboot your web server
This one might seem a little basic, but sometimes the simplest solutions are the most effective. If you host your database on your own server, try turning it off and back on. Computers are complex, and a simple reboot can fix all manner of odd problems you may be experiencing.
7. Contact your host for help
If you’re not comfortable doing your own troubleshooting, or you’ve tried everything and still can’t get rid of the “Error Establishing a Database Connection” message, there’s no shame in reaching out for help.
If you’ve gone to check on your website and found yourself greeted by the “Error Establishing a Database Connection” message, you’re not alone. It’s a common error with relatively simple fixes, so getting your site up and running again shouldn’t be too difficult.