Error Database Connection - Featured Image Database connection error in WordPress is fairly common unlike other errors such as WordPress white screen of death and plugin conflicts. When you encounter a database connection error in WordPress, you should not panic since it is an error you can easily fix. To understand why database connection error occurs in WordPress, you need to know how WordPress works!

WordPress is a Php application that utilizes MySQL database to store data. If you have seen how to install WordPress on localhost – Xampp or WampServer, you will note that WordPress only works when the database configuration is right.

WordPress used an SQL database to store data, every time you publish a post, create a page or add media to WordPress it is stored in the database. When visitors come to your site, WordPress queries the database and returns the data:

Fixing Database Connection Error When you get this database connection error, it means WordPress is not able to communicate with the database. The flow of information illustrated in the image above has been interrupted resulting in a database connection error.

To understand how this process of communication between WordPress and the database occurs, you need to know the role of the wp-config.php file.

How Database Connection Works in PHP

When connecting to a database in Php you require the username, password and the database host. To connect to the database you need to specify the values for the host, username and password. In Php you can connect to MySQL database using the following code:
<?php

$host = "localhost";

$username = "username";

$password = "password";

// creating the database connection

$conn = new mysqli($host, $username, $password);

// variable $conn is created for establishing the connecting to database

// Check connection

if ($conn->connect_error) {

die("Database Connection Error: " . $conn->connect_error);

}

echo "Connected successfully";

/* If the statement is used to test whether the connection works if it does not work it shows the database connection error*/

?>
In this connection query when the connection is not successful the script shows a database connection error. WordPress also utilizes this kind of query to relate with the SQL database. You can use this code to test if your database connection details are correct.

This connection is contained in the wp-config.php file. To locate wp-config.php file in your server, you need to go to Files > public_html > WordPress Installation Folder (example.com) > wp-config.php:

Fixing Database Connection Error When you open the wp-config.php file, the first thing to look for is the following code that connects the database and WordPress.
/** 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');
This code is basically Php constants that define the values of the WordPress database. You should ensure the values defined in each of these constants are correct. If any of these values are wrong, it results in a database connection error.

Testing for Database Connection in WordPress

To test these database values, we need to use the Php database connection code I shared earlier in this tutorial. The if statement tests the connection details, I have simplified it as follows:
<?php
$conn = mysql_connect('database host', ' database username', 'database password');

if (!$conn) {
die('Could not connect with Database: ' . mysql_error());
}
echo 'Your Database is Connected Successfully';
mysql_close($conn);

?>
Replace the database host, username, and password with the respective values. You should then save this file as dbtest.php and check on your browser to see the results.

Fixing WordPress Database Connection Error Check the URL below and see if the database connection is working. If it is working you should see:
yourdomain.com/dbtest.php
WordPress Database Connection Error Causes and Solutions If your database connection details are wrong, this connection will not be successful and will immediately show a Database connection error. I changed the settings to just illustrate what happens when the database connection details are wrong:

WordPress Database Connection Error Causes and Solutions

The database connection error occurs and you get the details about the error. As we have experienced in this example the leading cause of database connection errors in WordPress is faulty database connection details.

Let’s look details at the different causes of database connection errors and how to resolve this error.

WordPress Database Connection Error Causes and Solutions

Cause 1: Wrong Database Connection Information

The code above is basically PHP constants that define the database name, database host, username, and password values. You should ensure the values defined in each of these constants are correct. If any of these values are wrong, it results in a database connection error.

When installing WordPress after creating a database via MySQL wizard or Phpmyadmin, you get the database name, username, and password. You should ensure these values correspond with what you have in your wp-config.php. This is the most common cause of WordPress database connection errors.

WordPress Database Connection Error Causes and Solutions When you experience a persistent database connection error, one of the possibilities is the fact that these values are not correct. It happens when your site is hacked and the WordPress installation is compromised.

This happened to one of my clients; they just noted all their sites were showing database connection errors. On looking at the WordPress installation, the wp-config.php database connection values were all empty, something like:

WordPress Database Connection Error Causes and Solutions

If your database details are empty or are not working you should create a new database and test your WordPress installation with the new database. Also, check the Phpmyadmin if your database is present. If the database is present and the database connection error is persistent, the next possible cause is a corrupt database.

Cause 2: Corrupt WordPress Database

When the WordPress database has been corrupted the database connection error occurs. MySQL database can be corrupted by many causes among theme; server crash, failure in the database hardware, and MySQL bugs. A corrupted database can contain damaged tables and can be repaired without permanent dames to the entire database. One of the most used ways of dealing with a corrupt database is restoring from a backup. Before we consider recovering from a backup, let’s learn how to repair corrupt databases in WordPress.

Open your wp-config.php file and go to the part that defines your database name, username, password, and then host. Check below that code for the following line of code:

define('WP_ALLOW_REPAIR', true);
If you cannot see it in your wp-config.php file, you need to add it below the database configuration section.

Fixing WordPress Database Connection Error

After inserting the line of code or turning the Boolean value to TRUE. It is now time to start the database repair process.

Go to this URL on your site:
http://.yoursite.com/wp-admin/maint/repair.php
This will bring up the page that as two buttons for ‘Repair Database’ or Repair and Optimize Database’

WordPress Database Connection Error Causes and Solutions Choose either option but I prefer the second option to repair and optimize the database. When the process is complete you should go back to wp-config.php and delete the code. The presence of this line of code in your wp-config.php can compromise your database security.

If the database connection error was caused by a corrupt database, it should work now after the database repair.

Cause 3: Wrong Database Host

One of the most common questions people ask about WordPress database configuration is about the value ‘localhost’ for the database host. This is usually the case for most hosting environments; where the localhost specifies that the server is within the same environment thus the use of ‘localhost’.

When you migrate a WordPress site to a new host and a database connection error occurs, another possibility is that the database host is not localhost. You may come across hosting companies that use hosting IPs or MySQL database URLs for the host. In such a case you should substitute the database host with the respective value instead of having it as localhost.

WordPress Database Connection Error Causes and Solutions

Check with your hosting company if the database host is defined as localhost if you cannot find any other reason causing the database connection error.

Cause 4: Slow Server in Sharing Hosting Environment

WordPress Database Connection Error Causes and Solutions The final cause of database connection errors is when the server resources are used above the limit. This is common in shared hosting since the database performance is slowed leading to a database connection error.   If you are experiencing a database connection error and running the site on shared hosting, it may be good to review your hosting usage.

You can also delete some files from your site, upgrade your hosting plan or move the host to solve this problem. Caching, reducing plugins load, and using content delivery networks can be good alternatives to speeding up your site and averting the occurrence of database connection errors caused by overload.

Conclusion

These are the main causes of database connection errors in WordPress. Although these are the main causes of this error, there may be other causes. It is therefore advisable to always back up your database regularly so that you can restore it if everything else fails.

I hope this article helps you understand the causes of database connection errors and how to solve them. If you have additional comments, compliments, or questions related to this topic, please do not hesitate to get in touch by leaving a comment.