How to Fix “Error Establishing a Database Connection” in WordPress

Error Establishing a Database Connection

If you see “Error Establishing a Database Connection,” it means your WordPress website is having trouble communicating with its MySQL database. The database is where all the content and information of your website is stored. This error prevents access to both the website and the WordPress Admin Dashboard. This could occur for several reasons, including incorrect login details, a corrupted database, server issues, or exceeding hosting limits. Whatever the reason, here we have a few practical, tested methods to fix the “Error Establishing a Database Connection” in WordPress.

What Does “Error Establishing a Database Connection” Mean?

WordPress is made up of a combination of code written in PHP and a database, typically running MySQL. PHP is required for running the functions on your WordPress site that allow posting comments, sharing files, retrieving pages, and so forth. WordPress also requires a database to store settings, posts, contributions, links to uploaded files and much more. When someone visits your site, WordPress uses PHP to query the database and fetch the data it needs to display the page. If, for some reason, WordPress cannot connect to the database, it shows the Error Establishing a Database Connection message instead of your site.

Error Establishing a Database Connection

This could happen for several reasons. Some of the most common causes include:

  • Incorrect Database Credentials: Mismatched database name, username, password, or host in your wp-config.php file.
  • Corrupted Database: Database tables may be corrupted due to plugin conflicts, server crashes, failed updates, or malware.
  • Database Server Issues: The database server may be down, overloaded, or experiencing connectivity problems.
  • Corrupted WordPress Files: Core files, themes, or plugins may be damaged or misconfigured.
  • High Traffic or Resource Limits: A sudden traffic spike or hosting resource limits can overwhelm the database server.

In most cases, this error shows up either when the database is corrupted or the database login credentials are invalid. Both of which are easy to fix and won’t take more than a few minutes to fix.

Repair the WordPress Database

A corrupted database can cause connection errors. However, WordPress offers a built-in repair tool that makes it easy to repair the WordPress Database in a few minutes. Here’s how you can do it:

  • First, log in to cPanel, open the File Manager app
  • Navigate to the WordPress installation folder.
  • Now, find the wp-config.php file,
  • Select the edit option and add the following line of code at the end of it:

define(‘WP_ALLOW_REPAIR’, true);

This line of code will do is it will allow you to enter the database repair mode for your WordPress website.

To start the repair process, visit the following page in your browser:

yourdomain.com/wp-admin/maint/repair.php

Example for me: https://www.techaithreads.com/wp-admin/maint/repair.php

WordPress Database Repair

On this repair page, you can either choose repair or repair and optimize. We recommend that you to go with repair and optimization, but you should note that it takes more time than just the repair function.

Once complete, remove the WP_ALLOW_REPAIR line from wp-config.php for security.

Alternatively, use phpMyAdmin in your hosting control panel to repair tables manually.

Check Your Database Credentials in wp-config.php

Your WordPress installation uses the wp-config.php file to store database connection details. If these credentials are incorrect, the connection will fail.

  • Step 1: Access your WordPress files via FTP or your cPanel File Manager
  • Step 2: Locate the wp-config.php file in the root directory of your WordPress installation.
  • Step 3: Open the file and verify the following lines:

define(‘DB_NAME’, ‘your_database_name’);
define(‘DB_USER’, ‘your_database_username’);
define(‘DB_PASSWORD’, ‘your_database_password’);
define(‘DB_HOST’, ‘your_database_host’);

If you don’t know the name of your database, you can find it with PHPMyAdmin. It’s a free application that comes with all hosting solutions.

  • Step 4: Confirm these details match the ones provided by your hosting provider. You can find them in your hosting control panel (e.g., cPanel, Plesk).
  • Step 5: If credentials are incorrect, update them, save the file, and refresh your website.

Note: If you’re not sure about the credentials, contact your hosting provider or check your hosting account’s database management section.

Check if the Database Server is Down

Sometimes, the problem isn’t your site but your hosting provider’s database server.

  • Step 1: Contact your hosting provider to confirm if the database server is running.
  • Step 2: Check for any scheduled maintenance or server outages.
  • Step 3: If you’re on a shared hosting plan, ask if resource limits are causing the issue.

If you have SSH access, run: mysqladmin -u username -p status. This checks if the database server is responding.

If the server is down, your host will need to resolve it. Consider upgrading to a better hosting plan if outages are frequent.

Address Resource Limits or Traffic Spikes

If your site experiences high traffic or your hosting plan has limited resources, the database may become overwhelmed.

  • Step 1: Check your hosting control panel for resource usage (CPU, RAM, etc.).
  • Step 2: Enable caching using plugins like WP Rocket or W3 Total Cache to reduce database load.
  • Step 3: Consider upgrading to a higher-tier hosting plan or a managed WordPress hosting service.

Increase PHP Memory Limit

Again, if your site uses too much memory, the connection can fail.

  • To fix this, access and edit your wp-config.php file.
  • Add define( ‘WP_MEMORY_LIMIT’, ‘256M’ );
  • Save and check your site again.

Restore a Previous Backup

If the issue started after an update or major change, restoring a backup can help.

  • Use your hosting provider’s backup tool or a WordPress plugin like UpdraftPlus or All-in-One WP Migration.
  • Restore both the database and files.

Inspect WordPress Core Files, Themes, and Plugins

Corrupted WordPress files or conflicts between plugins/themes can trigger this error.

Disable all plugins and switch to a default theme. If the error resolves, reactivate items one by one to identify the culprit.

  • Step 1: Deactivate all plugins by renaming the plugins folder in wp-content to plugins_old via FTP.
  • Step 2: Check if the site loads. If it does, reactivate plugins one by one to identify the culprit.
  • Step 3: Switch to a default theme (e.g., Twenty Twenty-Three) by renaming your active theme’s folder in wp-content/themes.

Reinstall WordPress Core Files

Again, if your WordPress core files are corrupted, it may also cause WordPress error establishing a database connection

  • Download the latest version of WordPress from wordpress.org.
  • Extract the ZIP file.
  • Upload all files except the wp-content folder and wp-config.php to your server, replacing existing files.
  • This ensures your core files are fresh without affecting your themes and plugins.

Contact Your Hosting Provider

If none of the above works, contact your host. They can:

  • Restart the database server.
  • Fix permission issues.
  • Diagnose hardware or network problems.

How to Prevent This Error in the Future

  • Use reliable hosting with good uptime and support.
  • Regularly update WordPress, themes, and plugins to avoid compatibility issues.
  • Optimize your database using plugins like WP-Optimize.
  • Enable automatic backups so you can restore quickly if needed.
  • Monitor site uptime with tools like UptimeRobot.

Frequently Asked Questions (FAQ)

1. What causes “Error Establishing a Database Connection” in WordPress?
It happens when WordPress can’t connect to your database, often due to incorrect credentials, a corrupted database, or server issues.

2. How do I fix this database connection error?
Check your wp-config.php credentials, repair the database, and contact your hosting provider if the server is down.

3. Can a plugin cause this error?
Yes, faulty plugins can overload or corrupt the database, leading to connection issues.

4. Is “localhost” always the correct database host?
No, some hosting providers use a different hostname—check with your host for accuracy.

5. Can high traffic trigger this error?
Yes, sudden traffic spikes can exceed hosting limits, causing database connection failures.

Kelvin smith

I am Kelvin Smith, founder of TechAIThreads. I have completed a Bachelor of Arts (BA) and am a Microsoft Certified Professional (MCP). With a strong background in computer applications, I love writing practical guides on Microsoft Windows (11, 10, etc.), Android, cybersecurity, WordPress, and more.