The "Error Establishing a Database Connection" message means WordPress cannot communicate with its database. This is one of the most common WordPress errors, but it's usually fixable in minutes by verifying your database credentials.
What Causes This Error
WordPress needs a database to store your posts, pages, comments, users, and settings. If WordPress can't connect, it's typically due to:
- Incorrect database name, username, or password.
- Database server is down or unreachable.
- Database user doesn't have permission to access the database.
- Database server address is wrong (usually localhost).
Step 1: Verify Your Database Credentials
Your WordPress configuration file stores the database connection details. You'll need to check them against what your hosting account actually has.
1. Find your database credentials:
- Log in to your hosting control panel.
- Look for "MySQL Databases" or "Databases" section.
- Find the database associated with your WordPress installation (you may have created it when setting up WordPress).
- Note the database name, username, and password (if you don't remember the password, you can usually reset it in the control panel).
- The hostname is usually localhost for sites on the same server as the database.
2. Open your WordPress configuration file:
- Use FTP or your control panel's File Manager to navigate to your site's root directory (usually public_html).
- Find the file named wp-config.php (it's in the root directory, not in a subfolder).
- Right-click it and select "Edit" (in File Manager) or download it (via FTP).
3. Check the database settings in wp-config.php:
```php
// MySQL settings //
define( 'DBNAME', 'yourdatabase_name' );
define( 'DBUSER', 'yourdatabase_user' );
define( 'DBPASSWORD', 'yourdatabase_password' );
define( 'DB_HOST', 'localhost' );
```
4. Compare with your control panel:
- Does DB_NAME match the database name in your control panel? If not, correct it.
- Does DB_USER match the database user? If not, correct it.
- Is DB_PASSWORD the correct password? If you're not sure, reset it in your control panel and update it here.
- Is DB_HOST correct? It's almost always localhost unless your host told you otherwise.
- Save the file and refresh your site. If you edited it in File Manager, click Save. If you downloaded it, upload the corrected version back via FTP.
Step 2: Check If Your Database User Has Permission
Even if the credentials are correct, the database user might not have permission to access the database.
1. In your control panel, go to MySQL Databases or similar.
2. Look for a "Users" or "User Privileges" section.
3. Verify the database user is associated with the correct database and has at least these permissions:
- SELECT
- INSERT
- UPDATE
- DELETE
- CREATE
- ALTER
- DROP
- INDEX
4. If the user doesn't have these permissions, remove it from the database and re-add it with full permissions.
Step 3: Check If the Database Exists
Sometimes a database is accidentally deleted or was never created.
1. In your control panel, go to MySQL Databases.
2. Look for your database in the list (the name from Step 1).
3. If it's missing, you'll need to create a new one:
- Click "Create Database."
- Enter the database name (must match what's in wp-config.php).
- Click Create.
Step 4: Test the Connection
If you've corrected your credentials, test the connection.
- Refresh your website in your browser.
- If the error persists, wait 5 minutes (sometimes changes take a moment to apply) and try again.
Step 5: Check If Your Database Server Is Running
This is rare on shared hosting, but if the above steps didn't work, your database server might be down.
1. Contact your hosting provider's support and ask if:
- The MySQL/database server is running.
- There are any known issues or maintenance happening.
- They can verify your database user has access.
Step 6: Last Resort—Recreate WordPress Configuration
If you've checked everything and still see the error, you can regenerate the WordPress configuration.
- Delete
wp-config.phpfrom your site's root directory (via File Manager or FTP). - Visit your site in a browser (e.g., yourdomain.com).
- You should see the WordPress setup screen asking for your database details.
- Enter your credentials from your control panel again and click "Submit."
- Complete the setup and log back in to WordPress.
Prevention Tips
- Write down your database credentials and store them securely when you first create your database.
- Never change your database password without updating
wp-config.phpimmediately. - Use your control panel's backup feature regularly so you can restore if something goes wrong.
This error usually resolves within the first two steps. If you're stuck, your hosting provider's support team can help you verify the database settings.