MySQL/MariaDBWordPress

WordPress Error Establishing a Database Connection

You will see the Error Establishing a Database Connection message when visiting your WordPress site if your WordPress website is unable to connect to your MySQL database. This error is caused by only a few possibilities so it’s usually easy to pinpoint the problem.

1) Database Credentials – wp-config.php

This might be obvious but has the database information changed? In the wp-config.php file, note the current database details. If you have changed the username or password or even the entire database the new details need to be placed in this file.

 /** The name of the database for WordPress */
 define( 'DB_NAME', 'database_name_here' );
 /** MySQL database username */
 define( 'DB_USER', 'username_here' );
 /** MySQL database password */
 define( 'DB_PASSWORD', 'password_here' );
 /** MySQL hostname */
 define( 'DB_HOST', 'localhost' );

2) Check Database Credentials

If the details have not changed and you are sure the details in the wp-config.php file are correct the next thing to do is test the credentials. We can do this with a quick PHP Script.

This check will tell us

  • Is the MySQL Server Online
  • Are the database credentials correct

If you use a High Availability Web Hosting account you will not have access to SSH. You can still create the file from the cPanel File Manager. Create a new file called database-check.php and paste the below code to it. Ensure the file is placed in a public location like the public_html folder. You will navigate to the file in a browser in the next steps.

If you have SSH access. Login to the server and create a file called database-check.php

nano database-check.php

Next, paste the below code to the file. Replace the $db sections below with the information from the wp-config.php file

<?php
# Fill our vars and run on the file on the command line
# $ php -f database-check.php

$dbname = 'database-name'; # REPLACE WITH wp-config.php details
$dbuser = 'database-user'; # REPLACE WITH wp-config.php details
$dbpass = 'database-pass'; # REPLACE WITH wp-config.php details
$dbhost = 'localhost';

$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");

$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);

$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}

if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "Credentials OK - There are $tblCnt tables<br />\n";
} 
?>

Now, save and close the file and run the following command

php -f database-check.php

The script will return one of two values.

There are x tables

This means the script was able to log in to the database. The wp-config.php details are correct. If this is the case the problem is either;

  • The wp-config.php file is malformed
  • There is an error in it or your database is corrupted.

wp-config.php

First, rename your wp-config.php file to wp-config.php.backup then enter your database details into the wp-config-sample.php file. Rename the file to wp-config.php. Does this resolve the error?

Repair WordPress Database

If not next try to repair the database. If you use WHM. In Home Â» SQL Services Â» Repair a MySQL Database locate the database name and try to repair it. If your site runs out of space it’s possible the database was corrupted. If you only have access to cPanel click PHPMyAdmin and then the database you want to repair. Select all the tables and click repair database.

You can repair a MySQL/MariaDB database from the command line with mysqlcheck.

mysqlcheck database_name

There are no tables

This means the script was unable to login to login to the database. The details in the wp-config.php are wrong. Login to cPanel and select MySQL Databases. Ensure the database name database user matches what is in the wp-config.php file. Next update the password for the database. Under current users click “Change Password” and place the new password in the wp-config.php file. The issue should now be resolved.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button