"Error Establishing a Database Connection" in WordPress

This error indicates WordPress cannot connect to your database, usually due to incorrect credentials, database corruption, or server issues. Here’s how to troubleshoot and fix the issue.

1. Check Database Credentials in wp-config.php

Start by verifying the credentials stored in your WordPress configuration file.

  • Login to cPanel.
  • Open File Manager under the Files section.
  • Navigate to your WordPress root directory (usually located in /public_html/).
  • Find and open the wp-config.php file for editing.

Within the file, you’ll find entries like this:

define('DB_NAME', 'your_database_name'); 
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');

Take note of the following values:

  • DB_NAME: Your database name.
  • DB_USER: The MySQL username.
  • DB_PASSWORD: The password for that user.
  • DB_HOST: This is typically set to localhost unless your hosting provider uses a different hostname for databases.

2. Verify Database Details in cPanel

Now that you’ve noted the database credentials from wp-config.php, you need to confirm that they match your actual database details.

  • In cPanel, go to MySQL Databases under the Databases section.
  • Look for the Current Databases section.
    • Verify that the DB_NAME from wp-config.php matches an existing database in this list.
    • Check that the User assigned to this database matches the DB_USER in wp-config.php.

If any of these details don’t match, you’ll need to update them in the wp-config.php file to reflect the correct database name and username.

3. Reset the MySQL User Password

If the database details are correct, the issue might be with the database password. You can reset the MySQL user’s password and update it in wp-config.php.

Steps:

  • In cPanel, go to MySQL Databases.
  • Scroll down to the Current Users section.
  • Find the MySQL user matching the DB_USER value in wp-config.php.
  • Click Change Password next to the user and enter a new, strong password.

Now, update the DB_PASSWORD in your wp-config.php file with the new password:

define('DB_PASSWORD', 'new_password_here');

Save the changes in the file.

4. Ensure MySQL User Has Proper Permissions

The MySQL user needs the correct permissions to interact with the database. If the permissions are incorrect, the connection will fail.

Steps:

  • In cPanel, under MySQL Databases, scroll to the Add User to Database section.
  • Select the correct MySQL User and Database from the dropdown menus.
  • Click Add.
  • On the next screen, ensure All Privileges are selected, then click Make Changes.

This grants the MySQL user full access to the database, ensuring proper functionality.


How did we do?


Powered by HelpDocs (opens in a new tab)
© Krystal Hosting Ltd 2002–