Check out our YouTube Channel.
The error message “Failed to establish a database connection” indicates a fatal error that makes your WordPress website inaccessible to users.
The reason for this error is that WordPress cannot connect to the database. In this case, several different variables affect the connection between WordPress and the database, which makes troubleshooting a bit difficult for beginners.
We have prepared a list of possible reasons for this error and a step by step guide to bringing back your WordPress website as fast as possible.
Contents
What Causes the Error Message “Error Establishing a Database Connection”?
The error message “Error Establishing a Database Connection” can be caused by either incorrect database credentials in your WordPress settings, a corrupt database, or an irresponsive database server.
What is a Database?
To understand the problem, we briefly explain what we are dealing with:
A database is a software that makes it easier to store, organize, and retrieve data from one software to another.
WordPress acts as a content management system. It uses a database to store all of your content and other website data. A connection to the database is established each time the website is visited.
What Data does WordPress need to Establish a Connection to the Database?
WordPress needs the following information:
- Name of the database
- Database username
- Database password
- Database server
This information is saved in your WordPress configuration file with the name “wp-config.php.”
Once any of these items are incorrect, WordPress can no longer connect to your database server. Then you get the error message “Error Establishing a Database Connection.”
It is one of the most common WordPress errors. Aside from incorrect credentials, this error can also occur if the database server has failed or the database files are damaged.
Various reasons can cause this error; we have listed the different scenarios one by one. If you have already tried one of these approaches yourself, select the next steps from the table of contents above.
We’ll start with the most likely reason and then go into less common reasons.
Check Your WordPress Database Credentials
The most common cause of database connection failure in WordPress is incorrect database credentials.
One of the reasons for incorrect WordPress database connection can be a recent move of your WordPress website to a new host or server.
The login information for the WordPress database is saved in the wp-config.php file. This WordPress configuration file contains all the basic WordPress settings, including the database information.
Login to your website by using an FTP client like FileZilla and look for the lines below in the file wp-config.php:
You now need to ensure that the database name, username, and password are correct.
You can check this information in your WordPress hosting account dashboard. Log into your hosting account and click on “MySQL Databases” in the Database section.
You will now be taken to the database administration page in your hosting dashboard. Here you can see your database name and user name.
Below are the database users and links to change the user password.
After you have matched your database name, username, and password, you can change this information in your wp-config.php file if necessary.
Try revisiting your website to see if the database connection error is resolved.
If you can still see the error, it means that something else is causing the error. Keep reading the article for more troubleshooting steps.
Check Your Database Hostname Information
If you followed method one and verified that you are using the correct WordPress database login information, you should ensure that you are using the appropriate database host information.
The majority of WordPress hosting companies use “localhost” as their database host. However, some WordPress hosting companies use separate servers to host the databases. In this case, your database host information is not localhost.
In this case, you will need to contact your WordPress hosting company to obtain your database host information.
Repair the WordPress Database Tables
If you’ve fixed the first error, but you get another error message, “One or more database tables are unavailable, the database may need to be repaired.”
To repair the database, add the following line to the “wp-config.php” file:
define('WP_ALLOW_REPAIR', true);
Add this text directly before the line “That’s all, stop editing! Happy blogging”.
Visit http://www.example.com/wp-admin/maint/repair.php to view the settings on this page:
Screenshot 2 Meldung
Click on the “repair database” button and wait for the process to finish.
Check if The Database Server is Offline
If you checked all items and WordPress still cannot connect to the database, your database server (MySQL server) may be down.
This happens when there is heavy traffic on a server. The hosting server cannot handle this heavy load. The problem occurs, especially when you are using shared hosting.
As a result, your site will become very slow, and some users may even get the error.
Get in touch with your hosting provider and see if your MySQL server is responding.
Alternatively, if you have other websites running on the same server, you can check those websites to make sure your SQL Server is down.
To check if your database access credentials are correct and your database server is not offline, create a new empty text file with a text editor, name it tod testconnection.php and copy and paste the code below to that empty text file:
<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Adjust the values ”username” and “password.” to your settings
Now upload this file with FTP or a file manager plugin to your website and access it through a web browser.
If the script can connect successfully, it means your user has sufficient permissions, and your database server is online. Now use the same credentials in the wp-config.php, and WordPress can connect to the database.
In the next step, we will list other solutions that also work but occur less often than the previous ones.
Update the WordPress Site URL
Access phpMyAdmin from your hosting account dashboard and select your WordPress database.
Then, click on the SQL menu at the top and enter the MySQL query below:
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
Note: Be sure to enter your own “Site URL” and change “wp_options” to your site’s table name if your site uses a different WordPress table prefix.
Reboot the Web Server
If you are using a dedicated server, a local server, or a virtual private server (VPS), you can try restarting your servers.
Restarting the web and database servers can resolve temporary glitches that may be causing the error.