How to Fix the Error Establishing a Database Connection in WordPress
You’ve just tried to open your website and to your horror, you don’t recognize it. Gone is the beautifully designed home page. Your carefully curated photos are nowhere to be seen. That blog you spent hours writing? Poof. Disappeared. All you see now is a white screen with an error message: Error Establishing a Database Connection. Before you panic, this is fixable.
What is it? Why did it happen? More importantly…how do you fix it? This article will answer all of those questions.
TL;DR: Having a great backup, with a backup plugin like BlogVault, can be a God-send because you can restore instantly to a recent version with little to no downtime. Once you’re done restoring your site, try the steps in this article to fix the root of the WordPress database connection problem.
What is the Error Establishing a Database Connection
Error establishing a database connection is caused when your website is unable to connect to your database server. Your WordPress database is 1 of 2 integral cogs that make your site run smoothly. In essence, it’s a well-organized collection of important information like posts, pages, media, plugin settings, etc.
Your website server needs a username, password, host, and name to connect to the database for your site to function. If this information on your wp-config file is wrong, the site is unable to connect to the database.
What causes an Error Establishing a Database Connection in WordPress
There are a few reasons that can cause an error establishing a database connection in WordPress and we’ll discuss that in this section. Once you’ve identified what the underlying issue is, we will talk about detailed steps for resolving it.
- Incorrect database credentials: WordPress needs some information to connect to your database server: database name, database server, username, and password. This information is stored in your site’s wp-config.php file. If you have recently installed a plugin or made changes to your core files, this information may be incorrect. Check if this information is wrong and correct it to re-establish the database connection.
- Issues with the web host: This is especially common if you’ve had a sudden spike in traffic and your server can’t handle it. This is great news for you but you’ll need to talk to your host provider. They should be able to fix it on their end.
- Bad plugin or theme: An incompatible or buggy plugin or theme can also affect the site’s ability to connect to the database. Depending on when your site was working last, you can figure out which one is being a troublemaker. Having an activity log for this troubleshooting is a great help.
- Corrupt WordPress files: It could also be caused by corrupted WordPress files. Corrupt files are caused by either a hacked WordPress installation or malware. Use a scanner to check if your site is infected with malware, and get rid of the malware immediately with a WordPress security plugin.
- Corrupt database: It could also be a corrupt database but this is a little less likely. Corrupt databases typically throw up the error “One or more databases are unavailable”. Thankfully, WordPress is able to repair your site database from your cPanel.
Now that you’ve understood your database a little more, what’s next? You need to fix whatever is causing your site so much trouble. We’ll help you identify the clues and troubleshoot the error establishing a database connection issue. Let the investigation begin.
How to fix the Error Establishing a Database Connection problem
There are many ways to fix the error establishing a database connection in WordPress. We discuss them all in this section, starting from the easiest to the most difficult. Keep reading to get started.
Restore your site
This is by far the easiest way to regain your site, when you see the error establishing a database connection WordPress error, with little to no downtime. The method for restoring a backup will vary depending on how the backup was taken in the first place. Check out our guide on how to restore a WordPress backup. The site will also be reverted to whatever version was backed up. So, make sure to choose the current one.
On a side note, make sure to take frequent backups, at least before and after you make changes to your site. We recommend using BlogVault because backups can be automated and scheduled. So, you don’t have to worry about it. Additionally, restoring a site takes just a few clicks and can be done without your admin panel – on the BlogVault dashboard.
If you don’t have a recent backup or want to find out what caused the issue in the first place, there are some more things that you can try.
Check WordPress database credentials
When you’re struggling with the WordPress database connection error, incorrect database credentials are the most common cause of this error and can be fixed by checking if the database credentials in the wp-config file are correct. You’ll need an FTP client and access to your database. Here are the steps to fix the error:
- Find FTP credentials: FTP credentials include server IP or hostname, username, and password. You will find those on your hosting dashboard.
- Install an FTP client: This is what will help you connect to your server and access your files. We’re using Cyberduck, but there are other popular ones like FileZilla.
- Connect to your server: On Cyberduck, click Open Connection and select the correct protocol. We used SFTP. Then add the credentials you found in the first step. Click Connect.
- Open your wp-config.php file: Once you’re connected to the server, look for the root folder. You can identify it because it’s the one with core WordPress folders. For us, the folder is called public_html. Then, double click on wp-config.php and this downloads the file to your local computer. Open it in a text editor once it’s on your system.
- Look for the database credentials: You’re looking for the following piece of code:
// ** MySQL settings – You can get this info from your web host **
/** The name of the database for WordPress */
/** MySQL database username */
/** MySQL database password */
/** MySQL hostname */
- Open MySQL database page: Go to your cPanel or Advanced Settings page and click MySQL Databases in the Database section. This will lead you to a page with all the credentials. Depending on your host, you’ll find the database credentials in different places. We’re using Bluehost to host our site, so we used cPanel. If your host doesn’t provide cPanel, you may find the database credentials on your hosting dashboard.
- Find the database credentials Scroll to the Current Database section and you should find your database name and username. Scroll further down to the Current Users section and check the password. If you cannot view it, you may have to set another one altogether.
- Check your database host name with your host: Get in touch with your host and find out what your database server host is called. It’s typically localhost but some managed hosting companies use their own servers.
- Compare and correct the credentials: Compare the code to the credentials you just found. Make changes to your wp-config.php file if you need to.
- Upload the wp-config.php file: Once you’ve made the changes, save it to your computer, and upload it to your server using your FTP client. At this point, you can check if your site is back up. If not, try the next troubleshooting method.
Check if a plugin or theme is causing the error
The WordPress Database Connection Error can be caused by corrupt or bad plugins and themes. This is typically identified as a problem, soon after a plugin or theme update. While the solution is easy, it is a little time-consuming. So, brace yourself. Here are the steps to do so:
- Open the wp-content folder: You find this folder in your root folder. Open the folder and you should see folders called plugins and themes.
- Deactivate plugins: The easiest way to do so is to rename the folder. We added the word “deactivate” to the end of the name. This will deactivate all your plugins. Now check your site. If it’s not working, remove the word “deactivate” and skip to step number 6. If it is back, continue reading, because you’ve now discovered that a plugin was the problem. Rename the folder to plugins, and double-click to enter the folder.
- Check each plugin: To check which plugin is causing the error, rename all individual plugin folders by adding “deactivate” to the end of their names. Now, reenable them one at a time. Every time you rename a folder, go back and check your site. This process is cumbersome but keep at it till your site is back. Once you do, you’ve found the troublesome one. Bear in mind, that there could be two or more plugins at fault here.
- Fix the bad plugin: If you’ve recently updated this plugin, rollback to an older version. If you can find alternatives to this plugin, keep this plugin deactivated and use the alternative. If not, check WordPress forums to see if this issue has been found by other plugin users and how to fix it. If all else fails, reach out to the plugin’s support team to get help.
- Change back all the plugin names: Besides the troublemaker, go back and remove the word “deactivate” on all the plugin folders. This will reactivate them. You’re now good to go back to your regular programming.
- Rename the themes folder: Add the word “deactivate” to the end of the name and this will deactivate all your themes. Check your site. If it’s still not working, remove the word “deactivate” from the folder and move on to the next troubleshooting method.
- Check each theme: First, reactivate all the themes by removing the word “deactivate” from the themes folder. This will crash your site. Next, go back and rename each individual theme folder by adding “deactivate to the end of it. Check your site to see if your site is back online. Keep at it, doing it one by one, till you find all the troublesome themes.
- Fix the bad theme: If you’ve recently updated this theme, you might have to use the older version or find an alternative. If not, check WordPress forums for your plugin to see if this issue has been found by other users and if there is a way to fix it.
- Change back all the theme names: Apart from the troublemaker, go back and remove the word deactivate on all the theme folders. This will reactivate them.
Replace corrupt files
Your WordPress files could be corrupted by a hack, malware, or user error. To fix this issue, you have to download the correct WordPress files from wordpress.org and replace the existing ones with them. Alternatively, if you have a backup, restore it to a previously working version.
- Download fresh WordPress files: Depending on which version of WordPress you have, download the files to your local computer. It should be downloaded as a zip file and unzip it once it’s on your device. If you’re unsure of which version of WordPress you have, check the version.php file in your wp-includes folder. Look for the following code: $wp-version = ‘x.x.x;
- Upload the core files: Reconnect with your server using an FTP client. Open the root folder. Upload all the new files and folders except the wp-config.php file and wp-content folder. Click Continue when prompted to overwrite. Now check to see if your site is back.
- Add correct database credentials: If that didn’t work, you’ll have to replace the old wp-config file but before you do, correct the database credentials. Go back to your cPanel and copy the correct database username, password, and name. Paste it correctly in the new wp-config.php file.
- Replace old wp-config file: Using the FTP client, upload the new one and overwrite the old one. Check the site. If it hasn’t worked, you can go ahead and repair the database as seen in the steps in the next section. If it has worked, congratulations. You’ve fixed the error.
- Scan your site: Install a security plugin like MalCare, and scan and clear the malware that hacked your site. With MalCare, you can do it all in just a few clicks and keep your site in the future.
While this is a less likely cause for an error connecting to the database, it’s a fairly simple solution. There are two ways to do it: using the cPanel or editing your wp-config file. Here are the steps to do either:
If your host doesn’t have cPanel, you skip this section and skip to the next one that tells you how to add a piece of code to your wp-config file. If you do have cPanel, follow the steps given below:
- Open MySQL Databases: Go to your cPanel and scroll down to your database section. Click MySQL Database.
- Initiate Database Repair: Scroll to the Modify Database section. Select the right database in the drop down menu and click Repair Database. You’ll be redirected to a page that lets you know the repair is complete. Check to see if the site is back. If not, you’ll have to restore your site from a backup. This is a sure-fire way to get your site back.
Editing wp-config file:
- Connect to your server: Use your application credentials and an FTP client to connect to your server.
- Add code to the wp-config file: You can download it from your root folder. Once it’s on your system, open it on a text editor. Add the following piece of code after the last line:
- Upload the edited wp-config file: Reupload it back to your root folder and click Continue when prompted to overwrite.
- Run the repair: Type in the following URL in your browser. Replace “your domain” with your actual domain.
- Initiate Repair: Click Repair Database and give it some time. This will automatically repair your database by searching for common errors. Once done, check to see if your site is back to normal. If not, go ahead and restore your site to the latest backup.
Check your database server connection with the host
Lastly, talk to your hosting provider. Tell them exactly what error you are facing and ask if they can fix it on their end. Ask them to check there is an issue with your database server. This is especially common with shared hosting and is a problem that they can fix on their end.
How to avoid the WordPress database connection error
- Backup: When nothing works, restoring a good backup may be your last resort. But what makes a good backup? Make sure your site is backed up after every change that is made – updates, new posts, etc. Use BlogVault because you can schedule regular and automated backups and in real time. So, you’re always prepared for the worst.
- Security: A general rule of thumb is to have great security. You need to be protected from malware attacks and hacks. With MalCare, you can be made aware of malware attacks with its automatic scans, have the malware removed in one click, and have an advanced WordPress firewall.
- Invest in good web hosts: You need a host that has a great community of people that can help. This is especially important if you can’t solve the problem on your end.
There are quite a few ways to fix the Error Establishing a Database Connection issue but they’re all pretty cumbersome. The best way to be prepared, in the future, is to have a great backup plan, regularly scheduled and automated. This is where BlogVault comes in. Install the plugin, sign up, and your site is good to go.
You can schedule your backups for when traffic is low. BlogVault also offers real-time backups and most excitingly, you don’t have to store them on your computer because all backups are stored on the BlogVault servers. It’s the best for a reason.
Why does it say Error Establishing a Database Connection on WordPress?
Essentially the Error Establishing a Database Connection error is caused by WordPress being unable to access or connect to your database server. Some common reasons for this to occur are that your database credentials in your wp-config file are wrong, your database or WordPress files are corrupted, or your host is experiencing server issues. There are a bunch of ways to fix the issue.
Why do I keep getting Error Establishing a Database Connection on WordPress?
In all likelihood, this is because your database credentials are wrong. This is especially common if you’ve recently changed hosting providers. We recommend you check by checking the database username, name, and password on cPanel or your hosting dashboard, and checking the corresponding details in your wp-config file. Here’s how you do it.
Shreya has been a writer for as long as she can remember. Now, she writes articles that help WordPress users manage the sites that they're proud of, with little to no coding