How to Resolve Database Connection Error?

Have you ever come across this error “Error Establishing Database Connection”? As a developer, this error troubles especially when you are new and have not made any changes within the database. What are the causes responsible for this error?

  1. Incorrect login credentials

  2. Unresponsive database server

  3. Corrupt database

Before you begin solving the issue, which in most cases is caused by server error, you need to ensure whether you are getting a similar error in the backend or not. That will determine how you can solve this issue.

Let’s say you are getting the same error, “error establishing database connection”, in the front and backend, and then you can go ahead with solving the issue. But, in case you are getting an error “one or more database tables are unavailable” then you will need to repair the existing database first

Repair the Database

Here’s how you can easily repair your database

Add this code to the wp-config.php file

define('WP_ALLOW_REPAIR', true);

Now, view the settings on the following website

As you are repairing a corrupt database, chances are you may not be allowed to login. You can easily make necessary changes without logging in.

Once you are done with the repair, you will need to clear this define code from the wp-config.php file.

In case, repair is not the solution for the issue you are facing, you will need to implement the following steps

Check for Default Settings in WP-Config File

WP-Config.php is one of the most important files as far as Wordpress installation is concerned. This is the core file for your Wordpress website. The code that will establish connection with database is written in this file. In case you are changing the database credentials, you will need to make necessary changes to this file too. To check for default settings and credentials on this file, use the following code.

define('DB_NAME', 'database-name');

define('DB_USER', 'database-username');

define('DB_USER', 'database-username');

define('DB_HOST', 'localhost');

In this case, the host is a local host, so it has been defined in that way. It is not necessary that your server uses a local host. You will need to search for host values that define the host you are using to connect your server, and enter that value when defining DB_Host.

You can even solve the issue by replacing the host name with your IP address.

Now on running this code, you will know if all the settings on the config file are correct or not. In case, the settings are the same, then it is clear that the issue is at the server side.

Check MySQL Server

There are times when your site is unable to handle the traffic coming its way. It is just not capable enough to handle heavy load, and this can cause your site to slow down. In case, you have more than one site connected to MySQL server, check if the other sites are facing a similar situation or not.

Let’s say you don’t have any other website, except the one facing this error issue then you should go to cPanel, access phpMyAdmin and try establishing a connection with your database. If the connection is established, try and check if you have access to required areas within the database to make necessary changes.

To test the connection and permissions you have, create a file testconnection.php and copy-paste the following code to this file

$link = mysql_connect('localhost', 'root', 'password');
if (!$link) 
die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

After pasting this code, if the connection is established, then your user credentials has been given sufficient access. This simply means, you don’t have any issue related to accessibility and there is something else that’s preventing you from connecting to the database, probably a typo.

In case, you are unable to establish the connection, then it is clear that the issue lies at the server end. There are two possibilities: SQL server is facing downtime or, your user does not have necessary permissions

In the second case, you will need to connect with your hosting provider, and restore the permissions

Note: Whatever changes you make, don’t forget to take a backup of your config files and database

Author Bio: Deepa is a passionate blogger associated with Silver Touch Technologies., a leading Offshore WordPress development company. She loves sharing information regarding WordPress tips & tricks. If you are looking for Hire Dedicated WordPress Developers then just get in touch with her.

Deepa Ranganathan
  • wp database connection error
  • database connection error
  • wordpress
By Deepa Ranganathan On 17 Apr, 15  Viewed: 112

Other blogs you may like

How to Add Custom Icons to Your Navigation Menu?

It's always good to make your designs user friendly! When your design is intuitive, it guides your user towards the right path all the while. Navigation menus are one of the most intuitive areas of your design. You need to make sure the menus talk for themselves. Adding custom icons to your... By Deepa Ranganathan   On 14 May 2015  Viewed: 181

Adding Twitter Cards to Your Wordpress Website

In the past, posting links to your Twitter meant your user needed to click on the link to know more about it. It never showed a synopsis. But, things have changed with Twitter cards. You can attach the title, and a brief preview of the content along with the link. This is almost similar to pasting... By Deepa Ranganathan   On 12 May 2015  Viewed: 1,164

Customize The Admin Post List in Wordpress Website

What do you see when you click on Admin post list in your Wordpress website? A list of all the posts created by you is displayed with their titles and dates. But, that's about it which is displayed! You won't be able to see any other details related to your post. So, if you want to display some... By Deepa Ranganathan   On 08 May 2015  Viewed: 387

5 CSS Customizations for Your Wordpress Website

<center>![Customizations for Your Wordpresst][1]</center> Have you just started using Wordpress? If yes, then there are a few things that you may want to play around with. Customizing a Wordpress theme at beginner level actually means customizing your stylesheets to make the design look... By Deepa Ranganathan   On 06 May 2015  Viewed: 251

Tips to Integrate Facebook to Your Wordpress Website

Social sharing is vital for your website, especially if you want to strike the right chord with your audiences. You don't want to miss out on the conversions that might happen from the different social media websites, do you? While installing a plug-in and accessing social sharing using the plug-in... By Deepa Ranganathan   On 23 Apr 2015  Viewed: 139