How to Fix 'Error Establishing a Database Connection' in WordPress

75 views
Short answer: Cross-reference DB_NAME, DB_USER, and DB_PASSWORD inside wp-config.php against cPanel MySQL Databases, ensuring the database user holds 'All Privileges'.

Restore communication between WordPress and MySQL by auditing wp-config.php credentials, checking database users, and fixing corrupted tables.

Before you start: Back up your site files and database and download the backup to your own computer. How to take a full backup

Quick reference

Config File public_html/wp-config.php
Required Variables DB_NAME, DB_USER, DB_PASSWORD, DB_HOST (localhost)
Repair Tool define('WP_ALLOW_REPAIR', true); in wp-config.php

Quick diagnosis

Scenario & Suspected Cause Immediate Action & Remedy
Mismatched credentials in wp-config.php Cross-check DB_NAME, DB_USER, and DB_PASSWORD between wp-config.php and cPanel MySQL interface.
Missing database user permissions Navigate to MySQL Databases, assign user to database, and grant 'All Privileges'.
Corrupted MySQL database tables Click 'Repair Database' in cPanel or append define('WP_ALLOW_REPAIR', true); to wp-config.php.
Max MySQL connections saturated Review persistent queries and optimize database indexing to speed up connection release.

Steps

  1. Check wp-config.php Credentials: Open public_html/wp-config.php in File Manager and inspect:
    • DB_NAME: Ensure the full database name matches cPanel (including prefix, e.g. user_wpdb).
    • DB_USER: Ensure the database user name matches cPanel (e.g. user_wpusr).
    • DB_HOST: Must be set to localhost.
  2. Verify User in cPanel: Go to cPanel > MySQL Databases. Scroll to Current Databases and verify that your DB user is actually assigned to your DB with ALL PRIVILEGES.
  3. Reset Password: Under Current Users, change the password for the DB user, and paste that exact new password into define('DB_PASSWORD', '...'); in wp-config.php.

Troubleshooting and tips

  • Corrupted Tables: If credentials are 100% correct, a table may be corrupted. Add define('WP_ALLOW_REPAIR', true); to wp-config.php, then visit https://yourdomain.com/wp-admin/maint/repair.php in your browser to run the automatic repair tool. Remove the line after repair.
Need more help? If you run into an error while following these steps, open a ticket with technical support and our team will look into it. Open a ticket
Was this article helpful?
No, I need more help
Didn't find your answer?
Under 3 minutes
Submit Support Ticket