A "database connection" is one of the most
common WordPress errors that can occur due to a several different
causes. You receive this
error because your WordPress script is unable to establish connection
with the database. We will discuss all causes and recommended solutions
to fix this issue.
Incorrect database login credentialsIt is possible that database login credentials are incorrect or have
been changed recently. You will find these database login credentials at
wp-config.php file under WordPress installation directory.
Testing existing database login credentials
The best way to determine whether you have correct database login credentials is to try login to
phpMyadmin
with connection credentials you have in wp-config.php file. As an
alternative you can also test your existing credentials with following
method:
Create
a php file in WordPress directory and paste the following code. You can
name this file anything you wish. For example: connection.php
<?php
$Connection = mysql_connect('localhost', 'db-username', 'password');
if (!$testConnection) {
die('Error: ' . mysql_error());}echo 'Database connection working!';
mysql_close($testConnection);
?>
Replace your database username and password and save it. Now, browse this file like:
http://website.com/connection.php. You would see either a successful connection message, or an error message. If any of above database connection tests fail, it indicates that you have incorrect database login credentials at
wp-cofing.php.
Checking wp-config filewp-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 have to reflect
it in wp-config file as well. wp-config.php file contains 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,
DB_HOST
value would be localhost but it is not necessary. It may vary depending
on your web hosting provider. You can always contact your web hosting
provider for the exact
DB_HOST value. To fix this, you can reset the database password with the password you found in the wp-config.php file from cPanel.
Database table corruptionIf your website showing
Error establishing a database connection in WordPress website but WordPress dashboard (wp-admin) throws some different error like
One or more database tables are unavailable, then you should consider database repairing.
WordPress 2.9 and later
versions have built-in feature to repair 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 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). You would see above screen with two options to repair,
Repair Database or Repair and Optimize Database. You can choose either one of these options. Please note that
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
database repair page is not secure and to access this functionality, admin user does not need to be logged in. If you do not remove this line, anyone can access this functionality through this URL.