What this error actually means? The establishing a database connection error means that for some reason the PHP code was unable to connect to your MySQL database to retrieve the information.
How to Fix Error Establishing a DB Connection Error
Note: Before you make any database changes, make sure you have sufficient backups.
Method 1: Repair the Database.
Your database can easily be repaired using a built-in WordPress feature.
- Open wp-config.php file and add following code:
define( 'WP_ALLOW_REPAIR', true );
- Navigate to www.yoursite.com/wp-admin/maint/repair.php
- Click on Repair Database or/and Repair and Optimize. Repairing and optimizing the database can take a long time and the database will be locked while optimizing.
- Remove the following line from wp-config.php to prevent this page from being used by unauthorized users.
If this repair did not fix the problem, you can find another solution to work.
Method 2: Checking the WP-Config file
If you changed your root password, or the database user password, then you will need to change this file as well.
- Open wp-config.file and finf following code:
define('DB_NAME', 'database-name'); define('DB_USER', 'database-username'); define('DB_PASSWORD', 'database-password'); define('DB_HOST', 'localhost');
Remember your DB_Host value might not always be localhost. Depending on the host, it will be different.
- Login to PHPMyAdmin
- Click on your database.
- Find the table named wp_options and click on the Browse button next to it.
- Compare and edit, if necessary data in wp-config.php file.
Additional suggestion is replace localhost with the IP, like:
define('DB_HOST', '127.0.0.1:8080');
Method 3: Fix Corrupted Files
- Download a fresh copy of the WordPress CMS from WordPress.org.
- Unzip it in your drive and delete the whole wp-contents folder and the wp-config file so there’s no chance of overwriting your current wp-config file. All the hard work you’ve put into setting up themes and plugins up till now won’t be lost.
- After deleting the infected or corrupt files, paste the rest of the files into your WordPress root folder using the File Manager or FTP client.
Method4: Check your Web Host (MySQL Server)
- Often error establishing database connection could be if your site gets swarmed with a lot of traffic. In this case you should call hosting provider and ask them if your MySQL server is responsive.
- You can create test.php file with following code :
<?php $link = mysql_connect('localhost', 'root', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?>
Make sure to replace the username and password. If the connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong.
- If you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server. It does not necessarily means that your MySQL server is down. It could mean that your user does not have sufficient permission.
The Error Establishing a Database Connection error is one of the most frustrating errors you can encounter with your WordPress website. There are several different possible root causes for this error making it difficult to identify and resolve. The small typo in wp-config.php file can make it happen.
Excellent article! It just made my work easier. It saved lot of time. Great work.
Thanks.