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, you will see 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 don’t have SSH access and cPanel Web Hosting users will not usually have this. You can create a new file called database-check.php, paste the below code to it, and FTP the file to the public_html folder then visit it in your browser.
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 commad
php -f database-check.php
The script will retuned one of two values
* There are x tables
This means the script was able to login 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.
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?
If not next try to repair the database. If your use WHM. In Home »SQL Services »Repair a MySQL Database locate the database name and try to repair it. If your site run 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.
At this point, the Error Establishing a Database Connection message should be gone and your site should be working.
* 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 match 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.
How was this article? – Error Establishing a Database Connection
You might also like
More from Web Hosting
Get The Most Out Of High Availability DNS
On the F2Hcloud network we utilize High Availability DNS. This network of DNS servers surrounds our Cloud product and provides …
How to redirect pages or websites with .htaccess files
On Apache servers, you can use a .htaccess file to configure redirects. You can redirect pages to different pages which …
How to host websites by using cPanel and NVMe storage
Host Websites cPanel allows you to host websites by using a GUI or graphical user interface to manage your website. Used …
1 Comment
[…] be corrupted and these will need to be repaired. This is especially true if you’re seeing a database connection error but nothing has changed recently on the […]