Home > Help Center > Applications > WordPress > WordPress Database Connection Error

WordPress Database Connection Error

Modern CMS systems such as WordPress keep the entire site content in a database. Sometimes, for various reasons, the site cannot connect to the database, and then the users see in the browser this familiar message:

wordpress database connection error

In this article, we'll look at several reasons that cause the error, as well as the steps that the wordpress site administrator should perform to fix the error and make the site accessible to visitors.

Many articles about Wordpress start with similar reminder - make an archive before doing this or that. This case is not one of them - there is no point in backing up a not working site.

Check if MySQL server is Running

First of all, find out whether the MySQL server is running by loading phpMyAdmin. If you cannot access your databases or cannot open a specific database - contact technical support for assistance.

If the databases and the corresponding tables are responding normally, let's continue with debugging the problem.

If you have a suitable archive

If you have a suitable archive, restore the wp-config.php file and if the site starts normally, be happy with the easy fix of the problem.

However, you should check how the access data in the wp-config.php file has been changed, who has accessed the file, whether there are any changes to other sites in the hosting, in order to detect and correct the cause of the error.

If you do not have a suitable archive

The most common reason for this error to happen is due to a discrepancy between actual database access data and the data in wp-config.php file.

It is a good practice when installing WordPress to save the installation data to a separate file. If you have such a file, open the wp-config.php in a convenient way (via SSH, in cPanel through the file manager or by downloading the file through the FTP and open it on your computer) and compare the entries in both files.

If you find an error, paste the correct information to wp-config.php, save the file (if you downloaded it through FTP upload it back to the server by overwriting the existing file) and refresh the browser.

Check cPanel Username

Note the following - cPanel adds the cpanel user name in front of the database name and MySQL user you are creating. So the complete database name becomes cpanelusr_database and the name of the MySQL user - cpanelusr_username.

These credentials must be saved in wp-config.php instead of just database and username.

Change database user password

If there is no obvious error, it is possible you did not correctly copy the user password inadvertently.

To make sure you are using the correct password, open cPanel > Databases, find Current Users section and click Change Password:

wordpress database connection error

Enter the password from your installation info file or generate new password but you have to update its value in the installation info and wp-config.php file:

wordpress database connection error

Check database user privileges

Now find Current Databases section and click on username link (not on trash icon):

wordpress database connection error

Make sure All Privileges box is checked:

wordpress database connection error

If not, check it and (all other boxes should get checked) and click Make Changes button.

Since all database access credentials are now correct you should not receive Error Establishing a Database Connection.

How to get MySQL error codes

Let's show you another way to check if database access data is correct.

Create new file testconnection.php (name it as you wish) alongside wp-config.php file and paste this code within:

<?php
$con = mysqli_connect("localhost","db_user","password","database");
if (! mysqli_connect_errno()) {
echo "Connected to MySQL Successfully";
} else {
echo "Error: Unable to connect to MySQL." . PHP_EOL.";
echo "Debugging errno: " . mysqli_connect_errno() .";
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
}
?>

Replace db_user, password and database with your actual data, save the file and enter this URL in your browser:

http://hostpulse-demo.com/testconnection.php

If access data is correct you'll see the message:

wordpress database connection error

If not, an error message and error code will be generated:

wordpress database connection error

We intentionally used wrong database name and got error number 1044:

Error: Unable to connect to MySQL. Debugging errno: 1044 Debugging error: Access denied for user 'username'@'localhost' to database 'database'

Then we used wrong username, wrong password and deleted all user privileges in a three different tests and got the same 1045 error number:

Error: Unable to connect to MySQL. Debugging errno: 1045 Debugging error: Access denied for user 'username'@'localhost' (using password: YES)

Using this type of check with generating error number will help you in cases some unusual error has happened to continue debugging in the right direction.

Find description of MySQL Server Error Codes and Messages here.

Check MySQL using SSH

If your hosting plan includes SSH connection you can check very easily if MySQL server is running and if your access data is correct:

# mysql -u username -p database Enter password:

wordpress database connection error

If any of the credentials are incorrect, you'll get the same error numbers (the first test uses with wrong database name, the second and third – wrong username and wrong password):

ERROR 1044 (42000): Access denied for user 'username'@'localhost' to database 'database'
ERROR 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)
ERROR 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)

Because there are only a few possible causes for Error establishing a database connection, using some of the above ways should resolve this error.

Database table is marked as crashed and should be repaired

We want to share with you few more possible MySQL errors and how you can fix them.

If you get an error message when loading the site or opening phpMyAdmin with the following content:

#145 - Table './DB_NAME/hstp_dm _posts' is marked as crashed and should be repaired

You can fix the crashed table in two ways - by using phpMyAdmin or by adding code to wp-config.php.

Repair database table using phpMyAdmin

Open the site's database, highlight the table specified in the error, and selectRepair from the drop-down menu:

wordpress database connection error

Upon successful result you'll get confirmation message:

wordpress database connection error

Let WordPress repair the crashed tables

You can also use an inbuilt WordPress' mechanism to repair crashed tables. Add the following code in wp-config.php just above the line:

define('WP_ALLOW_REPAIR', true);

'That's all, stop editing! Happy blogging'

Now enter this URL in the browser:

http://example.com/wp-admin/maint/repair.php (replace example.com with your site domain)

wordpress database connection error

Click on any button and WordPress will generate report ending with similar message:

Repairs complete. Please remove the following line from wp-config.php to prevent this page from being used by unauthorized users.
define('WP_ALLOW_REPAIR', true);

If single quotes are deleted

One often mistake is to delete by accident any of the single quotes surrounding the values in the wp-config.php file:

define('DB_NAME', 'database');
define('DB_USER', 'username');
define('DB_PASSWORD', 'J&Pg!GRLRU5KD');
define('DB_HOST', 'localhost');

If this happens you'll get php parse error:

Parse error: syntax error, unexpected 'DB_USER' (T_STRING) in /home/cpnl/public_html/hostpulse-demo.com/wp-config.php on line 26

because the closing single quote is missing:

define('DB_NAME', 'database);

Just add the quote and save the file.

Summary

You see fixing this type of errors is not complicated and the described steps will help you do it in no time. It's good to know how to fix errors, but make sure you are working carefully so you do not need fix your own errors.

Still not finding what you're looking for?

Contact our support team with any additional questions or concerns.

Contact support