How to Fix WordPress Database Connection Error

The error message ‘Error establishing a database connection’ appears when WordPress isn’t able to salvage data from your website’s database. WordPress stores all the meta information, post data, login information, plugin settings and page data in its database. Albeit, the themes, images, and WordPress core files are not stored in the database. So whenever you or anyone else access your website, a PHP query is executed which gets/retrieves the desired data from the database and when for some reason that functionality gets broken (i.e data can’t be retrieved from the database), this error pops up which simply means that WordPress wasn’t able to retrieve the particular data from the database.

Error Establishing a Database Connection

This error message is quite generic and if you have ever used WordPress on your web server, you are very likely to have come across this error message at least once. Nonetheless, in this article, we will be covering the possible causes of the said error message and then proceed with providing a list of solutions that you can implement to get the issue resolved. So, let us get started.

What causes the ‘Error Establishing a Database Connection’ Error Message in WordPress?

 The reasons which cause WordPress to be not able to connect to its database server can be plenty. The most common and often ones are mentioned down below:

  • Invalid database login credentials: The most prominent reason for this error to occur is having invalid login credentials used by WordPress to establish a connection with the database server. If the login credentials used by WordPress to connect to its database server are not valid, then you will face this error as WordPress would not be able to establish a connection with the database due to wrong login credentials.
  • Corrupt Database: If your database has been corrupted, then it’s most likely that you are going to get this error. By corruption, I mean that some of the tables/columns in the database might be altered and WordPress isn’t able to get the desired data values from those particular tables/columns in the database.
  • Database server issues: Another common cause for this error is having a down database server. For example, if you have hosted your website on a web server somewhere and the database server resides there with it too but it is not online or can’t be connected to then you will get this error.
  • Corrupt WordPress files: Another possible reason for this error to occur is having corrupt files in your WordPress installation directory. Often, hackers can get access to your WordPress website and modify the files in your WordPress directory by adding new files or deleting the existing ones or tweaking with your original files. Well, whatever the case may be, but if you have got corrupted files in your WordPress directory, then you may be getting this error due to it.
  • Insufficient PHP Memory: The error message can also arise if your web server does not have sufficient PHP memory. In such a case, you will have to update the php.ini file to increase the PHP memory.

Solution 1: Review your wp-config.php in your WordPress installation directory

The first thing you should do while proceeding to fix this error is to go and have a look at the wp-config.php file in your WordPress directory. You should see whether the contents of the wp-config.php has been altered with or not.

To do that, you can either use the file manager provided in your web hosting’s cPanel for accessing the wp-config.php or you can use an FTP application like Filezilla to connect to your web server and access the file on a text editor installed on your local computer.

Once you have access to the wp-config.php file, see that there are these lines present in it:

define('DB_NAME', 'database-name');

define('DB_USER', 'database-username');

define('DB_PASSWORD', 'database-password');

define('DB_HOST', 'localhost')
FileZilla Manager

The value of the DB_HOST might not be localhost if the database server is not having the same IP address as of your web server (this can be usually the case when you have not hosted your website locally on your computer).

Solution 2: Try establishing a dummy connection to your Database server

The next thing you should do is to see if you can establish a successful connection to the database server using the username and password for your database user account.

Create a new file testdatabaseconnection.php in your website’s root directory with the following contents and access it from your browser. If you get the message “Connected successfully” then there is nothing wrong with the MySQL database server and it’s working fine.

<?php

$link = mysqli_connect('localhost', 'username', 'password');

if (!$link) {

die('Could not connect: ' . mysqli_error());

}

echo 'Connected successfully';

mysqli_close($link);

?>
Test Database

Remember to change the username and password fields with the username and password for your database user account.

In case, you are wondering about the username and password of the database user account then you don’t need to worry. You can see it in your cPanel of your web hosting account in the database section. Or if you had made/configured the database yourself then you will probably remember the username and password.

If you get an error message instead of the connected successfully message, then you need to make sure that you are using the correct username and password to connect to your database server.

An alternative here would be to make another user for your database server with all the privileges and use that same user account to access your database.

After you have created a new user account, you can test that account with the above method and if it is successful then you need to change the contents of the wp-config.php to match with the username and password of the database account that you have just created.

Solution 3: Repair your database (if it’s been corrupted)

If doing the above steps fail and you are still not able to connect to your database, then you should be considering repairing your database because it’s likely that it may have been corrupted. The good news here is that WordPress has a repair database mode which you can initiate just by adding a line to your wp-config.php.

  1. To do that, add this line in your wp-config.php.
    define('WP_ALLOW_REPAIR', true);
  2. After adding and saving the file, go to this link to initiate the Database repairing mode.
    https://yourdomain.com/wp-admin/maint/repair.php
  3.  Change yourdomain.com with the domain of your website.
  4. Once you have accessed the page, click on the option “Repair Database”.
    Repair Database

It will take some time and will finish. Afterwards, you should go and check whether you are still facing this error or not.

Solution 4: Executing SQL Query in PhpMyAdmin

A user suggested that he got rid of the said error message by executing the following SQL query in PhpMyAdmin in the web host cPanel (PhpMyAdmin is a web-based application for managing different aspects of your database).

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Remember to change the YOUR_SITE_URL with the link of your website.

After doing that, you can see if your error has been fixed or not.

Solution 5: Reinstall WordPress core files (Don’t worry your website will not get affected)

After trying out all the above solutions and getting nothing to work for you, you should try reinstalling the WordPress core files.

  1. To do that, head to WordPress.org and download the latest WordPress zip file.
  2. After that connect to your webserver using Filezilla and navigate to the WordPress installation directory.
  3. There, delete the folder ‘wp-content‘ and the file ‘wp-config-sample.php‘ and upload the WordPress zip file that you downloaded. Once uploaded, you need to extract the contents of the zip file there in that directory (not to a subdirectory).
    Removing WordPress
  4. The zip file extraction can be done either through Filezilla or by using the file manager of your cPanel as most webservers have the support of extracting zip files.
  5. That’s it. Now check again to see if your WordPress website is still facing the same error or not.

Expert Tip

How to Fix WordPress Database Connection Error

If the issue is with your Computer or a Laptop you should try using Reimage Plus which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Reimage by clicking the Download button below.

Download Now

I'm not interested

Close