How to fix Error establishing a database connection in WordPress Prindi

  • 0

An error establishing a database connection in WordPress is a worst nightmare for all WordPress beginners. It is one of the most common WordPress error that can occur due to a several different causes. In this article, we have attempted to cover all possible solutions to fix it.

What is this error and why it occurs in WordPress?

This is the most common error and does not specifically related to WordPress. 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. First, we will determine why WordPress is unable to establish a database connection.

Cause 1 :: Incorrect database login credentials

The most common reason behind this error is incorrect database credentials. It 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.

How do I test my 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.

What is wp-config file?

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 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.

Fix

To fix this, you can reset the database password with the password you found in the wp-config.php file from cPanel.

Cause 2 :: Database table corruption

If 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 unavailablethen you should consider database repairing.

Fix

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.

Cause 3 :: Check if MySQL server is up and running

This error can also occur when your website is bumped with lots of traffic or your WordPress webite is having 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 shared Host, you can contact your Web Hosting provider and ask them if their MySQL server is responsive. If you own VPS or Dedicated server, connect to your server through SSH and fire following commands to check status of MySQL server.

/etc/init.d/mysqld status
service mysqld status

Kas see vastus oli kasulik?

« Tagasi

Powered by WHMCompleteSolution