List of content you will read in this article:
If you face "Error establishing a database connection" while using your WordPress website, it becomes hard for your user to visit your website without any trouble. Generally, this error occurs when your WordPress cannot create a proper connection to the database. Hence, many people face this issue, but they don't know how to resolve this issue from their WordPress website. So in this guide, we will give you a brief on What is "Error establishing a database connection" and how you can easily resolve it.
What is "Error establishing a database connection"?
Your database uses a language called MySQL, and your website depends on a server-side language called PHP. Whenever your website URL is entered into the browser, WordPress uses PHP commands to connect with the MySQL database, get the information needed, and then display everything on the screen.
At this point, WordPress stops accessing the information in the database via PHP commands. When that happens, WordPress doesn't know what to do next, so the only thing it can say is, "Error establishing a database connection."
Reasons of Error Establishing a Database Connection
There can be a handful of reasons why the error occurs but some of the more common ones:
- Incorrect login credentials: WordPress needs a specific login and password to access the database. If those have been changed for any reason, WordPress will no longer fetch anything from the database.
- Corrupted WordPress files: The core WordPress files can get corrupted due to a failed update, among others. It includes failed updates of the plugins, themes, and even the main WordPress software updates.
- Corrupted database: This might result from a problematic plugin messing up the database internally, a hacker’s attack, a theme failing, and many more.
- The database server is down: In some cases, the web server (the server that stores your website files) is run on a different machine than the database. And that separate database server can be down for whatever reason.
- Too much traffic: Even though this is a good problem, it’s still a problem. Specifically, your database might be unresponsive due to a high spike in traffic.
How to Fix Error Establishing a Database Connection
We will show you how to fix the error by setting a database connection in WordPress by compiling a list of solutions all in one place. Remember, before you make any database changes, make sure you have sufficient backups.
Get In Touch With Your Web Host Provider
It's safe to assume that if your site was working just a minute ago (or a day ago) and now it isn't, then the fault can be on your web host's end. Contact your web host provider's support and ask if the database server is up and running with no problems and if there isn't any spike in activity or traffic on the server. In most cases, if you tell them what sort of error you're having, they will let you know what the cause is exactly, or they will even fix it for you.
Check If Your Plugin or Theme Files Haven't Been Corrupted
First, connect to your host via FTP. You can do this with a free tool called FileZilla. To connect, you need a login and a password to your FTP account. If you don't know what those are, the easiest way to get them is to search through the initial emails you got from your web host when you first signed up.
After that, connect to the server and navigate to the WordPress root folder. It's usually called "public_html" or "www," or you might be in it already once you establish the FTP connection. The rule of thumb is that if you can see subfolders like "wp-content" and "wp-admin", then you're in the WordPress root. After that, go into "wp-content" and rename the "plugins" folder to something else.
Now, go back to your site and see if the error is still there. If not, you now know that it's been caused by some plugin. Go back to FTP, rename the folder back to "plugins". Go in it, and change the folder names of the individual plugins, one by one.
Keep coming back to your site to see when the error gets triggered or vanishes. Once you've narrowed it down to one specific plugin, you have found the troublemaker. Either delete it or go online to see if the solution has been documented.
If the error is still there even after renaming the "plugins" folder, go through the same process with the "themes" folder. In case that doesn't help, rename both folders back to "plugins" and "themes". The error is caused by something else.
Check If Your Database Hasn’t Been Corrupted
The first thing to check is to go to your WordPress dashboard – YOURSITE.com/wp-admin If you see the same main error: "error establishing a database connection", skip this step. In case you see something else, pay close attention to what WordPress is suggesting you should do. WordPress has expanded its problem-solving solutions in its recent versions so that you might see some good advice over there.
Another possibility is that you'll see a message starting with "One or more database tables are unavailable" This means that it's you who needs to "repair" the database. First, connect to your site via FTP once again. Go to WordPress root and download the "wp-config.php" file to your desktop. Edit this file in notepad, add one extra line at the end (right before "That's all, stop editing"):
Now upload the file back to your site (via FTP), overwriting the old version.
Go to YOURSITE.com/wp-admin/maint/repair.php
Click either of the buttons and once the automatic repair finishes, go back to your site and see if the error is still there.
Suppose there are no errors, great! But you still need to revert your "wp-config.php" file to its previous state. Remove that new line that you just added and re-upload the file.
Restore the Default WordPress Files
WordPress core files can get corrupted for several reasons. Maybe there's been a hacking attempt on your site, possibly you modified some files by accident, and maybe a rogue plugin did the modification.
Whatever it might be, you can still restore the default, native files of WordPress. The easiest way to do that is to download a clean version of WordPress from WordPress.org and copy it over the one you have right now.
Download the newest package to your desktop. Unpack. Browse through the unpacked archive and delete the "wp-content" folder. Next, connect to your site via FTP, go to the main WordPress root, upload your new WordPress files. Overwrite all the previous files. When the upload is done, go back to your site and see the effect.
We hope you found this guide helpful and that you could solve your WordPress "error establishing a database connection" problem. However, WordPress experts recommend using managed WordPress hosting to reduce the stress by handing over the technical challenges to professionals.
Check Your Database Connection Credentials
WordPress uses a specific username and password to connect to the database. Normally, those access credentials don’t change on their own. However, you can experience some weird scenarios after making any changes to your site files, the hosting environment, or anything else. In other words, even though it shouldn’t be happening, the access credentials sometimes do change. And with bad access credentials, “error establishing a database connection” occurs. The fix is done in the “wp-config.php” file. I’m talking about these lines specifically:
They hold all the details that WordPress needs to connect to the database. If any of these are wrong, no connection will be established. All you need to do is take the new credentials and put them in your “wp-config.php” file where the old ones exist:
define('DB_NAME', 'some_db'); /* this is the name of your WordPress database */
define('DB_HOST', 'localhost'); /* this is the web address of your database server */
At this stage, you can go back to your site and see if the error has been fixed.
So it was the complete information on "Error establishing a database connection" and how you can easily resolve it from the system. We have included different procedures to eliminate this issue because probability and the reason behind this error can be anything. If you like this guide, then visit our official website because there are a bunch of informative guides available, and you can also use our services because we provide amazing services such as DNS server, Dedicated server, Web hosting, VPS hosting, and so on.