In this article we cover the common “error establish a database connection in WordPress” message that can sometimes be displayed when a website is unavailable.
First up, we look at some of the causes of this error, before exploring the different ways it can be fixed.
Although this article doesn’t dive too deeply into the technical details behind the causes of this problem and the ways to fix it, this article should give you a good overview of this issue to help you understand it better and speak about it more confidently.
So with that in mind, let’s begin…
What Causes a WordPress Database Connection Error?
Unlike some errors that you might see on a website or on your computer, such as the 404 error or a warning that an illegal operation has been performed, the error that we’re looking at today at least has a self-explanatory name or output message.
So when you see the Error Establishing a Database Connection message when trying to access a WordPress website, it really does mean that the WordPress software can’t connect to its underlying database.
As this database is where all of the WordPress website’s content, settings, files, and more are stored, it’s a pretty big problem if the site can’t make the connection. However, even though it is a serious issue, it doesn’t mean that your site is broken beyond repair. As we’ll see in the solution section to this guide to understanding this WordPress database error, it can be fixed.
Unfortunately, though, the causes for this problem do vary and there’s no one explanation for when your WordPress website can’t connect to its database. Also, as there’s no single cause for this problem, there isn’t a single solution to the WordPress database error either.
However, there are some common causes of the WordPress database connection error that we’ll quickly cover here before moving on to sharing some tips on how you can fix this issue.
Incorrect Database Details
One common cause of this error is that the details which WordPress is using to connect to the database are incorrect, including the database name, username, and password.
If WordPress can’t access the file that contains these details – the wp-config.php file – or the contents of that file doesn’t match the credentials of the database, then you’ll see the database connection error.
Causes that could lead to this problem arising include changes to the database username or password that haven’t been reflected in the wp-config.php file, or the renaming, deletion, or corruption of the wp-config.php file itself.
Corrupted Database
Another cause of this error could be the fact that the database has been corrupted or damaged in some way. If this happens, then WordPress might not be able to connect to it, producing the error establishing a database connection message to be displayed instead of the website and its content.
Overloaded Database Server
Another cause of this issue can be that the server hosting the database is overloaded. When this happens, the files on the server can become unavailable, causing a connection error.
This cause is especially common if you’re using cheap shared hosting for your WordPress website. With this type of hosting, multiple websites and their databases are stored on the same server, sharing the same resources and increasing the chances of an overload. Due to this, it might not be your website that’s causing the problem, and instead, one of your neighbors on the same server.
The good news is that if this is the cause, there’s a chance the error will resolve itself, once the load on the server has reduced.
How to Fix a WordPress Database Connection Error
While it’s a big deal when a WordPress website is unable to connect to its database – mainly because the site becomes unavailable — thankfully, it’s often a problem that can be fixed.
Although in many cases, you can fix this problem yourself, your first port of call should be your web hosting support desk. Most good web hosts have a priority support channel you can use if your website is unavailable, which it will be if the error establishing a database connection message is displayed. But whatever your level of service, get in touch with your web host help desk via the most responsive method, which is usually live chat or telephone.
If you want to try and fix the problem yourself, the tips below should help you to solve this issue.
Don’t Forget to Check Your Backups
Before you start trying to fix this issue, make sure you have a working backup of your WordPress website and its database. If you’ve been using one of our recommended WordPress backup solutions, then there’s a good chance you’ll already have an archive of your website to fall back on should something go wrong, or if you’re unable to fix the database connection error. Your backup strategy should include testing your backups to ensure you’re able to restore them when they’re needed.
If you haven’t been backing up your WordPress website, then you could try contacting your web host to see if they’ve been taking backups of your site on your behalf.
If there aren’t any recent backups of your website, you could still be able to take one. While you won’t be able to log in to the WordPress Dashboard to create a backup when you’re seeing the database connection error message, you could create a backup at the server level, using the tools provided by your web host. These tools can often be found in the cPanel. Again, contact your web host if you’re unsure about how to create a backup outside of WordPress.
Any backups created after the database connection error has occurred will still contain the error, but at least if you make any mistakes while trying to fix the problem, you’ll be able to roll back to when you created the backup and try again.
Fixing the WordPress Database Connection Error
So, now that you have your backup files somewhere safe and accessible, here are some of the ways it might be possible to fix the error establishing a database connection in WordPress issue.
Incorrect Database Details
As mentioned earlier, if the details, such as the database name, username, and password, in the wp-config.php file don’t match the actual details of your database, WordPress won’t be able to connect to the database.
So to check if this mismatch is the cause of the error, you’ll need to locate and open your wp-config.php file and check those details yourself. The wp-config.php file can be found in the root directory of your website on the server where it’s hosted. To access that area, you’ll need to log in to the server via FTP or use an online file manager to access the file.
If you’re not sure how to do this, you’ll need to find the documentation provided by your host, or contact their support staff.
Once you’ve found the file, be sure to create a backup of it before making any changes. Once that’s done, open the file for editing, by either downloading it from the server or through the file manager interface.
As the image below shows, the lines you’re looking for start with // ** MySQL settings – You can get this info from your web host ** //
In that section of the wp-config.php file, you can see the following details:
- The name of the database: DB_NAME
- The database username: DB_USER
- The database password: DB_PASSWORD
- The database hostname: DB_HOST
Now you’ll need to check that those details match the details for the database. To do so, you’ll need to access the database. Your options here will be defined by your web host. But one popular way to access the database is through your web hosting control panel, such as via the phpMyAdmin tool found in the cPanel interface.
Once you’ve launched phpMyAdmin to access the database, you need to confirm the details from the wp-config.php file that we just covered. This is perhaps the most difficult part of the process so it’s best to refer to this guide on the Kinsta blog that covers how to check the credentials of your WordPress database to see if they match the details in your wp-config.php file.
If at this point you’d rather seek support from your web host than proceed on your own, you can send the help desk staff the database details from your wp-config.php file to help them get started.
Overloaded Database Server
If the cause of your database connection error is an overloaded server, then there’s a good chance the problem will resolve itself once the burden on the server has decreased.
Unfortunately, there’s no quick and easy way to determine if this is the cause of the error. While your Google Analytics data could reveal that your website is experiencing a surge in traffic, it could be one of the other websites hosted on the same server that is causing the overload.
If you contact the support staff of your web host, they should be able to tell you if this is the cause of your issue.
If an overloaded server is causing downtime for your website, then it might be time to consider changing your hosting plan. One option would be upgrading to a higher plan with your current provider, or switching to a better web host.
Final Thoughts
Hopefully, this article has shed some light on the infamous error establishing a database connection problem that can affect WordPress websites from time to time.
In most cases, your best option will be to contact your web host and instruct them to resolve the issue. However, the information in this article should have given you a better idea of what’s caused the problem and some of the ways it can be fixed.
Fixing your website yourself can be rewarding. But in most cases, its best left to the professionals.
Has your website ever displayed the error establishing a database connection message? If so, what did you do to resolve it? Please let us know in the comments below.
I am searching on google how to fix database connection error problem and I find your post. Thanks in advance! hopefully it will work!