“Error establishing a database connection” is the most common error you face in WordPress. This error means your site is unable to connect to the database. Sometimes this error occurs during the updating, installing or migration the WordPress. Sometimes, It will happen due to the other reasons like SQL server is down, hosting issue etc.
You can identify all possible reasons and appropriate solutions by following methods:
How to Fix the Error Establishing a Database Connection in WordPress
These are the main reasons of an error establishing a database connection:
- Wrong MYSQL database login details in wp-config file
- Database server is unresponsive or down
- WordPress database is corrupted
- MYSQL Database User Has Limited Privileges
1. Wrong Mysql Login details:
Wrong credentials are the most common cause of the error establishing a database connection. You can fix it easily.
You have to check wp-config.php file that contains all details of the database. Open your wp-config.php file via cpanel filemanager or your favorite FTP client. Check the following details:
define('DB_NAME', 'database-name'); define('DB_USER', 'database-username'); define('DB_PASSWORD', 'database-password'); define('DB_HOST', 'localhost');
If you change your hosting and migrate the site to another server, you should change the above details with new server details. Some cloud hosting uses a separate DB host instead of localhost, so you should change it very carefully with the new credentials.
Many times this error is fixed by changing the hostname to the IP address as follows:
define('DB_HOST', '176.0.0.5:22');
2. MYSQL Server is down
If your site is a very heavy traffic site, you can get this error during peak hours for some users. It happens due to high database queries.
In this situation, it will be fixed after restarting the MySQL service. So, Contact your hosting provider to know whether your Mysql server is down or not. If it is down your host will fix it. If you have a cloud or VPS server, you can fix it by restarting the MySQL service.
You can test the server status by creating testconnection.php in your installation root directory. After creating this file add the following code:
<?php $link = mysql_connect('localhost', 'mysql_user', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Congratulation Connected successfully'; mysql_close($link); ?>
In line no.2, change mysql_user and password value with username and password.
Now open this file in the browser by typing yoursite.com/testconnection.php
If you can see the page ”Congratulation Connected successfully”, All service is up and running. If not connected and get the access denied error, contact your hosting provider.
3. WordPress database is corrupted
Many times, It may happen due to a corrupted database and the need to repair the database. You can repair a corrupt WordPress database by following the below steps:
Add the following line to the wp-config.php file
define('WP_ALLOW_REPAIR', true);
Now open the following URL in browser:
http://www.yoursite.com/wp-admin/maint/repair.php
Click on the button “Repair” or “Repair and Optimize”. WordPress will repair your database in a few minutes. Do not forget to delete the above function from the wp-config.php file after repairing the database.
4. MYSQL Database User Has Limited Privileges
If none of the above methods works, then apply this method.
- Delete the current MySQL user that is connected to the database.
- After deleting the current user, Create the new user and give the full privileges to the newly created User.
- Now open the wp-config.php file and replace the old database username with the new username and save it.