How to Fix ‘Error Establishing a Database Connection’ in WordPress?

The 'Error establishing a database connection' in WordPress is the worst nightmare for WordPress beginners. Unfortunately, it is one of the most common WordPress errors due to several causes. This article has tried to cover all possible solutions to fix the Error in establishing a database connection.


What is Error Establishing a Database Connection and why does it occur in WordPress?

Error Establishing a Database Connection is the most common error and does not specifically relate to WordPress. You receive this error when the WordPress script cannot establish a connection with the database. We will discuss all causes and their recommended solutions to fix this issue. 

First, we will determine why WordPress cannot establish a database connection.

Cause 1:: Incorrect Database Login Credentials

The most common reason behind this error is incorrect database credentials. Database login credentials may be incorrect or have been changed recently. You will find these database login credentials in the wp-config.php file under the WordPress installation directory.

How do I test my existing database login credentials?

The best way to determine whether you have the correct database login credentials is to try logging in to phpMyAdmin with the connection credentials in the wp-config.php file. As an alternative, you can also test your existing credentials with the following method:

  • Create a php file in the WordPress directory and paste the following code - 

<?php
$Connection = mysql_connect('localhost', 'db-username', 'password');
if (!$testConnection) {
die('Error: ' . mysql_error());}echo 'Database connection working!';
mysql_close($testConnection);
?>

  • You can give any name to this file, for example, connection.php
  • Replace your database username and password and save it. 
  • Now, open this file in the browser - http://website.com/connection.php.
    You would see either a successful connection message or an error message. 
  • If any of the above database connection tests fail, you have incorrect database login credentials in wp-cofing.php.

What is the wp-config file?

The wp-config.php file is one of the most important files in WordPress installation. It contains your website's installation settings and configuration details, such as database-connection information. When you change your WordPress database user password, you will also have to reflect it in the wp-config file. The wp-config.php file contains the following 4 database connection parameters -

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

In most cases, the DB_HOST value would be localhost, but it is not always necessary; it may vary depending on your web hosting provider. Of course, you can always contact your web hosting provider for the exact DB_HOST value.

Fix

To fix this, you can reset the database password with the password you found in the wp-config.php file from cPanel. 
Refer to our Knowledgebase article on how to reset the MySQL database password.

Cause 2:: Database Table Corruption

Suppose your WordPress website shows the Error establishing a database but the WordPress dashboard (wp-admin) throws some different error like One or more database tables are unavailable. In that case, you should consider database repairing.

Fix

WordPress 2.9 and later versions have built-in features to repair the WordPress database. To enable this feature, you'll have to access wp-config.php and add the following line –   

define('WP_ALLOW_REPAIR', true);

Once you add this line to the wp-config.php file, navigate to www.website.com/wp-admin/maint/repair.php.
(Do not forget to replace "website.com" with your actual URL).

Wordpress Database Repair

You would see the screen shown above with two options to repair, Repair Database or Repair and Optimize Database; you can choose either one of these options. 

Please note that the Repair and Optimize Database option will take longer.

Once you have repaired and optimized your database, make sure you remove this line from your wp-config.php because the Database repair page is not secure, and to access this functionality, the admin user does not need to be logged in. 

Anyone can access this functionality through this URL if you do not remove this line.

Cause 3:: Check if the MySQL Server is Up and Running

This error can also occur when your website is bumped with lots of traffic, or your WordPress website has too many concurrent connections. If you are on shared hosting plans, your Web Host might have restricted the number of MySQL connections, or their MySQL server is too busy/ unresponsive.

Fix 

If you are on a shared hosting plan, you can contact your Web Hosting Provider and ask if their MySQL server is responsive. For example, if you own VPS or Dedicated server, connect to your server through SSH and fire the following commands to check the status of the MySQL server.

/etc/init.d/mysqld status

service mysqld status

That is all you have to do to fix 'Error Establishing a Database Connection' in WordPress.



Was this answer helpful?

« Back

chat