How to Fix the Error Establishing a Database Connection in WordPress

Error Establishing a Database Connection

If you are using Web for long time then you might have seen the the Error Establishing a Database Connection in WordPress. Sometimes for a beginner it seems scary a little. In this guide I’ll show you how to fix the ‘error establishing database connection’ error.

Why cause Error Establishing Database Connection?

There are many reason behind this error. First of all, lets see why this error occurs.

  • Wrong Database Login Credentials
  • Corrupted Database
  • Corrupted WordPress files
  • Overloaded Database Server

How to solve Database Connection Error in WordPress?

There are some unique methods by which you can fix your database connection error on WordPress. Follow the steps below.

Step 1: Repair your WordPress

First of all, try to repair your WordPress. To repair your WordPress download the wp-config.php file from WordPress root directory and add the line below.

 

define('WP_ALLOW_REPAIR', true);

After adding the lines replace the file with newly edited file from your site. Next, go to the link yoursite.com/wp-admin/maint/repair.php

You will be redirected to a page like-

 

Repair Database

Select anyone from the option to repair your Database. Hope this may work if you have a error with database. If the error still alive follow step 2.

Step 2: Check your WordPress credentials file

Now check if your wpconfig.php file is well configured. Simply this one is the most important file to connect your site to Database. Check the settings that if it is corrupted.

/** 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');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Remember: Here DB_HOST name varies with your Host Provider.

If everything is ok in this file then be sure that your database credentials is free from causing this error.

Step 3: Check your MySQL Server

If step three doesn’t work for you maybe there is a error with MySQL Server. Your database may be busy or overloaded that can’t take loaded properly. This occurs mainly when you are in shared hosting or too much traffic. For this try to use a dedicated hosting.

Sometimes this problem if you have a permission issue with your database. To check whether you have the necessary permission connect your Database through PhpMyAdmin.

Create a file named test.php in WordPress root directory and open the file through text editor. Add the lines below in your newly created php file.

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

Make sure you have replaced username and password with your ones. Save settings and go to the URL yoursite.com/test.php. If you get a message “Connected successfully” then your login details are correct.

But if get a message saying  “Could not connect: Access denied for user ‘username’@’localhost’ (using password: password)” then the problem lies in here.

Create a new username and password and update the file in WordPress to get rid of this issue.

So, this is all for today. I hope this article will help you fixing the Error Establishing a Database Connection in WordPress. There similar articles on WordPress issue. You can read Fix WordPress Session Cookie Error, Fix White Screen Of Death in WordPress and Common Errors in WordPress and How to Fix Them.