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:
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:
/** 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.
yourdomain.com/dbtest.php
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.
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:
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.
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’
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.
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
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.
No Comments Yet