How to Fix the “Error Establishing Database Connection” in WordPress: A Comprehensive Guide
If you’ve encountered the “Error Establishing Database Connection” message while trying to access your WordPress site, it typically points to one of the following issues:
- The database has crashed
- Incorrect database login details in your WordPress configuration file
- Corrupted WordPress database tables
Let’s explore these potential problems and how to address them step by step.
Step 1: Repair the WordPress Database
Database corruption can happen due to various reasons, such as a failed update, a plugin malfunction, or a crash. This can lead to a database connection error. The first step you should take is to attempt a database repair.
If you’re using cPanel, you can easily repair your database via phpMyAdmin.
WordPress also has a built-in database repair feature, though it’s disabled by default due to security concerns. Here’s how to enable, use, and then disable this feature:
- Open your wp-config.php file in any text editor.
- Add the following code to a blank line in the file:
define('WP_ALLOW_REPAIR', true);
This defines a variable that WordPress looks for when determining if it should enable the repair feature.
- Save the changes and close the file.
- In your browser, navigate to the following URL, replacing it with your domain name:
http://www.yourdomain.com/wp-admin/maint/repair.php
This will take you to a database repair page. Click the “Repair Database” button to start the process. Once completed, revisit the wp-config.php file to remove the line you added earlier.
After repairs, check your site to see if the issue is resolved.
Step 2: Verify Database Login Details
If you’ve recently migrated your WordPress site to a different server or hosting provider, you may need to update your database connection settings stored in the wp-config.php file.
- Open the wp-config.php file in your preferred text editor.
- Locate the database connection information:
/** The name of the database for WordPress */
define('DB_NAME', 'database_name');
/** MySQL database username */
define('DB_USER', 'database_username');
/** MySQL database password */
define('DB_PASSWORD', 'database_password');
Ensure that the details match your current database credentials. If any information is incorrect, update it accordingly, save the file, and exit.
If the problem persists, restoring the database from a backup may be necessary.