Fix Error Establishing Database Connection in WordPress

If you have a website or if you are running a blog for sometime then getting errors is not new to you. Among all kinds of errors, Error Establishing Database Connection is a common one that will frequently cross your way. So it is always useful to know about this error. Any kinds of error displayed in your website means you could lose the trust of your visitors and subscribers. So it is better if you take actions against errors as quickly as possible. This guide will give you idea of how to fix Error Establishing  Database Connection in WordPress.

Fix Error Establishing Database Connection in WordPress

Fix Error Establishing Database Connection in WordPress

Introduction to Error Establishing Database Connection in WordPress

MYSQL database is the heart of your website and has all the vital information needed to lead and run your website. Database is the storehouse of all data of your website like your contents, image, user information, settings, login details and many more. To load any website, PHP code in WordPress communicates with database and requests the required information. When the PHP trying to load your website cannot access the database then this error occur. The error simply means for some reason connection to your database could not be made.

Reasons for occurrence of Error Establishing Database Connection
  1. Incorrect database login credentials
  2. Unresponsive database server / Server that host your database is down
  3. Corrupted database

Like any other errors this error is fixable too. The most common solutions that are known to work are as follows.

Note: Back up before jumping to any action. This is a precaution you should always follow.

1. Double check database credentials

Database uses separate login credentials. If these credentials are incorrect then you may not be able to access the database. If you have recently done any of the following actions chances are your database login details is changed.

  • Installed WordPress using the “Famous 5 steps install”
  • Changed host or domain names
  • Moved from a localhost to server host

So cross check your login credentials and make sure you are using the correct username and password. Gather some important information required to login

  • Database name
  • Database host
  • Database username
  • Database password

You can collect these information from the cPanel under MySQL database. The best way to get correct detail is under your host panel. You can also ask your host or check their support documentation for detail.

Next step is to open wp-config.php file from your WordPress installation files. The wp-config.php file can be found in the root of WordPress installation in the same location as /wp-admin/ and /wp-content/ folders. In the wp-config.php, look for the following lines of code.

Fix Error Establishing Database Connection in WordPress

Replace the second parameter of define function with the detail you had collected earlier. For example

In line define(‘DB_NAME’, ‘database_name_here’); replace database_name_here with your database name.

Finally save the file. And check if the problem is solved.

2. Repair corrupt database

If you get the error in wp-admin or in the backend as well then you need to carefully read the error message and see what it is saying. If your error message in backend (wp-admin) is saying something like “One or more database tables are unavailable. The database may need to be repaired”, then your database is corrupted and you should follow this step of repair carefully.

Follow these steps.

  1. Connect to your website using cPanel or FTP and then open root folder of WordPress installation.
  2. Search and open wp-config.php file
  3. Add the following code in the file
define('WP_ALLOW_REPAIR', true);

4. Finally save the file.

Also Read:   7 Best Author Box Plugin in WordPress

Once you have added the code in wp-config file, you can visit the repair page by going to the address.

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

Replace yoursite with your website URL. This should open a database repair page.

Now you can repair and optimize your database using this repair page. For security reasons after the completion of repair action, don’t forget to remove the code which was added above in wp-config file.

3. Check your Web Host (MySQL Server)

When your website gets tons of traffic, you can get this kind of error. If you are using shared host then your server may not handle excess traffic resulting in Error Establishing Database Error. In this case you have to test the server. If you have multiple website in the same server, check other sites for same error. If other websites are too having the error then the problem is with your Web server.

In case you are running single site in your server, then you may need to perform few tests to confirm that the problem is being caused by your site.

You have to find if your database is getting sufficient permission. From the cPanel try connecting database from phpMYAdmin. If you cannot connect to phpMyAdmin then that is another confirmation that something is wrong with server.

If you can connect to phpMyAdmin then create a new file called testserver.php or whatever name you like then paste the following code

localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Replace the username root and password in above code with your database username and password. The code will simply try to connect to your database and then return if connection was successful or not. When you open the file from browser, an unsuccessful connection confirms that you do not have sufficient permission to your MySql server. With all the above tests if you find the fault of your server in creating the error then immediately contact your web host and tell them to fix the problem.

We hope our article helped you fixing this Error Establishing Database connection. You can also check our article on ‘’Fixing the 500 internal server error in WordPress

The following two tabs change content below.
Kantiman Bajracharya

Kantiman Bajracharya

Kantiman Bajracharya is a freelance web developer. He is a computer geek who has a Bachelor’s degree in Computer Engineering. He has earned lots of satisfied customers while working for numerous successful projects. He is also a WordPress theme creator and web article writer. On free time he likes reading philosophy books, traveling and playing chess.

Share This Post