Saifudheen is a young Entrepreneur from Kerala, India who founded MakTal™. This blog aims at helping individuals and businesses with the knowledge he gained throughout his career. Like his facebook page to stay updated.
  • Home
  • /
  • Wordpress
  • /
  • How to Fix Error Establishing a Database Connection on WordPress

How to Fix Error Establishing a Database Connection on WordPress

WordPress is a good open-source blogging platform which can sometimes lead you into troubles. When you get an error saying “Error establishing database connection!”, there is nothing to worry. It should be a silly issue that we can easily fix in a few minutes.

What is this issue?

It simply means that wordpress couldn’t connect to your database. It can happen when either the database or the database user details are wrong! This happens mostly when you migrate your site to a new hosting. Or it can happen when the database server is down or the database is damaged somehow! Following things addresses how to fix the issue.

Fix 1: Check wp-config.php

Make sure that you have entered the right details in wp-config.php file. When you migrate your hosting, you should change these values to the new database credentials you created.

define('DB_NAME', 'your-database-name');
define('DB_USER', 'your-database-username');
define('DB_PASSWORD', 'your-database-password');
define('DB_HOST', 'database-host');

In the above code, you need to replace

  • your-database-name with your own database name
  • your-database-username with your database username
  • your-database-password with your database password
  • database-host with your database host

For most hosting providers, database host will be localhost. When you create the database user, you have to grant All Privileges. Otherwise the database connection won’t be possible.

Fix 2: Test Database Credentials

If you have completed Fix 1 and still facing the issue, you can go with this step. on your public_html folder, create a new file called test.php and put the following code. Do not forget to change database credentials dbhost, db-username, db-password and database-name with your own values.

<?php
$con = mysql_connect('dbhost', 'db-username', 'db-password');
if (!$con) {
die('Database connection Failed : ' . mysql_error());
}
echo 'Connection is working!';
mysql_close($con);

$selected = mysql_select_db('database-name',$con)
  or die("Could not select database");
?>

Save the file and run it by accessing http://yourdomain.com/test.php and see what it shows. If you see

  • Connection is working! – Everything is fine.
  • Database connection Failed: error description – Database user, password or host is wrong. Or user do not have All Privileges.
  • Could not select database – Database name is wrong

These errors may show when the database server is down due to high traffic. You need to contact your hosting support to see whats happening.

Fix 3: Repair Database

If you didnt find a solution yet, a database repair will help! Open your wp-config.php file and add

define('WP_ALLOW_REPAIR', true);

Make sure that you have entered the right database details in wp-config.php, otherwise this won’t work! Now save the file. Make sure that you take a backup before doing this. Once you are done, go to http://www.yourdomain.com/wp-admin/maint/repair.php. Now you will see a window with a button Repair Database and this should fix your problem.

Once you have completed this step, you need to delete that single line of code you have just added to wp-config.php in this step.

Saifudheen is a young entrepreneur from the great India who founded MakTal™. He is a passionate designer, color lover, coder and knowledge giver ;)