How to resolve Error establishing a Database Connection in Wordpress

How to resolve Error establishing a Database Connection in WordPress

Yesterday, one of my websites did not open. And instead, it displayed the content as “Error establishing a database connection“. It was frustrating and once I thought that the sky has fallen on my head. It was not opening on my laptop and I didn’t know that from how long my site was facing this error. Thanks to one of my readers who emailed me with the screenshot and I came to know about this hell.

After troubleshooting for some time, luckily I was able to solve the issue. I thought of writing a blog post on this, covering all the steps that I did and plus, some more steps that you can also take, if you are facing the same situation.

Why Error establishing a database connection comes

If you are little-bit into technical side then the error itself must be clear to you. It clearly says that the site is unable to build an association with your database (DB).

For people from non-technical background, let’s dig into detail.

At a broad level, a wordpress blog is categorized as below:
• Model
• View
• Database

It constitutes the server side files which are written in php language and those generally end with .php extension.

These are the client side files which include html, css, javascript and images.

It is the repository where actual data is stored. Data constitutes every piece of information that is visible on any website; whether it is post, pages or user comments. These are of various types such as Mysql, Sql, Oracle and IBM DB2. WordPress by default uses Mysql.

Why database is most important for any website?

For any website in the world, DB is the most important asset because it is that contains all your articles, your users’ information (their names, emails etc.), administrator login credentials. As any website grows over time, the size of database keeps growing. At any point of time, you can switch your wordpress themes, can activate or deactivate any number of plugins but you don’t want to lose the information

The php code is written in server side files. It connects with DB and fetches all the data that you see on your blog. So, when any user visits your site then the php code queries that information repository and displays those results on your website.

The fetching of information and displaying on website is performed in below steps:
1. Connecting with the database.
2. Firing select query to fetch the records.
3. Actual fetching of the records.
4. Displaying those records (in organized way) on a website.

If your website fails in Step 1 above and it is unable to connect with the data repository then you see Error establishing a database connection on your website.

Hope, the error is clear now to non-technical persons also.

But now the question arises that why wordpress blog is unable to connect with the DB.

Why this Error establishing Database connection comes?

There could be a number of reasons for this, which we have listed below:
The userid and password in wp-config file are incorrect. WordPress php files uses the credentials saved in wp-config file to connect with the DB. And if these are not correct, then your website will not be able to access the data-repository.

The website itself has got corrupted.If wordpress core files have become corrupted then they will not be able to access wp-config file and as such, will not be able to retrieve any information to display on the site.

Database has got corrupted.Suppose you have uploaded a bad plugin or bad theme and it has deleted or corrupted all information at backend then also, you might see this error.

Your webhost server went down.Though there are very less chances of this that your web hosting company’s servers have gone down. But then also, it’s a possibility.

Your blog got spike in traffic.Suppose your blog got too much traffic which your current hosting plan was unable to handle and they deliberately killed the connections from backend; then this error will come. Again, the plausibility is low here.

Your Web Hosting plan expired.If you have not renewed your hosting plan or forget to renew; then obviously, your site will not be able to access the information repository. But this is at the lowest side of possibilities because it’s very unlikely that we do not renew our hosting and still expect the site to function normally :).

Now when we have seen the causes of this error; let’s discuss something on how to fix it.

Fixing Error establishing a Database Connection in wordpress

There are different methods which allow to fix this database error in wordpress. We are going to list all the methods here. Follow these sequentially and let’s see which one works for you.

Method One – Repair WordPress Database

If you can see that your blog is running fine but you get error only while trying to login to dashboard area or if you get some other error such as “The database need to be repaired” then this method one should work for you. This error shows that your database has got corrupted and it needs to be repaired now.

WordPress has a built-in mechanism that allows us to repair the wordpress database on its own. But for it to work, we have to enable the repair feature.

1. Login to cPanel.

2. Go inside File Manager.

Error database connection
File Manager

3. Open public_html directory.

error establishing database connection
public_html directory

4. Download wp-config.php file on your computer.

5. Add the below code at the bottom of file.

define( 'WP_ALLOW_REPAIR', true );

6. Upload wp-config.php file back in public_html folder.

7. Go to your browser and enter

8. You will see below screen with two options. Feel free to choose any one. Just to let you know, we clicked Repair Database.
o Repair Database
o Repair and Optimize Database.

Repair database
Repair Database

9. Once the repairing is done, you should be able to access your website properly.

10. Make sure that you remove WP_ALLOW_REPAIR code that you added in Step 5 and re-upload the file to public_html. This repair feature is just for troubleshooting and you should deactivate it once you are done with it else it can be a security concern for your blog.

11. If still the issue has not resolved then you need to proceed to below Method Two.

Method Two – Check credentials in wp-config file

1. Open wp-config.php file again.

2. Check the userid and password in wp-config file, if these are correct or not. If not, then change them and upload the file back in the same directory.

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

/** MySQL database username */
define('DB_USER', 'username_here');

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

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

As you can see in the code above, there are four variables defined and if any of these variables has wrong value then the connection will stop working.

If you see the incorrect credentials above then you might wonder that how the hell the credentials got changed? There could be possibility that you might have recently moved to a new hosting and you didn’t change the userid and password corresponding to new host. In such a case, your old login credentials will not work and you will see the error on your blog.

If you are on same host then you should inform this to your host that how the credentials got changed.

If you don’t remember your login credentials then also, no need to worry. Let’s test that whether the values defined for above 4 variables are correct or not.

Step A: Test existing login credentials to wordpress
• Create a file testlogin.php.

• Paste the below code into that file.

	$hostname = 'localhost';
	$username = 'username_here';
	$password = 'password_here';

	$link = mysql_connect("$hostname", "$username", "$password");
		if (!$link) {
			echo "

Unable to connect to the server '" . $hostname . "'

	echo mysql_error();
			echo "

Connected to the server successfully '" . $hostname . "'


Please make sure that you replace the username_here and password_here with the ones that are present in your wp-config file.

• Upload testlogin.php in public_html directory.

• Open browser and type

• If the message displays as “Connected to the server successfully” then it means your login credentials are working perfectly. Do talk to your hosting company now and DO NOT go to Step B.

• If you see the message “Unable to connect to the server” or any other error then it means there is problem with the credentials and you need to change them now. Go to Step B to change the userid and password.

• Please note that irrespective of the message you got above (connection successful or not), make sure to delete testlogin.php from public_html directory.

Step B: Create new userid and password
• Login into cPanel. Make sure that you perform the below steps very carefully because any wrong thing can make your site dumb.

• Click on MySql Databases as shown below.

MySql Databases wordpress
MySql Databases

• Create New Database.

Create new database
Create new database

• Go in Add New User section. Enter user name and a strong password. Copy this password at a safe place on your notepad. Click on Create User button.

add new user wordpress
Add New User

• Go to Add User To Database section. Select the above entered user and database and click Add button.

Add User to Database
Add User to Database

• On next screen, make sure you check All Privileges and click the button Make Changes.

All Privileges mysql error
All Privileges

• In wp-config.php file that you downloaded before, replace the old values of username, database and password with the new ones that you just created in above steps. Make sure to keep a backup of old wp-config file.

• Upload this wp-config.php file in public_html directory. If it asks for override option, then proceed ahead and override the existing file.

Open your website in the browser and see if the error has gone away. If not, then try below method.

Method Three – Upload WordPress core files

Hopefully, the above 2 steps must have solved your issue. But if not, then you can try this method too.

1. Go to and download the latest version on your computer.

2. Delete the entire folder wp-content and wp-config-sample.php file. The below highlighted folder and file you need to delete.

3. Go to cpanel –> File Manager –> public_html and upload the whole wordpress files and folders here. If it asks you to override the files, click yes and precede overwriting the files.

error establishing database connection
Upload overwrite wordpress files

4. After upload is completed, check if your site is functioning properly. If No, then you need to try the last method four as mentioned below.

Method Four – Chat with your Web Hosting Company

If none of the above troubleshooting methods have worked then you are left with the last option to chat with your webhost and ask them for resolution. They will be able to help you and you can see your blog running again.

Let us know that by which method you were able to solve the Error establishing a database connection on your wordpress blog.

Leave a Comment

%d bloggers like this: