Error Establishing a Database Connection

How to Fix the Error Establishing a Database Connection in WordPress

While using WordPress as your website CMS, you may encounter some technical errors like Error Establishing a Database Connection on your website. Today we will talk about Error Establishing a Database Connection in WordPress website and try to figure out the ways to solve the issue.

What Causes Error Establishing a Database Connection in WordPress?

Actually when a database can’t build the connection to the WordPress files, the web browser shows this error notification. There are lot of causes that may generate this error. Some common scenarios that may cause this error:

  1. Your database server is down
  2. Your databse has crashed
  3. Incorrect database credentials may cause the error
  4. The WordPress database tables have been damaged
  5. Poorly coded Plugin is the cause
  6. Your website may be experiencing a heavy traffic spike, which is beyond your server’s capacity.
  7. Even if your website hosting is not good enough to run your website perfectly, you can face the Error Establishing a Database Connection in your website.

How to Fix the Error Establishing a Database Connection in WordPress

If you are getting the Error Establishing a Database Connection in your WordPress website, move onto the next step.

Note: Before stepping in to fix the error, we advise you to take a backup of your website. At VernalWeb, we keep daily backups with 7 days rentention, still we encourage you to take an offsite backup of your website.

Step 1: Repairing the Corrupted Database

Sometimes, due to a failed upgrade, a database crash, or a faulty plugin, your WordPress database can become corrupted. This problem can present itself as a database connection error. The first thing you can do is try repairing your database.

If you are using cPanel as a control panel at the actual web host, you can easily repair your WordPress database through phpMyadmin.

Additionally, WordPress provides a built-in functions to repair the database. It is disabled by default because it has no access controls and could be a security issue. Below, you will see how we will enable the feature, run the repairs, and then disable it to avoid this potential security threat.

Open the wp-config.php file. On any blank line, paste in the following:

define('WP_ALLOW_REPAIR', true);

This defines a variable that WordPress looks for when determining if it should enable the repair feature.

Save and close the file. Switch over to your browser and load the following address, being sure to substitute with your site’s domain.

http://www.yourdomain.com/wp-admin/maint/repair.php

A database repair page will load. Press the Repair Database button, and you’ll be taken to a results page where you can see the checks and repairs happening in real-time.

Once the process finishes, be sure to open up the wp-config.php file again and remove the line we just pasted in.

Did you notice any repairs being made? Try your site again, and check whether the error is gone.

Step 2: Check Database Login Credentials

If you’ve just moved your WordPress site between servers or hosting providers, you might need to update your database connection details. These are stored on the server in a PHP file called wp-config.php.

Now use your favorite text editor to open the configuration file. Locate the database connection information:

/** The name of the database for WordPress */
define('DB_NAME', 'database_name');


/** MySQL database username */
define('DB_USER', 'database_username');


/** MySQL database password */
define('DB_PASSWORD', 'database_password');

Check that these three variables are correct based on your records. If they don’t look right, update as appropriate, save and exit.

Step 3: Check if Your Database Server is Down

If your website is hosted on a overloaded shared server which can’t afford your website traffic, you may encounter the database connection error. It may cause database server down.

Your site will get really slow and for some users it may even output the error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.

If you have other websites running on the same server, then you can check those sites to confirm that your SQL server is down.

Step 4: Updating the website URL in the Database

Go to your Database using phpMyAdmin and select the WordPress database. After that click on the SQL menu on the top and enter the following MySQL query.

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Don’t forget to provide your own site URL and change wp_options to your own table name as you may have changed the WordPress table prefix.

Step 5: Rebooting Web Server

If you are using virtual private server or dedicated server, you may reboot your server. Rebooting may make your website accessible.

In case steps above didn’t solve your issue, you might need to restore the database from a backup if you have one available. If your website is hosted on VernalWeb, you can easily restore backup in your cPanel.

We hope this tutorial helps you find the perfect solution. If your like our tutorials, definitely you’ll love our support. All VernalWeb hosting plans include 24/7 support from our amazing inhouse support staff. Check out our WordPress Hosting plans and have your website migrated today!