Troubleshooting “Error Establishing a Database Connection” Issues in WordPress
One of the most common errors in WordPress is the “Error Establishing a Database Connection” message. This error typically occurs when WordPress fails to connect to the database that stores your site’s content and settings. Below are common causes of this issue and troubleshooting steps to help you resolve it.
Check Database Credentials
The first step is to ensure that the database credentials in your WordPress configuration file (wp-config.php
) are correct.
Steps
Open wp-config.php
: This file is located in the root directory of your WordPress installation.
Verify the following values: define('DB_NAME', 'your_database_name'); define('DB_USER', 'your_database_user'); define('DB_PASSWORD', 'your_database_password'); define('DB_HOST', 'localhost');
Confirm with Your Hosting Provider: If you’re unsure about the DB_HOST value, check with your host. In many cases, it is set to localhost
, but some providers use specific IPs or hostnames.
Test the Database Connection
Sometimes, database credentials may seem correct, but the database server might be down or the connection might be misconfigured.
Steps
- Create a test PHP file called
test-db.php
in your WordPress root directory. - Add the following code:
<?php $connection = mysqli_connect('localhost', 'your_database_user', 'your_database_password', 'your_database_name'); if (!$connection) { die('Connection failed: ' . mysqli_connect_error()); } echo 'Connected successfully!'; ?>
Run the file by navigating to http://yourwebsite.com/test-db.php
in your browser.
- If the connection fails, double-check the credentials.
- If the connection is successful, there might be another issue affecting WordPress.
Read: How To Set up and Configure Postman SMTP On WordPress
Repair the WordPress Database
If the database has become corrupted, you can try repairing it.
Steps
Add the following line to your wp-config.php
file:
define('WP_ALLOW_REPAIR', true);
Go to: http://yourwebsite.com/wp-admin/maint/repair.php
Choose either Repair Database or Repair and Optimize Database.
Once the repair is complete, remove the line from your wp-config.php
file for security purposes.
Verify Database Server Status
The database server may be temporarily down or unreachable.
Steps
- Check your hosting dashboard for server status or maintenance notifications.
- Contact your hosting provider to ensure the MySQL server is running properly.
- If you’re on a shared hosting plan, it’s possible the server is under high load, which can cause database timeouts.
Know more: PHP Missing The MySQL Extension Error In WordPress
Increase PHP Memory Limit
Sometimes the issue is related to a low PHP memory limit, especially if you’re running a resource-heavy site or multiple plugins.
Steps
Open wp-config.php
and add:
define('WP_MEMORY_LIMIT', '256M');
Save the file and refresh your site.
Correct Database Prefix Issues
If you recently modified the database prefix in wp-config.php
, you may have caused a mismatch.
Steps
Open wp-config.php
and find:
$table_prefix = 'wp_';
Ensure the prefix matches the one used in your database tables.
Example: If your tables start with wp123_
, update the prefix to: $table_prefix = 'wp123_';
Check for File Corruption or Malware
A corrupted WordPress core file or malware infection could also cause database connection issues.
Steps
Scan your site with a malware scanner or use a security plugin.
Re-upload core WordPress files: Download a fresh copy of WordPress and upload the wp-admin
and wp-includes
folders, replacing the existing ones.
Learn: How to Prevent WordPress Brute Force Attacks
Restart MySQL Service (For VPS/Cloud Hosting)
If you’re on a VPS or cloud server, you might need to restart the MySQL service.
Steps
- Access your server via SSH.
- Run the following command:
sudo service mysql restart
Check Permissions
Ensure that WordPress has the correct permissions to access the database.
Steps
- Log in to your hosting control panel and navigate to the MySQL Databases section.
- Verify that your database user has ALL PRIVILEGES assigned to the WordPress database.
Check for Conflicting Plugins or Themes
Some plugins or themes can conflict with the database connection.
Steps
- Rename the
plugins
folder toplugins_old
to deactivate all plugins. - If the site works, reactivate plugins one by one to identify the problematic one.
- If plugins are not the issue, switch to a default theme to rule out theme conflicts.
In Conclusion
Database connection issues in WordPress can arise from multiple sources, such as incorrect credentials, server issues, or corrupted files. Following these steps will help you systematically identify and fix the problem. If all else fails, contacting your hosting provider is a good final step, as they can check for deeper server-level issues.