How to Fix "Error Establishing a Database Connection"

The "Error Establishing a Database Connection" is a common issue that prevents a website from accessing its database. This error is often seen in WordPress and other database-driven websites when the connection between the website and the database server is disrupted. It can be caused by incorrect credentials, a corrupted database, server issues, or high traffic loads. In this guide, we will go through various troubleshooting steps to fix this error effectively.


1. Check Database Credentials

Incorrect database credentials are one of the most common reasons for this error. To verify them:

  1. Access the website files via FTP or File Manager in your hosting control panel.

  2. Locate and open the wp-config.php file (for WordPress sites).

  3. Look for the following lines:

    define('DB_NAME', 'your_database_name');
    define('DB_USER', 'your_database_user');
    define('DB_PASSWORD', 'your_database_password');
    define('DB_HOST', 'localhost');
  4. Cross-check these details with the database information in your hosting control panel under MySQL Databases.

  5. If the credentials are incorrect, update them in the wp-config.php file and save the changes.

After making the corrections, refresh your website to check if the error is resolved.


2. Check if the Database Server is Down

Sometimes, the issue is due to the database server being temporarily down.

  • If you have access to phpMyAdmin, try logging in to see if the database is accessible.

  • Contact your hosting provider to check for any ongoing server issues.

  • If using a VPS or dedicated server, restart the MySQL service:

    sudo service mysql restart

    For cPanel users, you may need to restart MySQL from the hosting dashboard.


3. Repair the WordPress Database

A corrupted database can also cause this error. WordPress has a built-in repair tool:

  1. Open the wp-config.php file and add this line:

    define('WP_ALLOW_REPAIR', true);
  2. Save the file and go to:

    https://yourwebsite.com/wp-admin/maint/repair.php
  3. Click Repair Database or Repair and Optimize Database.

  4. Once done, remove the WP_ALLOW_REPAIR line from wp-config.php for security reasons.


4. Check for Corrupt WordPress Files

If core WordPress files are corrupted, re-uploading fresh files may fix the issue:

  1. Download the latest WordPress version from wordpress.org.

  2. Extract the files and delete the wp-content folder (to preserve your themes and plugins).

  3. Upload the remaining files via FTP to overwrite existing ones.

  4. Refresh your website to check if the error is fixed.


5. Restore a Recent Backup

If the issue started after recent changes, restoring a previous backup might resolve it:

  • Use your hosting provider’s backup service if available.

  • If using a backup plugin (e.g., UpdraftPlus, VaultPress), restore the latest working backup.


6. Increase PHP Memory Limit

A low PHP memory limit can cause database connection errors. To increase it:

  1. Open the wp-config.php file and add:

    define('WP_MEMORY_LIMIT', '256M');
  2. Save the file and reload your site.

If that doesn’t work, modify the php.ini file (if accessible):

memory_limit = 256M

Then restart the web server.


7. Disable Plugins and Themes

Sometimes, a faulty plugin or theme can cause database connection issues.

Disable Plugins

  1. Access your website files via FTP.

  2. Navigate to wp-content/plugins/ and rename the plugins folder to plugins_backup.

  3. Refresh your site. If the error is resolved, a plugin is the culprit.

  4. Rename the folder back and disable plugins one by one to identify the problem.

Switch to a Default Theme

  1. Go to wp-content/themes/.

  2. Rename your active theme folder (e.g., mytheme to mytheme_backup).

  3. WordPress will automatically switch to a default theme.

  4. Check if the issue is resolved.


8. Restart Your Web Server

For VPS or dedicated hosting users, restarting the web server can resolve connection issues:

  • Restart Apache:

    sudo service apache2 restart
  • Restart Nginx:

    sudo service nginx restart
  • Restart MySQL:

    sudo service mysql restart

9. Check for High Traffic or Server Overload

A sudden spike in traffic can overload the server and cause the database to become unresponsive.

  • Monitor server resource usage via cPanel or SSH (top or htop commands).

  • Optimize the database using a plugin like WP-Optimize.

  • Upgrade your hosting plan if necessary.


10. Contact Your Web Host

If none of the above steps work, reach out to your hosting provider’s support team. Provide details such as:

  • The error message displayed.

  • Steps you have taken to troubleshoot.

  • Any recent changes made to the site.


Conclusion

The "Error Establishing a Database Connection" can occur due to incorrect database credentials, server issues, corrupted files, or resource limits. By systematically checking credentials, server status, database integrity, and file corruption, you can resolve the issue efficiently. If the problem persists, contacting your web host for support is the best option.

Last updated

Was this helpful?