How to fix error establishing a database connection in WordPress
Probably facing one of the most troubling error in WordPress is “Error establishing a database connection”. Because it vanishes everything and your site is nowhere available to user and your repute is going to be damaged.
You see that it just keeps on showing some utterly creepy piece of error saying:
“Error establishing a database connection”
It means you’re losing your traffic and your visitors are disappointing. You’re upset now. But I say:
No worries. Keep calm!
You’re just some tricks away to the solution.
“StarsGeek” is a better cure or remedy to an error on your website.
So I have gathered all the possible mishaps and then taken down the solution to all those reasons that may cause such kind of error.
The point is why this error occurs and how to get your website back to its normal condition.
What does this “Error establishing a database connection” mean?
Just to make things easy and understandable for you, I would mention two things here, MySQL and PHP.
MySQL Server stores information in a database and PHP retrieves data from this database which then is shown on the website.
Whenever a webpage is loaded, the content that makes a complete webpage comes from database.
So retrieval of data is possible only if there is connection parameters are correct all the way around configuration.
This is why we see everything vanishes and a blank page is there to welcome us with just an error statement, not with web content.
This means the error generates when PHP fails to establish its connection with database for information.
What are the possibilities that caused this error?
There is only one error but possibilities of reason behind could be more than one which cause failure to the successful establishment of connection with database. The reasons behind could be one of stated below:
The login credentials to your database are incorrect.
The login credentials associated with database are different from admin-login. So if any change is found there or any credential has been modified by you, so website won’t be able to connect to database and access it.
Your database has got corrupted.
The error might occur due to the database corruption. The database corruption issue might arise during installation of any bad plugin, WordPress up-gradation or because of any internal problem on your server.
Sometimes, I have seen this issue while installing/uploading plugin on slow internet connection due to which some changes remain incomplete that cause this severe problem.
That’s why I advise my users to avoid updating their plugins whenever they are struck with slow internet services. Wait for some minutes. Patience is better key to safety.
Any file could be corrupted.
Yeah, some files might be corrupted due to internal syncing so this might be the reason behind it.
Database server is down.
The problem could be with your server. This could happen either due to some maintenance on the end of Web Hosting Company.
There might be some traffic boost which your server can't handle. So the server gets down and this error may occur.
And there’s a separate solution to each possibility.
But an alert:
Before fixing your problem, you must make sure the backup of your WordPress site. You can have backup to your website manually by downloading all the stuff from File Manager of cPanel or using FileZilla.
Don’t forget to export a database from your PHPMyAdmin section. Copy that file and above downloaded stuff in a folder to prevent from any issue.
How to fix “Error establishing a database connection”?
All the possible solutions have been combined here. I have collected them from my own personal experience and by some others.
Now coming to the solution step by step:
1. Database Corruption-You need to know where the error is actually occurring!
First of all, check to the the thing where the error is actually impacting. Check if it’s only front-end and not the backend (wp-admin-Admin Side), or if it’s on only backend and not front-end. If so, choose this solution as first practice or else proceed to the next step.
Now database corruption is the issue.
Just take a soothing breathe as we’re moving to the first solution.
WordPress has inbuilt ability to resolve this and repair the database. But for this you need to include a line in a file.
You just have to include a single line into your wp-config.php file.
Now question is where this file is!
You can easily access this file by logging into your cPanel and going into File Manager portion. You’re in File Manager now.
Now you need to switch to the root directory of the folder where your WordPress is installed and open wp-config.php file from there.
When you’re done with it, just add following line anywhere in the bottom of the file.
Once you’re done with it, you just need to navigate to the following link:
Note: (Don’t forget to replace yourwebsiteurl with your actual URL)
As seen in picture, this will allow you to repair your database and optimize it. But you just need to repair this. Optimizing will take long and also not the need of solution. So I would suggest just going for optimization.
You just be very careful, this repair page you’ll visit is not secure and can be accessible by anyone without having logged in. So make sure removal of this line, once you complete your repair process.
Now if you’re still facing same issue, need not to worry. Move 1 step forward. You’ll finally end-up resolving issue.
2. Check Login Credentials in wp-config
Again open that wp-config file from here:
If you changed username, user-password or database name, you just have to update it in the file too.
You need to check four values:
You’ll see all four like this:
// ** MySQL settings - You can get this info from your web host ** // /** 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' );
1. For database, you’ll have to move to PHPMyAdmin where you can see which database you’re using across your domain.
If there’s one domain, then only there should be only one database but you could have multiple databases.
You can confirm your correct database-name against your domain by going into wp-options under database. There you’ll see siteurl, if that’s your domain then this is correct database and it should be written in wp-config.
And if there is no database, then contact your host, surely they will resolve your issue.
2. You can also check your username and password there in cPanel anywhere along with PHPMyAdmin.
The last thing DB_HOST, Database-Host. You must be clear here that your database host value could be different and it totally depends upon your host.
For famous hosting companies like Hostgator and Bluehost, it’s “localhost”. For other hosts these are:
- 1and1 Hosting — db12345678
- DreamHost — mysql.example.com
- GoDaddy — h41mysql52.secureserver.net
- ICDSoft — localhost:/tmp/mysql5.sock
- MediaTemple (GS) — internal-db.s44441.gridserver.com
- Pair Networks — dbnnnx.pair.com
- Yahoo — mysql
So if anything is different or mismatch from actual, it won’t work i.e. it won’t connect to database which is the root issue.
Check your server with test connection:
Test your server by other sites you have on same server. But if you don’t have other site, you need not worry about that and check by following way.
You can test your MySQL server whether it is okay or not. For this, you just have to create a file testmyconnection.php (There could be any name of file but extension only .php). Now create a new file and put following code in the file:
Make sure you have inserted correct username and password.
Now put this file in the root folder of your website and open the link to this file.
The link would be like this:
If connection was successful, then it’s okay with your end and there is something else wrong.
But if the connection was not successful, it doesn’t necessarily mean your server was down. There could be problem with your username and there could be problem with your user-permissions. Check them in your MySQL Panel and make sure it has all privileges.
But if there’s everything okay with your end and you still are unable to fix the error, then you should immediately contact with your web-host and ask them to resolve this for you.