How to fix WordPress Error Establishing a Database Connection (Complete Guide) [updated]
Written by selimrana. Posted in WordPress 4 Comments
The message “Error Establishing a Database Connection” is a particularly frustrating WordPress error in which the entire site is replaced by a notice that looks like this:
If you are reading this post, there is a good chance that you are experiencing this error right now.
If not, you have a good luck. All WordPress users get this message in their lifetime at least once!
And even if you are not getting this message in your website, learning to fix this issue of “error establishing a database connection” for WordPress is a must know for you.
In this post, you will learn three things:
- What is the message “Error establishing a database connection” is all about?
- Some of the most common reason why this error message appears.
- How to fix this WordPress error establishing a database connection
What is the message “Error establishing a database connection” is all about?
If you are not familiar with the operation of WordPress, there are two essential parts of a website:
Your files – this includes WordPress application, your themes, your plugins, your multimedia uploads, etc.
Your database – this is where the actual content of your comments, posts, and pages is stored (as well as a lot of other important information such as meta information).
Every time a visitor lands on your WordPress site, the PHP files query in your database to determine exactly what information to display.
Without a functional connection to the database, the PHP file part of your WordPress site will not be able to get the information you need to render a webpage properly. That’s why it shows the “error establishing a database connection” message instead.
Some of the most common reason why this error message appears.
There are multiple potential reasons why you are seeing this message.
Most of the time , these causes are related to:
- A damaged database.
- Incorrect configuration details of the database in your wp-config.php file.
- Basic WordPress files damaged.
- Problems with your web hosting.
Because of the fact that you can not be sure what is causing the message for your specific situation, the best way to fix the problem is to take a diagnostic approach.
That is, you need to systematically follow a series of tests to isolate (and then fix) the problem.
The systematic approaches are listed is the order those need to be done:
How to fix this WordPress error establishing a database connection
Possible Fix # 1: Check if you can still access the WordPress panel
When you see this message on the front-end of your site, the first thing to do is to check whether or not you get the same error when you try to access your administrator control panel in WordPress (wp-admin dashboard).
If you still see the same error message when you try to access your wp-admin area, skip to the next step. You have already eliminated this problem as a potential cause.
If you see a different message mentioning something like “The database may need to be repaired,” continue with this section to learn how to repair your WordPress database.
Step 1: Activate the repair utility in the wp-config.php file.
To begin with, you need to edit the wp-config.php file for your site. You can do this for either of them:
Connecting to your site through FTP. ( FileZilla is a good program for that)
Using the integrated cPanel File Manager tool ( cPanel’s built-in File Manager).
Your wp-config.php file is located in the root folder/directory of your WordPress website. It is the same folder that contains wp-admin and wp-content directory/folder:
Once you can edit your wp-config.php file , add this short code snippet at the end:
define('WP_ALLOW_REPAIR', true);
Make sure to save the changes before continuing to the next step.
Want to boost your website with our product?
Step 2: Run the automatic database repair utility.
Once you have added the code snippet, you can access the WordPress database repair tool by going to:
yourdomain/wp-admin/maint/repair.php
On that page appears, click Repair database to run the tool:
Once the tool completes its repairs, your site should work again. If it does, all you need to do is to go back to your wp-config.php file and delete the same line of code that you added to enable the repair utility.
If your site is still not working, continue with the next step.
Possible Fix # 2: Check your login credentials of the database
If your database is not corrupted, the next thing to check is if your login credentials for the database are working.
Your database credentials are defined in the wp-config.php file :
/** MySQL database username */
define('DB_USER', 'yourusername');
/** MySQL database password */
define('DB_PASSWORD', 'yourdbpassword');
If this information does not match the credentials of the database that you created through the hosting control panel, WordPress will not be able to access that database.
To test if your database credentials are working, you can use the mysqli_connect () function.
Use a text editor such as Notepad to create a file called dbcheck.php.
Then, add this snippet of code to the file:
<?php
$con = mysqli_connect("localhost","mysql_user","mysql_password","mysql_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Make sure to replace “mysql_user” and “mysql_password” with the current username and password of your wp-config.php file. And, also don’t forget to change the mysql_db name.
Then, upload that file to your website through cPanel or FTP and access it by going to:
yourdomain.com/dbcheck.php
If a message appears that says “Connected correctly”, the credentials of the database is working correctly and you can proceed to the next step.
But, if you see a message like the one below, you have a problem with your database credentials:
The easiest way to solve the problem is to create a new set of database credentials to use on your WordPress site.
Step 1: Go to MySQL databases in cPanel.
Assuming that your host uses cPanel, you can create a new database user by going to the MySQL Databases option in your cPanel control panel:
Step 2: Create a new user.
Scroll down to the MySQL Users area and look for the Add a new user box.
Enter a new one:
Username.
Password.
Make sure you remember these details since you will need them in a second. Better to note down in a notepad/text file.
Step 3: Add a new user to the database.
After creating a new user, go to the option Add a user to a database and add the new user to the database of your WordPress site:
On the next screen, be sure to give the user ALL THE PRIVILEGES :
Step 4: Update the wp-config.php file with the new user of the database.
To finish, you must go back to the wp-config.php file of your site and update the DB_USER and DB_PASSWORD fields to the new user:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'yourdbname');
/** MySQL database username */
define('DB_USER', 'yourdbuser');
/** MySQL database password */
define('DB_PASSWORD', 'yourbdpass');
/** MySQL hostname */
define('DB_HOST', 'localhost');
Make sure you change, yourdbname, yourdbuser,yourdbpass and if needed server host from localhost to your hosting provided one.
Once you save your changes, your site should be working again!
Possible Fix # 3: Reload WordPress core files
If none of the above methods has corrected the error, some of the main WordPress files may be corrupted.
To make sure that this is not the problem, you can upload a new copy of WordPress to your site. If done well, this will not affect any of the content, plugins or existing topics.
Go to WordPress.org and download its latest version:
Then, extract the ZIP file and delete the wp-content folder and the wp-config-sample.php file :
Upload the remaining files to your WordPress site through FTP. When your FTP program asks you what to do with the duplicate files, make sure to choose the Overwrite duplicate files option.
This will ensure that none of your main WordPress files are corrupted, and the problem is expected to be fixed.
You may also read:
Essentials WordPress Plugins for Every WordPress Website
Possible Fix # 4: Contact to your hosting provider
If none of the previous solutions worked, it’s possible that it’s just a hosting problem.
There are a couple of potential problems:
Your database server does not work Some hosts use a separate server for the databases, which means that your database may be down, even if your regular server is still running.
Your database has reached its quota. Some shared hosts limit the use of the database. If your site has reached its quota, that could explain why your database has fallen.
To solve these problems, you should talk to your host’s support team.
Final Note
At this point, it is expected that you could fix the WordPress error establishing a database connection and have made your site work again.
Still, there are some smaller problems, which could also cause this error. These problems are often unique to your site and therefore are difficult to diagnose in a post like this.
If you still see the error message after going through these corrections and talking to your host, you may want to consider contacting a WordPress developer to investigate the problem further.
Or, you can always leave a comment and we hope we can all fix the problem together!
Lio
Really good!
Take going so, with tutorials and so on.
Miz the Whiz
Glad to hear that you liked it. We will post more tutorials, tricks, and information. Keep visiting. Have you tried our product yet?
oprolevorter
you are in point of fact a just right webmaster. The web site loading pace is incredible. It sort of feels that you are doing any distinctive trick. Moreover, The contents are masterpiece. you’ve done a fantastic process in this topic!
Digital signature
We are facing this issue to my site from last 2-4 day and we are not able to identify the issue.