The 'Error Establishing a Database Connection' message in WordPress is one of the most alarming errors — but it is usually straightforward to fix.
What Causes This Error?
- Wrong database credentials in wp-config.php
- The database user does not have access to the database
- The MySQL service on the server is temporarily down
- The database was deleted or renamed
Fix 1: Check wp-config.php Credentials
- In cPanel File Manager, navigate to public_html and find wp-config.php.
- Right-click and click Edit.
- Find these four lines and verify they are correct:
- define('DB_NAME', 'your_database_name');
- define('DB_USER', 'your_database_username');
- define('DB_PASSWORD', 'your_database_password');
- define('DB_HOST', 'localhost');
- To confirm your database name and username: in cPanel, click MySQL Databases and look at the existing databases and users.
Note: Remember that cPanel prefixes database names and usernames with your cPanel account username (e.g., myaccount_wpdb, not just wpdb).
Fix 2: Verify Database User Privileges
- In cPanel, click MySQL Databases.
- Scroll down to Current Databases and check that your database exists.
- Scroll to the Add User To Database section.
- Make sure your database user is assigned to the database with ALL PRIVILEGES.
Fix 3: Test Database Connection via phpMyAdmin
- In cPanel, click phpMyAdmin.
- In phpMyAdmin, check if your database appears in the left panel.
- If phpMyAdmin itself is having trouble connecting, the MySQL server may be temporarily down. Contact Hordanso support immediately.
Fix 4: Repair the Database
If the credentials are correct but the error persists, the database may be corrupted. Add this line to wp-config.php (after the database credentials):
define('WP_ALLOW_REPAIR', true);
Then visit: yourdomain.com/wp-admin/maint/repair.php and click Repair Database. Remove the line afterwards.
