6 Ways To Fix Error Establishing a Database Connection in WordPress

How to fix Error establishing a database connection in WordPress

Have you ever encountered the following problem?

“Error establishing a database connection”

Yes or No

For example, when you type in your website’s URL, you don’t see your home page content rather a blank page with these ominous words written on top of it. 

What does it mean when it says Error establishing a database connection?

It means your WordPress website is not able to communicate or has access to its database, and therefore your entire site goes down. 

I know it sounds scary but you don’t worry. This error can be fixed with hacks discussed in this article within just a few minutes. 

  1. What is Error establishing a database connection?
  2. Why do you get Database Connection Error?
  3. Does this error appear for /wp-admin/ as well?
  4. How to Fix WordPress Database Connection Error?
  5. How To Avoid This Error In Future?

What is Error establishing a database connection?

PHP and MySQL are the backbones of WordPress Software. The core programming language of WordPress is PHP and all the information that makes up your website is stored in MySQL database. 

In technical terms, WordPress makes use of PHP commands to connect to the MySQL database, retrieve the information it requires, and then display everything on the screen.

Error Establishing a Database Connection

Error Establishing a Database Connection

Let’s understand this with an example:-

  1. You enter a website URL in your web browser.
  2. WordPress runs its PHP code to connect with the MYSQL database.
  3. Collects all the information required to build that page.
  4. Displays the website page. 

Error establishing a database connection WordPress occurs when the connection between  MySQL and PHP is not working properly. 

That is why you see this error on a blank page. When you can’t connect to the database, there is no way to know what to present; no data of your site is available. 

Why do you get Database Connection Error?

There can be many reasons why there are connection problems between PHP code and MySQL. It is a very common problem and almost every website owner encounters this once in their lifetime.

Following are the reasons behind the WordPress database connection error:-

  • Incorrect database login details- It may be possible that your database credentials are wrong or have been changed recently. MySQL database uses separate logins to establish a connection with WordPress.
  • Your database is corrupt- It may occur in case you have installed a poorly coded plugin, hacker’s attack or deletion of crucial information. 
  • Corrupt WordPress files- Error in establishing a database connection may also happen if your WordPress installation files are corrupted due to failed software update, hackers, etc.
  • An issue with your web hosting server- This is quite common if your website is hosted on shared servers where a lot of users are utilizing the same resources. Therefore, your site goes down whenever there is a spike in traffic or there are too many concurrent connections. 
  • Too much traffic- If there is a sudden spike in your website traffic, your server may not able to handle so many requests at the same time. In that case, you can expect a downtime.

Does this error appear for /wp-admin/ as well?

Yes, it can appear for /wp-admin/ page as well. When your database connection error on your homepage, it is crucial to check if you are getting it on the wp-admin page also or not. 

If it is appearing on both pages, then you proceed with its possible solutions which will be discussed in the later section. 

In case you are receiving a different message on the admin page such as 

“One or more database tables are unavailable. The database may need to be repaired”

You can repair your database by following the given steps:-

  1. Login to your cPanel, go to File Manager and select the folder where you have installed the WordPress. 
  2. Open the wp-config file.
  3. Add this code to the bottom

define(‘WP_ALLOW_REPAIR’, true);

      4. Save your changes and go to http://www.abc.com/wp-admin/maint/repair.php

      5. You will get a message like this from WordPress-

Repair database connection of WordPress

      6. You are free to choose from both the options- Repair Database or Repair and optimize database. The latter will take longer to complete. 

Note:- You don’t need to be logged in to use the above function as its primary intent is to repair a corrupted database. In the majority of situations, users are not able to log in if the database is corrupt. After you are done with the database repairing process, remove the code from wp-config.php so that no one else can access it in the future. 

If your problem is still not fixed, then you can proceed with the steps discussed in the next section. 

How to Fix WordPress Database Connection Error?

Before any attempt to fix the error establishing a database connection in WordPress, we advise you to take a backup! You have the following options to make a backup:-

You can also prevent a bad user experience by enabling caching on your website. Visitors may not even see this error on the front-end if a site is still serving from the cache.

Therefore, it is advisable to check the duration of your cache and increase the same according to your website. 

For example, if your website’s content doesn’t get altered that frequently, enabling the cache can improve your site’s performance as the browser will not have to fetch the fresh files as often as cache expires. 

In the case of error establishing a database connection, the front-end of your website will stay up longer due to cache enabling.

Note:- It will not work if a scrip or part of your website is breaking the cache. 

When a user visits your website and encounters a problem, it will generate a 500 HTTP status code in your logs. It will also appear in the case of an “internal server error”. When there is no error and everything is working fine, then it will generate a 200 HTTP status code.

#1 Check Database Credentials in cPanel

To do this you will need to login to phpMyAdmin in cPanel under the Databases section.

To log in:-

  1. Go to your cPanel account
  2. Under the databases section, click on PHPMyAdmin.
  3. Now log in to your PHPMyAdmin account. 

After successful login to PHPMyAdmin account, you will see a list of databases on your server in the left-hand column. Compare the name against the DB_NAME value in your wp-config.php file.

If they are the same, then this is not the problem. In case they don’t match, you have to update your wp-config.php file. 

You can also check if this is the correct database or not by making sure it contains your website’s url. Follow the steps below:-

  1. Click on the database.
  2. Go to the wp_options table.
  3. Click on the ‘Browse” option next to it.
  4. At the top of the table, you can see your site’s URL and name.
  5. If these match your current site, then this is not the problem.
  6.  If it’s not correct, go ahead and update it in wp-config.

Now, if your database name and website are correct, then you have to check your username and password. 

#2 Test your existing credentials

Create a new php file in your WordPress directory such as testconnection.php  and paste the following code:-

<?php
$test Connection = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$testConnection) {
die(‘Error: ‘ . mysql_error());
}
echo ‘Database connection working!’;
mysql_close($testConnection);
?>

If the username and password aren’t working, we can just create a new one.Once you’ve created that file, just navigate to the URL of the file in your browser (e.g. your site.com/testconnection.php). You’ll see either a successful connection message, or an error with more details.

  1. Create a new database user and password.
  2.  In cPanel, click on MySQL Databases under the Databases section.
  3. Scroll down and create a new MySQL user.
  4. Try and pick a unique username and password so that they can’t easily be guessed. 
  5. Then click on “Create User.” 
  6. You could alternatively also change the password on this screen for the current database user that already exists.
  7. Then scroll down and add your new user to your database. 
  8. The next screen will ask which privileges you want to assign, select “All Privileges.”
  9. Update your wp-config file with the new username and password.
  10. Test your new username and password using the same steps as mentioned above. 

Now your database name, username, and password are all correct. That just leaves DB_HOST.

In most cases, this value will be “localhost,” but it depends on your host’s settings. WordPress has a list of common hosts with their DB_HOST values on their website. If your host isn’t listed there, you can contact your hosting company to ask what value to use.

WordPress Hosting

#2 Check If Your Plugin or Theme Files Haven’t Been Corrupted

You must check if your plugin/ theme files are corrupted or not. You can follow the below steps:-

  1. Connect to your host via FTP using your username and password.
  2. Connect to the server and navigate to the WordPress root folder (“public_html” or “www”).
  3. Go into “wp-content” and rename the “plugins” folder. For example- plugins_.
  4. Go back to your site and see if the error is still there.
  5. If not, then it’s been caused by some plugin. 
  6. Go back to FTP, rename the folder back to “plugins”. 
  7. Now change the folder names of the individual plugins, one by one.
  8. Keep coming back to your site to see when the error gets triggered or disappears. 
  9. Once you’ve narrowed it down to one specific plugin, you have found the troublemaker. 

In case the error is still there even after renaming the “plugins” folder, go through the exact same process with the “themes” folder. If that doesn’t help, rename both folders back to “plugins” and “themes”. The error is caused by something else.

#4 Restore The Default WordPress Files

If the issue is still not resolved, then you can restore the default WordPress files. Database connection error can occur due to corruption in core WordPress files. May be there was an attack by a hacker, malicious plugin or you modified some files by accident. 

Whatever the reason it might be, you can still restore native WordPress files by downloading a clean version of WordPress from WordPress.org and copy it over the one you have right now. 

Steps to restore the default WordPress files are given below:-

  1. Go to https://wordpress.org/download/
  2. Download the latest package to your desktop.
  3. Unpack it and delete the “wp-content” folder
  4. Connect your site via FTP 
  5. Visit the main WordPress root
  6. Upload your new WordPress files and overwrite all the previous ones. 
  7. After it is done, check your website to see if the error has been resolved or not. 

If this also fails, then you must contact your web hosting provider immediately for help. 

#5 Solutions that worked for others

Sometimes the connection error doesn’t go away even after repairing the database and valid login credentials. In these type of cases, the issue can be with the site url. 

To update the same, you can run a SQL query by going to phpMyAdmin:-

UPDATE wp_options SET option_value=’YOUR_SITE_URL’

WHERE option_name=’siteurl’

Replace YOUR_SITE_URL with your actual website url example. For example:- https://serverguy.com/

How To Avoid This Error In Future?

As discussed above there are many ways to fix database connection error in WordPress. One of the most common reasons behind this error is wrong login credentials in the wp-config.php file.

Therefore, you can begin by checking these details and then proceed to the next steps.

No one wants their website to go down. Here are some measures that you can take to prevent this type of error in the future:-

Quality Web Hosting:- It is crucial for every WordPress site owner that they go for those hosting providers who have optimized their platforms for WordPress. Managed WordPress hosting can prevent your site from going down as they ensure 100% uptime and 24*7 support. 

Having Proper Backups:- One can never have sufficient back ups. Therefore, you must ensure that you always have a working back up that you can restore in case of emergency. 

Enabling Cache:- You must enable WordPress cache and choose its duration according to how often you update your website. Your hosting partner can also help you in taking the right decision. 

We hope this guide helps you in tackling this error. If you are still struggling with this issue or want to give us feedback, then you can reach out to us. You can also leave a comment below and we will get back to you.

Latest WordPress Tips, Guides, & News

Stay updated with new stuff in the WP ecosystem including exclusive deals, how-to articles, new plugins, and more. 100% WordPress Goodness, a promise!

4 thoughts on “6 Ways To Fix Error Establishing a Database Connection in WordPress”

  1. Erica S Terner

    Well, this is nice information! Thanks for sharing this as it would help many users to know about important information about WordPress. Knowing about this, we are looking forward to implementing things accordingly.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

We can help you. Right now.

Fast growing merchants depend ServerGuy for high-performance hosting. Experience counts. Let's get started.

Talk to a sales representative

USA / Worldwide

+1.714.2425683

India

+91.9852704704

Core Web Vitals Book COver

Is your website ready for Core Web Vitals?

Take this FREE book with you and optimize your store for speed.

Learn all about new Google new ranking factors and get that top ranking.