We’ll unravel the mystery of the dreaded ‘error establishing a database connection’ WordPress issue once and for all, and get your site and database playing well together.
Read on for the solution!
Are you seeing this error establishing a database connection in WordPress? This is one of the most common errors that comes up in WordPress, and it can be frustrating when it just won’t connect to your database no matter what you try. Fear not!
Fix #1: Check wp-config.php and confirm you have the right credentials
open up the file wp-config.php in the root directory of your website. This file contains the credentials that WordPress uses to connect to the database. Specifically, we’ll look at these 4 lines:
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
Double-check these 4 lines and make sure that they match your database credentials exactly. Check for spaces and capitalization. Often, switching the DB_HOST from
'127.0.0.1', or vice versa from
'localhost' is enough to solve the issue, so try flipping the DB_HOST after you’ve checked the first three credentials.
Fix #2: Repair the WordPress Database
No success with the first fix? No problem! Sometimes the WordPress database can become corrupt, so the next thing we’ll try is a database repair. WordPress has an automated tool for DB repairing which makes this fix a cinch to apply.
First, you’ll need to allow database repairs through your wp_config.php file. Open wp_config.php and add a new line at the very end of the file as follows:
Save the file and then point your browser to [your_site_url]/wp-admin/maint/repair.php
This launches the database repair tool, where you’ll see the option to ‘Repair the Database’. This may take some time, so be patient once you click to repair. Hopefully after the repair, your WordPress error establishing a database connection will be resolved. Test to see, and move onto the next fix if you’re still having issues.