WordPress Error: Establishing a Database Connection
What could cause this error?
When you encounter the "Error Establishing a Database Connection" message on your WordPress site, one of the most common causes is incorrect database credentials in your WordPress configuration file. These credentials include the database name, database username, database password, and database host. Ensuring these credentials are correct is crucial for establishing a connection between WordPress and your database.
Caution
This article is technical in nature. We recommend that these steps be followed only if you are comfortable navigating and managing technical settings. If you are not confident in performing these actions, consider seeking assistance from our support team.Resolution Steps
- Log in to cPanel.
- Go to File Manager.
- Navigate to your website's directory. If the website is your main domain, the directory will be public_html. For other domains, the directory will have the same name as the domain.
- Find and edit the wp-config.php file.
- Verify that the following information is correct:
- Database Name: DB_NAME
- Database Username: DB_USER
- Database Password: DB_PASSWORD
- Database Host: DB_HOST
- To verify the database name, username, and host:
- In cPanel, go to the MySQL Databases section.
- Check if the database name listed in the wp-config.php file matches one of the databases in the Current Databases section.
- In the Current Users section, check if the database username matches the username listed in the wp-config.php file and that this user is assigned to the correct database.
- Database Host is typically localhost.
- To verify the database password:
- In the MySQL Databases section, locate the database user.
- Click on the Change Password option next to the user to reset the password.
- Update the wp-config.php file with the new password.
- Save the changes to the wp-config.php file.
- Edit the wp-config.php file again.
- Add the following line of code just before the line that says "That's all, stop editing! Happy blogging.": define('WP_ALLOW_REPAIR', true);
- Save the changes.
- Navigate to http://yourwebsite.com/wp-admin/maint/repair.php.
- Click on "Repair Database" or "Repair and Optimize Database."
- Once the repair is complete, remove the line define('WP_ALLOW_REPAIR', true); from the wp-config.php file.
- Log in to cPanel.
- Go to phpMyAdmin.
- Select your WordPress database from the left-hand menu.
- Ensure all tables are present and not marked as "in use" or "crashed":
- Look at the "Status" column for each table in phpMyAdmin. Tables marked as "in use" or "crashed" will typically display error messages.
- If any tables are marked as "in use" or "crashed," select the table(s) and use the "Repair table" option available in the dropdown menu at the bottom of the table list.
- Check for missing tables:
- Compare the tables in phpMyAdmin with the standard WordPress tables (e.g., wp_posts, wp_users, wp_options, etc.).
- If any tables are missing, you may need to restore them from a backup.