WP Division Help

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

A few things are as frustrating as opening your website and only being greeted with the dreaded “Error Establishing a Database Connection” This error can feel overwhelming, but it’s a common issue with clear solutions.

Let’s break down what this error means, what causes it, and how to fix it whether you’re using shared hosting or a VPS/dedicated server.

What does “Error Establishing a Database Connection” mean?

This error means WordPress cannot communicate with the database that stores all of your site’s essential data. The database contains posts, pages, user information, and settings. When WordPress cannot retrieve this data, it displays the error. This usually occurs due to misconfigured settings, resource limits, or server-side issues.

This error means your website is entirely down and inaccessible to visitors. It’s a critical issue that needs immediate attention. Let’s explore the most common causes of this problem.

What causes this error?

  • Incorrect database credentials – If the username, password, or database name in the configuration file is wrong, WordPress cannot connect.
  • Corrupted database – Damaged tables can disrupt database functionality.
  • Server issues – Overloaded or misconfigured servers can result in connection errors.
  • Exceeded database connection limits – If the maximum allowed connections are exceeded, WordPress cannot establish a connection.

Step-by-step guide to fix the error establishing a database connection

Let’s move on to troubleshooting these issues.

Step 1: Confirm the problem

  1. Test the wp-admin area – Visit yourdomain.com/wp-admin. If you see a different error message, such as “One or more database tables are unavailable,” the database may be corrupted. If the same “Error Establishing a Database Connection” appears, proceed with the next steps.
  2. Check other websites on the same server – If you host multiple sites and all of them are down, the issue is likely with the server itself.
  3. Review error messages – Specific error messages can help identify the root cause.

Step 2: Check database credentials

Database credentials are stored in the wp-config.php file. Verify these settings:

  1. Access wp-config.php – Connect via cPanel File Manager, FTP, or SSH. The file is in the WordPress root directory.

Then open the wp-config.php file. Find these lines:

wp-config.php database connection:

define('DB_NAME', 'database_name');
define('DB_USER', 'database_username');
define('DB_PASSWORD', 'database_username_password');
define('DB_HOST', 'localhost');

DB_NAME – Ensure this matches the database name. You can check the database name in hosting panel. If you use cPanel check it in “MySQL® Databases”.

DB_USER and DB_PASSWORD – Confirm the username and password for the MySQL are correct. In cPanel is in the same menu “MySQL® Databases”. If in some reasons the credentials are changed, the WordPress can’t make connection with the database. Don’t forget to check if the user is added to the database (the last option).

DB_HOST – For shared hosting, this is typically localhost. On VPS or dedicated servers, it might be an IP address or hostname. Check the information in the panel or contact hosting provider if you have concerns.

Test the credentials – Use a PHP script (explained in Step 3) to confirm they are correct.

Step 3: Test the database connection manually

Testing the connection directly can pinpoint issues.

Create a file named “testdb.php“. Place it in the WordPress root directory. Add the following code:

<?php
$link = mysqli_connect('localhost', 'your_username', 'your_password', 'your_database_name');
if (!$link) {
    die('Could not connect: ' . mysqli_error($link));
}
echo 'Connected successfully';
?>

Don’t forget to replace your_username, your_password and your_database_name with real settings.

Run the file in your browser. Go to https://yourdomain.com/testdb.php . If you see “Connected successfully,” the credentials are correct. If not, recheck credentials or contact hosting support.

Step 4: Repair the database

The the database maybe corrupted. So enable repair mode: Add the following line to wp-config.php:

define(‘WP_ALLOW_REPAIR’, true);

Then access the repair tool. Visit https://yourdomain.com/wp-admin/maint/repair.php and follow the instructions.

Repair WordPress database

Disable repair mode. Remove the line from wp-config.php after completing the repair.

Step 5: Address MySQL database connection limits

If the previous steps don’t fix the problem, it’s time to check if the issue is related to the MySQL database connection limits.

What does it mean?

Database connection limits define the maximum number of concurrent connections a database user can handle at one time. In shared hosting, this limit is often 30 connections per database user. These connections occur in milliseconds, so hitting the limit doesn’t mean only 30 visitors can access your site. It usually indicates an underlying problem.

Why does this happen?

  • Slow MySQL queries – Inefficient database queries can take longer to process, tying up connections.
  • Aggressive bots – Bots scanning your site excessively can consume connections quickly. This often happens alongside high CPU or memory usage.
  • Traffic spikes – Sudden increases in visitors during a campaign or promotion can overwhelm the server.

Steps to address MySQL database connection limits

  • Identify slow MySQL queries – Use a plugin like Query Monitor to identify and fix inefficient queries in your WordPress setup.
  • Block aggressive bots – Install a security plugin such as Wordfence to monitor and block unwanted traffic. Alternatively, use server-side tools like mod_security or Cloudflare.
  • Distribute database users – Ensure each site on your hosting account uses a unique database user. This prevents one site from consuming the connection limits of another.
  • Enable caching Implement a caching plugin like WP Super Cache or W3 Total Cache. This reduces the number of requests sent to the database by serving static versions of your pages.
  • Upgrade hosting – If the issue persists, consult your hosting provider to increase the connection limit or upgrade to a higher-tier plan better suited for your traffic levels.
  • Server-wide problems – If all sites on the same hosting account experience connection errors simultaneously, it’s likely a server-wide issue. Some hosting providers use tools like CloudLinux’s MySQL Governor to manage resources across accounts. Contact your hosting provider to confirm and resolve these limitations.

Step 6: Check server resources

For shared hosting:

  • Check the resource usage in your hosting control panel. If limits are exceeded, consider upgrading your plan.
  • Contact your hosting provider for clarification on MySQL connection limits and other configurations.

For VPS/dedicated servers:

  • Restart MySQL server
  • Check error logs, usually at /var/log/mysql/error.log
  • Use tools like df -h and free -m to ensure sufficient disk space and memory.

Step 7: Find profesional help

If all else fails, seek professional help. Be prepared to share the troubleshooting steps you’ve already taken for a more efficient resolution.

Our WP team can resolves issues like this. For a one-time fee of $49, we’ll identify the root cause and implement the fix quickly, so you can get back to business without unnecessary delays. Contact us!