How to Fix the Error in Establishing a Database Connection in WordPress

How to Fix Error in Establishing Database Connection Wordpress

If you are seeing ‘Error Establishing a Database Connection’ on the homepage or any other page of your WordPress website, it means that there is simply no connection between your website and your database.

Error Establishing a Database Connection

This article will explain what a database is and how it functions in relation to your WordPress site, and also a couple of things you can do to fix the problem if you are facing this issue.

What is a Database?

How WordPress Database Works

Database is just a collection of your data that is stored on your server. WordPress requires a database to function as that is where it stores your website’s data such as your pages, posts, users and other content. All these content are stored separate from your website files so that content can be easily managed and accessed.

Think of it this way. Your website files contain your themes, plugins and configuration files, things that are static and do not typically change on a live website. Whereas, your database is always changing and growing as you add content to your website.

The separation of your static files and content is the basis of how WordPress or any other CMS (content management system) works.

What are the Most Common Causes of the Error in Establishing a Database Connection?

  • Wrong configuration settings in your wp-config.php file, leading to your website not being able to connect to your database.
  • Corruption of your database files
  • Your database server is down

This issue typically happens after completing a hosting migration.

How do we solve the Error Establishing a Database Connection?

It is likely that your database credentials are not set right in your wp-config.php files. The main purpose of the wp-config.php file is to tell your website where to find the right database, and to provide the credentials to access it.

There are 3 important fields within the wp-config.php file that you have to configure in order to connect your website to your database successfully.

  1. Database Name
  2. Database User
  3. Database Password

For a successful connection to your database, the user also has to be granted access to the database itself. This will be covered later.

First of all, we want to dig into the wp-config.php file to check on the configuration. There are 4 lines in the file that will matter:

define( ‘DB_NAME’, ‘database_name’ );
define( ‘DB_USER’, ‘user_name’ );
define( ‘DB_PASSWORD’, ‘password’ );
define( ‘DB_HOST’, ‘localhost’ );
WP Config PHP Database Credentials
Here is example of how these lines look like in your wp-config.php file

Note down the following according to what you see in your configuration:

  • database_name (this is your database name)
  • user_name (this is your database user
  • password (this is your database password)
  • The last line should be exactly ‘localhost’ itself. You should not need to change this, and it should not be any different even after a hosting migration.

The idea from this point onwards, is to make sure that the configuration file is matching your actual database credentials. So the next step is to find out what your database credentials are.

How to Find My Database Name?

MySQL Databases
MySQL Databases Example

To access your database, you will want to go into your hosting control panel and click on MySQL Databases.

Here you will be able to see your database name, note this down. In my example, it is called example_wp101.

What If I Host Multiple WordPress Websites on My Server?
(You can skip this if you are only hosting 1 website)

If you have multiple WordPress sites on a single hosting, then you might have a list of different databases here. In order to find out which database is the one that is supposed to link to your website, we will have to access the tables in the database.

Go back to the dashboard of your control panel, and click on phpMyAdmin. (MySQL Databases has different functions from phpMyAdmin. MySQL Database helps you manage all your databases at the top level, where phpMyAdmin lets you access the information within the database itself.)

phpMyAdmin

In order to identify which the right database is, you will have to check each one manually. Click on one of the database and it should drop down the list of your database tables.

wp-options

Then, you will need to identify the table that ends with “_options”. In this example, it’s wpad_options.

siteurl in WordPress Database

Once you click on that, you will then be able to see the entries where one of the rows will show you your siteurl under option_name and your website address (https://www.example.com) under the option_value. If the website address matches the one in question, then you know that this is the right database.

How to Find My Database User?

Next, you want to go back to MySQL Databases and find the user that is connected to your database. This can be seen under the Privilege Users column that is inline with the Database. Note this down too. In my example, it is called user_wp101.

How to Set the Database User Password?

MySQL Databases Change Password

For security reasons, you will not be allowed to see the password, so you can use the Change Password function instead to create a new one. Make sure to create a strong and unique password, and note this down.
(Before changing your password, check whether your database and user matches your wp-config.php configuration in the ‘Checking if the wp-config.php Credentials are Correct step’, because if they match, then it is likely that your password is already matching)

Add User to Database

In order for the user to access the database, the user has to be granted the privilege to do so. This can be checked under Current Databases, Privilege Users column. If there is no user here, you can use the Add User to Database function, and connect the right user to the right database.

Checking if the wp-config.php Credentials are Correct

Now is the time for you to compare the wp-config.php credentials to the ones that you have noted down. If the database and user does not match, then you simply have to change the values in the wp-config.php file to the ones you have noted down and you should be able to successfully load your WordPress website.

Still Not Working? Here’s How to Fix Corrupted Database for WordPress Website

You can identify a corrupted database when you see an error message saying “One or more database tables are unavailable. The database may need to be repaired”.

The issue is slightly different from the first one. The first one is not being able to access your database at all. While for this case, your website is able to access the database, but is somehow not being able to read the tables from the database.

The simplest fix to this is to use WordPress’s database repair function. This function is not available to use right out of the box, and must be activated first. To activate it, go to your wp-config.php file and add in this line before “That’s all, stop editing! Happy blogging’:

define(‘WP_ALLOW_REPAIR’, true);

Once you have done that, you will get access to the following page: (change example.com to your domain name)

http://example.com/wp-admin/maint/repair.php

You can then proceed with 2 choices, either ‘Repair Database’ or ‘Repair and Optimize Database’.

Once you are done, remember to go back into your wp-config.php file and remove the code that we added earlier. This is important as anyone that goes into the URL will have access to the database repair function even if they are just a visitor and not logged into your WordPress admin.

Still Not Working? Here’s How to Check If Your Database is Down?

Under your control panel, click on phpMyAdmin. If you are not able to load your databases and tables at all, then it is a huge sign that your database is down at this point.

To verify, you can also create a php file to test out the connection to your database by adding the following code to a newly created file called databasetest.php:

<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Database is down, ‘ . mysqli_error());
}
echo ‘Success!’;
?>

Then load example.com/databasetest.php and you will be able to check if your database is down. (change example.com to your domain name)

If you have determined that your database is down, you should reach out to your hosting support to get it checked.

If you are using a self-managed VPS, you can also run the following commands via SSH to check on the status of your database:

sudo systemctl status mysql

or

sudo service mysql status

Getting Further Help

If at this point things are still not working even after trying all the above, perhaps it’s time to get a professional to help. You should first contact your hosting support, as most of the time, they have the ability to look into your configurations and fix the problem for you.

If you are not getting the right help, you could also hire an affordable WordPress troubleshooting service, such as https://www.wpfixit.com/.

Leave a Reply

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