How to Fix the Error Establishing a Database Connection in WordPress

Bulletproof Backups for Your WordPress Website

Fortify your business continuity with foolproof WordPress backups. No data loss, no downtime — just secure, seamless operation.

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.

error establishing a database connection notification

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.  

  1. 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.
  2. 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. 
  3. 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. 
  4. 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
  5. 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: 

  1. Find FTP credentials: FTP credentials include server IP or hostname, username, and password. You will find those on your hosting dashboard. 
  2. 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. 
  3. 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. 

  1. 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. 

  1. 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 */
define(‘DB_NAME’, ‘mybhsite_WP1AX’);
/** MySQL database username */
define(‘DB_USER’, ‘mybhsite_WP1AX’);
/** MySQL database password */
define(‘DB_PASSWORD’, ‘password’);
/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);

  1. 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. 
  2. 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.  

  1. 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.
  2. 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. 
  3. 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:

  1. 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
  2. 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. 

  1. 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. 
  2. 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. 
  3. 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. 
  4. 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.
  5. 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. 

  1. 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.
  2. 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.

  1. 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;
  2. 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. 

  1. 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.  
  2. 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.
  3. 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.

Repair database

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:

Using cPanel: 

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: 

  1. Open MySQL Databases: Go to your cPanel and scroll down to your database section. Click MySQL Database
  2. 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:

  1. Connect to your server: Use your application credentials and an FTP client to connect to your server. 
  2. 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:

define(‘WP_ALLOW_REPAIR’, true);

  1. Upload the edited wp-config file: Reupload it back to your root folder and click Continue when prompted to overwrite. 
  2. Run the repair: Type in the following URL in your browser. Replace “your domain” with your actual domain. 
  1. 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

  1. 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. 
  2. 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. 
  3. 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. 

Final thoughts

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. 

FAQ

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.

Tags:

You may also like


Fix: WordPress 403 Forbidden
Fix: WordPress 403 Forbidden

Stuck with a “403 Forbidden” error while trying to access your WordPress site? It’s a little scary and quite cumbersome but you can troubleshoot WordPress errors like this.  It’s a…

Fix: WordPress 413 Request Entity Too Large
Fix: WordPress 413 Request Entity Too Large

Fixing errors in WordPress site is like hitting a digital roadblock. It can be frustrating and disruptive. But, you’re also left wondering why this is happening to you.  The “413…

How do you update and backup your website?

Creating Backup and Updating website can be time consuming and error-prone. BlogVault will save you hours everyday while providing you complete peace of mind.

Updating Everything Manually?

But it’s too time consuming, complicated and stops you from achieving your full potential. You don’t want to put your business at risk with inefficient management.

Backup Your WordPress Site

Install the plugin on your website, let it sync and you’re done. Get automated, scheduled backups for your critical site data, and make sure your website never experiences downtime again.