WordPress hosting is now faster and easier than ever! Experience 99% uptime, lightning fast speeds, and 24/7 support for your WordPress site brought to you by hosting.co.uk. Begin your WordPress hosting journey today with one of our four payment options. We offer fast and easy options for each of our hosting packages, as well as affordable pricing. The best part is, if you find yourself with a database connection error, we provide the support and assistance you will need to fix it!
Errors are a common part of web hosting. There are many ways in which an error can come about; just about as many grains of sand there are on the beach. One of the most unfavorable errors you would face as a website owner is that of a database connection error.
An error that results in your website being unable to connect to your database can cause your website to load incorrectly, or incompletely.
There are a few reasons why your website may be unable to connect to your database; some of which are easy to fix, and some that are not. Either way, it is best to be able to identify the cause of the conflict between your database and your website, and to be able to resolve said conflict as soon as possible.
After all, you don’t want to lose clients over something like this.
Table of Contents
Why Can’t I Connect to the Database?
As you should recall, a database is a system upon which information is stored and organised so it can be accessed and presented in a meaningful way. Since WordPress uses databases to store the content that will be displayed on your website, you can see why it might be a problem if your website is unable to connect to the database.
There are specifically three reasons why your website may not be able to connect to your database:
- Your database credentials are incorrect
- Your database is corrupted
- The server that hosts the database is down
As we continue, we will explain how to identify which of these reasons is causing the disruption, and how to fix these database connection errors.
Incorrect Database Credentials
If your database credentials are incorrect, it may be because you recently made some sort of change to website at the server level, such as moving your website to a new host. This is the case because in the process of connecting to this new host, you might have used different login credentials to connect to the database on that host. So, naturally, the login credentials stored in your WordPress configuration file would be incorrect.
The solution in your case would be to identify your WordPress Configuration file and change the login credentials of your database to match the ones you used when setting up your connection to the new host. To do this, locate and open your wp-config.php file. Identify the lines that define your database name, your username and password, and the host that you can access the database from and verify that they are correct.
If they are not correct, you will need to edit them so that they are. If you’re not sure about editing your WordPress code, you can learn the basics of doing so here.
Database Corrupted
If your credentials are correct, and upon attempting to log in to your database you receive an error that says “One or more database tables are unavailable. The database may need to be repaired,” then guess what?
Your database may need to be repaired. Before you start to panic, you should know that WordPress provides a built-in feature that allows you to repair your database.
To access it you need to locate and open up you wp-config.php file, and the following line of code to the bottom of the file:
Define( ‘WP_ALLOW_REPAIR’, true ); (yes, the semi-colon too)
Once you have done this, you will need to navigate to your website’s extension to the repair page by entering into your browser:
http://www.yoursite.com/wp-admin/maint/repair.php
On the resulting page, you can choose to ‘Repair Database’ or ‘Repair and Optimise Database.’ Either option is fine, as long as you understand that the latter will take a longer time to complete. When you are done, be sure to remove the line of code you entered earlier from the wp-config.php file.
Database Server Down
If all else fails, then it means that your server is down. There are a few reasons why your server may be down:
- Your website may be experiencing a spike in traffic.
- There may be an error on the side of your web hosting service.
In both of these cases, there is not much that you can do except to make contact with your web hosting service provider and have them try to fix the issue, or at least tell you what the problem is and when it can be resolved.
Test Database Connection
If you would like a little more information on what is causing the error, there is a way you can find out on your own:
1. In your WordPress directory, create a new file called ‘testconnection.php’
2. Type the following code into the file:
<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
}
echo ‘Connected successfully’;
mysqli_close($link);
?>
3. Granted your database credentials are correct, enter those credentials into their respective places.
4. Navigate to ‘yourwebsite.com/testconnection.php’ making sure to replace ‘yourwebsite’ with the domain name of your website.
5. If the connection was unsuccessful, an error will be displayed stating same, and giving a little more information.
Problem Solved!
Fixing database connection errors in WordPress can be that simple. All it takes is a minimal understanding of how WordPress works and a little bit of coding. The most common solution is usually the best solution, so try verifying your credentials first. If that doesn’t work, then you may want to explore other options. If nothing else, you will learn something new. Happy hosting!