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 theBackupstab for your site inMyKinstaand click theBack up nowbutton 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’sInfotab in MyKinsta. Under theDatabase accesssection, you’ll find your database name, user name, and password. Next, view your site’swp-config.phpfile using eitherSFTPorSSHand compare the database details:
Database Name
// ** MySQL settings ** ///** The name of the database for WordPress */define('DB_NAME','xxxxxx');
MySQL database username
/** MySQL database username */define('DB_USER','xxxxxx');
MySQL database password
/** MySQL database password */define('DB_PASSWORD','xxxxxx');
MySQL hostname (server)
/** MySQL hostname */define('DB_HOST','localhost');
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 oldwp-config.phpfile.
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 yourwp-config.phpfile.
define('WP_ALLOW_REPAIR',true);
After saving that change, go to https://yourdomain.com/wp-admin/maint/repair.phpin your browser to open the interface for repairing your site’s database:
TheRepair Databaseoption is quicker than theRepair and Optimize Databaseoption. If this is a live site you’re working to get back up and running, we recommend going with theRepair Databaseoption.
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, selectRepair table. This does the same thing as running theREPAIR TABLEMySQL 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 viaSFTPor 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 thewp-contentfolder and thewp-config-sample.phpfile.
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 cacheand test the site to see if the error is cleared.
Reinstall WordPress Core with WP-CLI
To reinstall WordPress without overwriting thewp-contentdirectory (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 cacheand test the site to see if the error is cleared.
Restore Your Latest Working Backup
If all else fails, you can tryrestoring 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’sBackupstab and choose the backup you want to restore. If you want totest the backup in stagingbefore restoring it to your live site, click theRestore tobutton, and in the dropdown menu, select theStagingenvironment you want to restore the backup to.
Once you’re sure you want to restore the backup to your live site, click theRestore tobutton next to the backup, then chooseLivefrom the dropdown menu.