Have you ever experienced the error establishing a database connection error? If it’s a yes, We are hoping you will be able to decode this error by the time you finish reading this post.
Seeing “error establishing a database connection” error message on the screen can be a biggest headaches for any webmaster. Well, not anymore! As I’ve come up with the solutions to remove this error once and for all. Just quickly take a look at the precap of the post.
- What does “Error Establishing a Database Connection” Mean?
- Why Does “Error Establishing a Database Connection” Happen?
- Invalid Login Credentials
- Corrupted Database
- Corrupted Files
- Database Server Down
- Heavy Traffic (Due to the maximum number of simultaneous connections)
- Wrong Site URL in the Database
- Feasible Solution of Error Establishing a Database Connection Error
- Solution 1: Examine Your Database Credentials
- Check Your Existing Credentials First
- Create a New Database User and Password
- Solution 2: Fix The Corrupt Database
- Solution 3: Fix Corrupted Files
- Solution 4: Database Server Down
- Solution 5: Manage Heavy Traffic
- Solution 1: Examine Your Database Credentials
In this post, We will show you the practical approach to how to fix the error establishing a database connection error.
Before jumping to the solution I would like you to understand what exactly this error message means.
What does “ Error Establishing a Database Connection” Mean?
It is important here to understand what exactly this error establishing a database connection error means. I won’t go deep into the technical elements but instead will take a simple approach, so you can understand it well.
A WordPress website communicates with the MySQL database via PHP for storing and retrieving information. When PHP tries to establish a connection with MySQL database and fails to do so that’s where the problem occur. And you will see an error message on the plain screen saying “Error establishing a database connection”.
In the basic scenario, a web page is built in an on-the-go process. It uses various PHP functions and MYsql queries to generate pages on the fly.
But this error message “Error establishing a database connection” that you are seeing on your screen might be due to some other reasons. The PHP code is unable to connect to the MySQL database. So, the required information that PHP needs to build the page can’t be achieved and this leads to an error in execution.
Since it has no information about the page you’re looking for, therefore it cannot show you anything except an error message, which says Error establishing a database connection.
Why Does “Error Establishing a Database Connection” Happen?
If it still feels daunting to you and you’re thinking to hire a professional to get the job done for you, I would suggest you read the entire post with your calm mind first. I assure you will able to do it yourself.
Just by making some changes and rechecking some required files we can overcome this error. And the most common reasons which cause this error are given below.
- Invalid Log In Credentials
- Corrupted Database
- Corrupted Files
- Database Server Down
- Heavy Traffic(Due to the maximum number of simultaneous connections)
- Wrong Site URL in the Database
Let’s explore the causes of Error Establishing a Database Connection in WordPress.
1. Invalid Login Credentials
I feel this is quite self-explanatory. The credentials (Username and Password) you use to login to your database are wrong. And if you’re trying wrong credentials to log in to the database you won’t get the access. Make sure you are using the correct and updated credentials. If you forget, you can reset theme from your hosting CPanel.
2. Corrupted Database
There are various unimagined reasons for a database to get corrupted. For instance, you have uploaded a bad plugin or say you’ve accidentally deleted some essential information or a table is missing so forth. There could numerous reasons that can cause the database corruption.
3. Corrupted Files
Another reason could be a PHP file is having an error and even a malware activity can cause the same problem. Sometimes it may happen that it is a PHP file which is corrupted.
4. Database Server Down
Oops! the database server can’t be reached. Yes! Sometimes it is possible that you have database on a different server and it can’t be reached may be down for maintenance or maybe it is experiencing the maximum number of simultaneous connection which is making it slower or not accessible at all.
5. Heavy Traffic (Due to the maximum number of simultaneous connections)
Who doesn’t wish to have heavy traffic to one’s site? But this traffic can become a problem if it comes in abundance at a time. And then it gets tough to deal with it. Yes! Heavy traffic at a time can make your site unresponsive. Because it gets difficult for the server to respond to every request it is receiving at a time. And due to the maximum number of simultaneous connections the error establishing a database connection error arises.
6. Wrong Site URL in the Database
The reason behind the error establishing a database connection error can a wrong site URL in database. You will keep encountering the same error unless you won’t correct the site’s URL in the database. Simply go to the phpMyAdmin and run the following SQL query.
UPDATE wp_options SET option_calue=’YOUR_SITE_URL’ WHERE option_name=’siteurl’
Before you copy paste the above query and run in your phpMyAdmin, make sure you change the dummy “YOUR_SITE_URL” with your actual site name.
Note: Before you troubleshoot the error it is highly recommended to take the backup.
Fix up the “Error Establishing a Database Connection” in WordPress
Method 1: Examine Your Database Credentials
The very first and the basic thing is to check if your website’s database user credentials are correct or not. When people migrate to a new hosting provider then they tend to encounter this error establishing a database connection error.
And this is by far the most common error which arises after migration. The wp-config.php file contains the database connection details.
This file consists of four sections that give you the most important information about your WordPress site i.e.
- The name of the database (‘DB_NAME’)
- The login username (‘DB_USER’)
- The password (‘DB_PASSWORD’)
- The database host (‘DB_HOST’)
Your wp-config.php file must contain the correct and updated information about the login details to ensure the connection establishment.
define (‘DB_NAME’, ‘your_database_name’); define (‘DB_USER’, ‘your_database_username’); define (‘DB_PASSWORD’, ‘your_database_password’); define (‘DB_HOST’, ‘your_databse_host’);
These details are specific to the host and website. And if any of these four credentials are incorrect, then you will see the ‘error establishing a database connection’ error. Another possible scenario to check the database outside WordPress is through PHPMyAdmin. PHPMyAdmin is a powerful tool to access and manage a MySQL database.
Caution: Be double alert while dealing with PHPMyAdmin if you are a newbie.
Once you’ve logged in the PHPMyAdmin account select your database out of the list of databases on your server. And make sure it matches the name with the one in your wp-config.php file.
To Remember: If there isn’t any database them immediately contact your host provider. This entire problem can be related with your server only.
- Once you clicked on the database name, you’ll see a number of tables in your database. Now, to confirm that you’ve chosen the right database look for the table name wp-options and click on the Browse button alongside.
- This action will show you a page having the information regarding URL, name, and other general settings of your WordPress site. Now, it is your duty to make sure that the names should be same as you find in your wp-config.php file. Whereas, if you find any mismatch in the details then fix them in the wp-config file before moving forward.
Done with the database name. Now is the time to examine username and password.
- Out of many simple and complex approaches to check the username and password we’ll go with this one. Create a sample.php file to check whether you’re able to connect to the database with the login credentials from a wp-config file or with the existing username and password.
Now, there are two approaches;
a. Check Your Existing Credentials First
Simply create a file with desired name having .php extension and paste the following code.
Now, go to the file into your browser and test the connection like I set the file name as test.php and will run yoursite.com/test.php you will either witness a successful connection or another error with more details.
And more errors clearly means your previous credentials aren’t working anymore. So, you need to create new ones.
b. Create a New Database User and Password
We can achieve this by using another tool available in cPanel called as MySQL Databases. Just click on it and under MySQL users you will find an Add New Users option, just select this option. Add a username and a unique and strong password. Make note them in the wp-config file. Click on Create User to continue.
After adding the user to the database, just update your wp-config file with new credentials.
Method 2: Fix The Corrupt Database
Before everything else just be sure that you are getting the same error ‘Error establishing a database connection’ even when you trying access the wp-admin page.
Whereas, if you get to see a different error message let’s say “One or more database tables and unavailable. The database need to be repaired.” Then it’s crystal clear that you need to repair your database. And that can be achieved by using a built-in feature of WordPress.
By default this feature is disabled, thus, you have to manually enable it first.
You need to go to the wp-config.php file.
Now, add the following line of code in the bottom
define (‘WP_ALLOW_REPAIR’, true);
This line of code gives you the ability to repair and optimize your database through navigating to www.websitename.com/wp-admin/maint/repair.php (don’t forget to replace websitename.com with your actual URL).
Now you will see a page showing two options first Repair option and the second one is Repair and Optimize option that will help you repair and optimize your database.
Please Keep in Mind
This database repair page that you have opened is Not Secure and anyone can access it without having to log in. So, as soon as you complete the database repair just delete this line of code from the wp-config page after you successfully repair your site.
Method 3: Fix Corrupted Files
- After examining the database if you haven’t found any error then chances are there are one or more corrupted file present causing the error establishing a database connection error. Sometimes even re-uploading the basic WordPress files can fix the error.
- Sometimes the issues are related to the incorrect permissions of important files and folders. It is quite simple to reset WordPress files, just download the latest version of the WordPress from the WordPress.org. Now unzip the pack in your computer system and delete two folders i.e. config.php and wp-content. Once you’ve deleted these folders go to your FTP or File Manager and upload it, now upload the config.php and wp-content folder from your old website.
- Now, clean your browser’s cache and reload your site. Now, check your WordPress website if it’s working fine. And if the error persists do contact your web host provider once because sometimes the problem could be on their side.
Method 4: Database Server Down
If you have tried all the things mentioned above then it indicates that your hosting provider have separate server for databases and it is down. If you don’t have the access to the hosting tools, it becomes difficult to manage the server malfunctions and overloads.
If you are also on the shared server that has multiple other users, then it gets tough to analyze whether the error has occurred due to a malfunction or an overload. Sometimes some other site on the same server is overloading the database and there is no malfunctioning to the server. And in such a situation you can’t do anything except to wait until it all gets fixed.
Generally, this overload thing doesn’t last long and it is handled by the server only by automatically rebooting itself and be up and running again. But, if you’re facing this problem quite often then you must inform your host provider and tell them about the problem you’re facing and don’t forget to mention the measures you have already taken to resolve this issue.
Method 5: Manage Heavy Traffic
There are chances that you may experience a sudden flood of traffic that the server can’t handle. It may happen that suddenly one of your post, product, or article is trending and everyone is curious to know about it and everyone is hitting to your site. Such a sudden flow of traffic can down your server because at times handling such maximum number of simultaneous connections can be a problem for your server.
And this overload can result in error establishing a database connection error which will be resolved by the server itself.
What’s Your Pick to fix Error Establishing a Database Connection ?
Solving this error can be a little tedious and time-consuming depending upon the cause. And as you have seen that the exact cause to the “error establishing a database connection” error can be one of the many given above. So, backing up your website at regular intervals is advisable.
We hope the article will help you in tracking down the cause to this problem a little easy for you. Please feel free to share your thoughts and ask questions in the comments below. We will answer all of your questions and will provide you with the best possible solution to your problems as soon as possible.
Content retrieved from: https://blog.templatetoaster.com/how-to-fix-error-establishing-a-database-connection/#fix-corrupted-files.