When your WordPress site can’t connect to the site’s database, it displays this error:
Error Establishing a Database Connection
Error Establishing a Database Connection Causes
There are a few scenarios that can lead to this error:
- The database credentials in wp-config.php are incorrect. This is fairly common when manually migrating or installing a WordPress site.
- Your website’s database is corrupted. While this isn’t as common, it can happen, and if your database credentials check out, this would be the next thing to investigate.
- There may be corrupted files in your WordPress installation. Another situation that isn’t common but can happen sometimes due to a hack or an issue when copying or uploading the files.
Fixing Error Establishing a Database Connection
Head to the Backups tab for your site in MyKinsta and click the Back up now button to make a backup of your site.
Check Your Database Credentials
To check your site’s database credentials, begin by heading over to your site’s Info tab in MyKinsta. Under the Database access section, you’ll find your database name, user name, and password. Next, view your site’s wp-config.php file using either SFTP or SSH and compare the database details:
// ** MySQL settings ** //
/** The name of the database for WordPress */
MySQL database username
/** MySQL database username */
MySQL database password
/** MySQL database password */
MySQL hostname (server)
/** MySQL hostname */
If any of those details do not match, update your wp-config.php with the correct information and save your changes. If you’re viewing and editing the file on your local machine after downloading it with SFTP, be sure to upload the updated file to the server, overwriting the old wp-config.php file.
If all of the details match but the error persists, continue on to the next section.
Repair a Corrupt Database
As mentioned earlier, this isn’t as common but can happen over time as database tables are added, removed, or altered by plugins and themes. If you try logging into your WordPress dashboard and see the following error, this indicates the database is corrupted and will need to be repaired:
One or more database tables are unavailable. The database may need to be repaired.
To repair your database, you have a few options, which we explain below: with WordPress, in phpMyAdmin, or using WP-CLI.
Repairing a Database With WordPress
Fortunately, WordPress has a database repair mode which you can initiate by adding the following code snippet to the bottom of your wp-config.php file.
After saving that change, go to https://yourdomain.com/wp-admin/maint/repair.php in your browser to open the interface for repairing your site’s database:
The Repair Database option is quicker than the Repair and Optimize Database option. If this is a live site you’re working to get back up and running, we recommend going with the Repair Database option.
Repairing a Database With phpMyAdmin
Log in to phpMyAdmin, click on your database, and select all the tables. From the dropdown menu near the bottom of the page, select Repair table. This does the same thing as running the REPAIR TABLE MySQL statement.
Repairing a Database With WP-CLI
To repair your database with WP-CLI, run the following command:
wp db repair
Once it’s finished, you’ll see the following success message:
Success: Database repaired.
Fix Corrupt Files
Another less common cause of a database connection error is corrupted WordPress core files. To test and fix this, you’ll need to replace your WordPress core files via SFTP or WP-CLI.
Reinstall WordPress Core with SFTP
Begin by downloading a fresh copy of WordPress from WordPress.org. Be sure to download the same version you’re running on your site.
Unzip the downloaded file on your local computer, then delete the wp-content folder and the wp-config-sample.php file.
Connect to your site with SFTP and upload the remaining WordPress files to your site, overwriting the existing files.
Once the upload is complete, empty your site’s cache and test the site to see if the error is cleared.
Reinstall WordPress Core with WP-CLI
To reinstall WordPress without overwriting the wp-content directory (where your theme, plugins, and media are stored), run the following command:
wp core download --skip-content --force
Once the reinstall is complete, empty your site’s cache and test the site to see if the error is cleared.
Restore Your Latest Working Backup
If all else fails, you can try restoring your latest working backup. Note: this may not be your most recent backup if the error has been going on for some time.
In MyKinsta, go to your site’s Backups tab and choose the backup you want to restore. If you want to test the backup in staging before restoring it to your live site, click the Restore to button, and in the dropdown menu, select the Staging environment you want to restore the backup to.
Once you’re sure you want to restore the backup to your live site, click the Restore to button next to the backup, then choose Live from the dropdown menu.
If you’ve followed the troubleshooting steps above, but this error persists, our knowledgeable Support team is here to help 24/7.