Seeing the Error Establishing a Database Connection message on your WordPress site? This error can take your site offline, but don’t worry—here’s how to troubleshoot and fix it quickly.
Step 1: Check If Your Database Credentials Are Correct
Incorrect database credentials are a common reason for connection errors. WordPress stores this data in the wp-config.php file.
How to Verify:
- Access your File Manager via cPanel or an FTP client like FileZilla.
- Locate the wp-config.php file in the root directory.
- Find the following lines of code:
define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');
- Verify that:
- DB_NAME matches the database in your hosting control panel.
- DB_USER and DB_PASSWORD match your database login details.
- DB_HOST is typically
localhost
but may differ for some managed hosts.
✅ If the credentials are wrong, update them and save the file.
Step 2: Check If the Database Server Is Down
The database server could be temporarily down, especially on shared hosting plans.
How to Test:
- Log into your hosting control panel.
- Check server uptime or contact support to verify if MySQL is functioning.
- If you’re using a managed host like Kinsta or SiteGround, their support can confirm this for you.
✅ If the server is down, you may need to wait or contact your hosting provider.
Step 3: Repair Your WordPress Database
Corrupted tables can trigger database connection errors. WordPress has a built-in repair tool to fix this.
How to Repair the Database:
- Open your wp-config.php file.
- Add the following line at the bottom:
define('WP_ALLOW_REPAIR', true);
- Visit:
yourwebsite.com/wp-admin/maint/repair.php
. - Click Repair Database or Repair and Optimize Database.
✅ After the repair, remove the line from your wp-config.php
file for security reasons.
Step 4: Verify Hosting Resource Limits
If you’re exceeding your server’s resource limits, it can cause database errors.
How to Check:
- Log into cPanel and check the Resource Usage section.
- Look for CPU, RAM, or I/O Limit Exceeded warnings.
Fix Options:
- Upgrade to a higher hosting plan if your traffic has grown.
- Optimize your database (Step 5 below).
Step 5: Optimize Your WordPress Database
A bloated database can slow down your site or trigger connection issues.
How to Optimize:
- Install the WP-Optimize plugin.
- Go to WP-Optimize > Database.
- Select options like:
- Clean post revisions
- Remove spam comments
- Optimize database tables
- Run the optimization.
✅ Regular database maintenance keeps your site running smoothly.
Step 6: Check for Corrupted Plugins or Themes
A faulty plugin or theme could be interfering with the database connection.
How to Identify:
- Disable Plugins:
- Access your site via FTP.
- Rename the
/wp-content/plugins/
folder to/plugins-disabled/
.
- Switch Themes:
- Rename
/wp-content/themes/
to/themes-disabled/
.
- Rename
If the error resolves, reactivate plugins and themes one by one to identify the conflict.
Step 7: Verify File Permissions
Incorrect file permissions can also lead to database errors.
Correct File Permissions:
- wp-config.php:
440
or400
- All other files:
644
- All folders:
755
How to Fix:
- Use an FTP client.
- Right-click files and select File Permissions.
- Adjust as needed and save.
✅ File permissions should be strict to avoid security vulnerabilities.
Step 8: Check Your WordPress Site URL in the Database
If you’ve recently moved your site or changed domains, the site URL might be incorrect in the database.
How to Verify:
- Access phpMyAdmin from your hosting control panel.
- Open your WordPress database.
- Find the
wp_options
table. - Look for
siteurl
andhome
values. - Ensure both match your current domain.
✅ If they don’t match, update them and test your site.
Step 9: Restore a Backup
If all else fails, restoring a working backup can bring your site back online.
How to Restore:
- If you use plugins like UpdraftPlus or BackupBuddy, restore from the plugin dashboard.
- Hosting providers like Kinsta or WP Engine offer one-click restores.
✅ Always keep multiple recent backups to avoid data loss.
Step 10: Contact Your Hosting Provider
If you’re unable to resolve the error yourself, your hosting provider can assist.
Key Information to Provide:
- The error message you’re seeing.
- Steps you’ve already tried.
- Request them to check MySQL server health and error logs.
✅ Reliable hosts often provide database support as part of their service.
Step 11: Hire a WordPress Expert for Database Fixes
Still struggling with database connection errors? Avoid downtime and let a professional handle it for you.
Need Expert Help Fixing WordPress Database Errors?
Work with certified WordPress developers to quickly resolve database connection issues, repair corrupted tables, and optimise performance. Save time and avoid downtime!
FAQs About WordPress Database Connection Errors
What Causes a Database Connection Error in WordPress?
This error happens when WordPress can’t connect to the MySQL database due to:
- Incorrect database credentials
- Corrupted database tables
- Server downtime
- Plugin conflicts
Can a Plugin Cause a Database Connection Error?
Yes, poorly coded plugins or recent updates can trigger connection errors. To identify the issue, disable all plugins and reactivate them one by one.
How Do I Fix a Database Error After a Migration?
Ensure the DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST values in your wp-config.php file match your new hosting environment.
How Can I Prevent Database Connection Errors?
- Regularly back up your website.
- Keep plugins and themes updated.
- Use a reliable hosting provider.
- Optimize your database monthly.
Is There a Plugin to Fix Database Errors Automatically?
Yes, the WP-Optimize plugin can help clean and repair your database automatically.
What Is the Quickest Way to Fix a Database Error?
Check your wp-config.php file for incorrect credentials and run the WP_ALLOW_REPAIR tool.
Can Changing Hosting Fix Database Errors?
If your server resources are frequently maxed out, switching to a premium host like Kinsta, Cloudways, or WP Engine can prevent future database errors.
Fixing database connection errors can feel overwhelming, but following these steps should help you get your WordPress site back online quickly. If you’re unsure or need faster results, hiring an expert can save you time and prevent further issues.