How to Fix the Error Establishing a Database Connection in WordPress

Error establishing a database connection is a most common error in WordPress which occurs for many reasons. Commonly this error occurs while you update, install, migrate WordPress one server to another server. It is most frustrating when you did not change anything and this error occurs.

In this article, we will show you all possible reasons and solutions of error establishing a database connection.

Error Establishing a Database Connection

Common reason of Error Establishing a Database Connection in WordPress

These are the main cause of an error establishing a database connection:

  • Wrong MYSQL database login details
  • WordPress database is corrupted
  • Database server is unresponsive
  • MYSQL Database User Has Limited Privileges

Wrong Mysql Login details:

This is the most common cause of the error establishing a database connection. Check your wp-config.php file. It contains all details to establish a connection to the database. Open your wp-config.php file via FTP or default file manager. Check the following details are correct or not.

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

If you change your host and migrate the WordPress to another server and successfully edit the database name, user and password correct, this error commonly occurs due to “define DB_HOST” Because Different hosts use different hosting environment and this value is different in many Hosting. Netzole Hosting uses localhost and if you face problem, please open the ticket at support. Mostly Hosting uses localhost and some uses the different value. Many times this error fixed by changing the hostname to the IP address by defining as follow:

define('DB_HOST', '');

WordPress database is corrupted

Many times you did not change anything and this error occurs. It may be due to the corrupt database and need to repair the database. WordPress provides a functionality to repair the database from its backend. For this follow the following steps:

How to repair corrupted Database in WordPress

First add the following line to the wp-config.php file.

define('WP_ALLOW_REPAIR', true);

After adding the above line, type the given link to the browser and a main setting page will appear to repair the database.


Click on “Repair” or “Repair and Optimize” and your database will be repaired in a few minutes. Do remember after repairing the database, delete the above function line from the wp-config.php file because this function does not require the login and due to this anyone can go to repair setting page to mess up with your database.

MYSQL Server is down

If your site has heavy traffic, you can get this error during the peak hours due to high database queries and some of your users may see this error. In this situation, contact your hosting provider to know your Mysql server is down or not. If you have others site on the same server, both sites should be down due to server down. If one site is up and the other is down then your database server is not down. If you have only one site, You can test the server status by creating a new file in your installation root directory and name it testconnection.php and paste the following code into this file.

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

Make sure to change mysql_user and password value with username and password respectively into line 2. Type file address in the browser and test. You can see page “”Congratulation Connected successfully”” if it connected successfully. If not connected use value ‘root’  in place of ‘mysql_user’ If you get the access denied error through testconnection.php results or connecting to your phpMyAdmin, contact to your hosting provider.

MYSQL Database User Has Limited Privileges

If any above method is not working, then apply this method. Go to MySQL database via Hosting Cpanel. Delete the current user that is connected to the database. After deleting the current user, Create the new user and give the full privileges to the new created User. Now open wp-config.php file and replace old database username with the new username and save it.