Error establishing a database connection in WordPress – Fixed

When you try to use WordPress site, you can see this error a few times.First of all we should know about what’s the meaning of “Error establishing a database connection”?

WordPress is built using PHP and MySQL. PHP functions are that posting comments,retrieving posts and etc.MySQL use to data storage for WordPress site. When you are trying access to your WordPress site, MySQL database shows homepage and images,videos and other media are retrieved by PHP. When PHP can’t access your MySQL database for some reason then you will get “Error establishing a database connection” message on your screen.

 

What are the reasons for this?

  1. Incorrect login details / Incorrect login details in Wp-config.php
  2. Unresponsive database server
  3. Corrupted WordPress files
  4. A corrupted database

Now lets see how to troubleshooting “Error establishing a database connection”

WordPress Database Corrupted

When you are trying to access your site,if the front-end (i.e.yoursite.com) and the back-end (i.e.yoursite.com/wp-admin/) getting the same error message that “Error establishing a database connection” then make sure that you are getting another error like “One or more database tables are unavailable…”.If you are getting this error, your database is corrupted and you need to repair your database.

To repair this you have to do simple steps.

  • First of all, You had better get a Full Backup your WordPress site.
  • Your WordPress site installed in root directory folder. So,first of all you have to locate wp-config.php file from that folder.
  • Then go to your Control Panel and navigate to File Manager.

 

  • Then go to your WordPress root folder.

  • Locate your wp-config.php file.

 

  • Open your wp-config.php file in editing mode,then right click on it and choose the code edit or choose code editor in the menu.

 

  • Then your wp-config.php file will open in a new tab.After that enter the following code just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

define('WP_ALLOW_REPAIR', true);

  • Save your changes. After these all you can see the settings by visiting this page

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

Note:Once you repaired your database, remember to remove the code from your wp-config.php. Because hackers can access to your database.

After repairing the database and still error didn’t fix, then continue reading this article.


Incorrect Login Details and error establishing a database connection

Wp-config.php file is most important file in your wordpress.This file is the connect to your wordpress with database. If you change Database passwords then you have to change those passwords in this file as well. If not, definitely  you will get “error establishing database connection” error.

First, open your Wp-config.php again and check the following code:

 

// ** MySQL settings - You can get this info from your web host ** //

/** The name of the database for WordPress */

define('DB_NAME', 'database_name');

/** MySQL database username */

define('DB_USER', 'database_user');

/** MySQL database password */

define('DB_PASSWORD', 'database_password');

/** MySQL hostname */

define('DB_HOST', 'localhost');

Note:DB_HOST value might not always be localhost.(it will be different depending on host)

Checking Database Name

First you have to check if you have the database name in your wp-config.php. To do this:

  • Open your Control Panel and go to phpMyAdmin.(Under the Database Tools tab).

  • Then you can see phpMyAdmin login screen and log into your phpMyAdmin.
  • Click on the Database tab or you can see the Database names left side bar

 

If you can see DB_NAME in your wp-config.php file from the list of databases,that mean database name not cause for the “error establishing database connection” error.If you can’t see then it might be the problem.So,you need to connect your WordPress hosting provider.

 


 

Checking Database Username and Password

  • First of all you want to create a .php file and save it in your WordPress root folder.Name it testing.php or whatever you want.

 

  • You can do this clicking New File on the menu in your WordPress root folder.

  • After that right click on it and select Code Edit or Code Editor on the menu.

  • Then you will get a blank page.
  • Enter the following code on it:

<?php

$link = mysql_connect('localhost', 'root', 'password');

if (!$link) {

die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_close($link);

?>

Note: Replace ‘localhost’ ‘root’ and ‘password’ with the database host, username and password in wp-config.php respectively.

Save changes and go to yoursite.com/testing.php. If you get “Connected successfully”, that mean your details are correct.If you couldn’t get,that mean there is some other error.

That mean your MYSQL server is not down, it means that your user doesn’t have sufficient permission.

To solve this you need to create new ones and update your new details with wp-config.php.To do this you need to create new user.

 


Creating New Database User and Password

  • Go to your Control Panel and open MySQL Databases.(Under the Database Tool tab)

 

  • After loads your MySQL Databases screen, go to MySQL Users where bottom of the page.Then click on it and select Add New User.
  • Enter your new username and password and click Create User.
  • Imagine –User name is Web_User , password is 1234web$

 

  • After create the new user then assign the user to your WordPress Database (Once go to your wp-config.php file,this is the database that shown in there.)
  • To do this,go to MySQL Databases again and navigate down where you have Add User to Database.
  • Imagine – database name is Web_DB

 

 

  • Then select the user you just created and click Add.You can choose user privileges and save changes.

 

  • Go back to wp-config.php and update the new details.
  • Don’t keep a single space between the quotes.

 

define('DB_NAME', 'Web_DB');

/** MySQL database username */

define('DB_USER', 'Web_User');

/** MySQL database password */

define('DB_PASSWORD', '1234web$');

/** MySQL hostname */

define('DB_HOST', 'localhost');

 

  • Save changes and access to your website.

 


Corrupted Database Files

If still you are seeing “Error establishing a database connection” error that mean there are corrupted files.This is very dangerous because a simple mistake can clear your entire site. First you need to back up your entire WordPress site.

From WordPress, download a fresh copy of WordPress and unzip the package on your computer.Then delete wp-config.php and wp-contents folder.Remember that don’t delete your existing wp-config.php file.

Then using File Manager, upload the other reaming files to your WordPress root folder.

Finally, reload your site and see if it works or not.


If you Still getting  error “Error establishing a database connection” then get Support from your Host Provider, then they will help you online. Sometimes it could be your host’s end problem. Give them to Full Detailes about this it will help them.

Leave a Reply

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