Error Establishing a Database Connection – WordPress

What this error actually means?  The establishing a database connection error means that for some reason the PHP code was unable to connect to your MySQL database to retrieve the information.

How to Fix Error Establishing a DB Connection Error

Note: Before you make any database changes, make sure you have sufficient backups.

Method 1: Repair the Database.

Your database can easily be repaired using a built-in WordPress feature.

  1. Open wp-config.php file and add following code:
    define( 'WP_ALLOW_REPAIR', true );
  2. Navigate to www.yoursite.com/wp-admin/maint/repair.php
  3. Click on Repair Database or/and  Repair and Optimize.  Repairing and optimizing the database can take a long time and the database will be locked while optimizing.
  4. Remove the following line from wp-config.php to prevent this page from being used by unauthorized users.

If this repair did not fix the problem, you can find another solution to work.

Method 2: Checking the WP-Config file

If you changed your root password, or the database user password, then you will need to change this file as well.

  1.  Open wp-config.file  and finf following code:
    define('DB_NAME', 'database-name');
    define('DB_USER', 'database-username');
    define('DB_PASSWORD', 'database-password');
    define('DB_HOST', 'localhost');
    

    Remember your DB_Host value might not always be localhost. Depending on the host, it will be different.

  2. Login to PHPMyAdmin
  3. Click on your database.
  4. Find the table named wp_options and click on the Browse button next to it.
  5. Compare and edit, if necessary data in wp-config.php file.

Additional suggestion is  replace localhost with the IP, like:

define('DB_HOST', '127.0.0.1:8080');

Method 3: Fix Corrupted Files

  1. Download a fresh copy of the WordPress CMS from WordPress.org.
  2. Unzip it in your drive and delete the whole wp-contents folder and the wp-config file so there’s no chance of overwriting your current wp-config file. All the hard work you’ve put into setting up themes and plugins up till now won’t be lost.
  3. After deleting the infected or corrupt files, paste the rest of the files into your WordPress root folder using the File Manager or  FTP client.

Method4: Check your Web Host (MySQL Server)

  1. Often  error establishing database connection could be if  your site gets swarmed with a lot of traffic. In this case you should call hosting provider and ask them if your MySQL server is responsive.
  2. You can create test.php file with following code :
    <?php
    $link = mysql_connect('localhost', 'root', 'password');
    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_close($link);
    ?>

    Make sure to replace the username and password. If the connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong.

  3. If you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server. It does not necessarily means that your MySQL server is down. It could mean that your user does not have sufficient permission.

 

The Error Establishing a Database Connection error is one of the most frustrating errors you can encounter with your WordPress website. There are several different possible root causes for this error making it difficult to identify and resolve. The small typo in wp-config.php file can make it happen.

One thought on “Error Establishing a Database Connection – WordPress

Comments are closed.