Few WordPress errors are as frustrating as seeing the message Error Establishing a Database Connection. It instantly takes your site offline, blocks both visitors and admins, and can cause serious downtime if not resolved quickly.

This error occurs when WordPress cannot connect to your MySQL database, where all your posts, pages, and settings are stored. Without that connection, WordPress has nothing to display.

The good news is that this issue is common and almost always fixable.

In this guide, we’ll walk through exactly how to fix the error establishing a database connection in WordPress, step by step. From checking your wp-config.php file to repairing corrupted tables and optimizing your server, you’ll learn the proven fixes that quickly get your site back online.

Let’s get started!

What Causes ‘Error Establishing a Database Connection’ in WordPress?

Before we dive into the fix, it’s helpful to understand what triggers this error.:

  • Incorrect Database Credentials: This is the most common reason for this error. The wp-config.php file contains the database name, username, password, and host. If any of these are wrong, the connection will be refused.
  • Corrupted WordPress Database: Your database can become corrupted due to a faulty plugin update, a theme installation gone wrong, or a server glitch.
  • Corrupted WordPress Core Files: Although less common for this specific error, a damaged core file related to the database functions can cause the connection to fail.
  • Unresponsive Database Server: The server where your database is hosted might be down, overloaded, or experiencing technical issues. This is a problem on your hosting provider’s end.
  • Sudden Traffic Spikes: A massive surge in traffic can overwhelm your hosting server and temporarily make the database unresponsive to new connection requests.

Suggested Read: How to Create a Database for Your Web Application | RunCloud Docs

Step-by-Step Checklist to Fix ‘Error Establishing a Database Connection

Follow these steps in order, as they are arranged from the most common and easiest fix to the least common.

1. Back Up WordPress Website Before Troubleshooting

Before making any changes, always create a full backup of your website. This protects your files and database if something goes wrong. RunCloud users can do this in one click from the dashboard.

If you’re not using RunCloud, you can:

  • Use your hosting control panel (cPanel, Plesk, etc.) to generate and download a backup.
  • Download your WordPress files via FTP and export your database through phpMyAdmin.

Skipping this step risks turning a small fix into a much bigger problem.

Suggested Read: How to Connect a MySQL Database to PHP (A Developer’s Guide)

2. Check and Update wp-config.php Database Credentials

Incorrect credentials are the most common cause of this error. To check and fix them:

  1. Connect to your site using FTP or your host’s File Manager. Open the wp-config.php file in the root WordPress folder. RunCloud users can edit this file directly from the dashboard.

Look for these lines of code:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'your_database_name' );
/** MySQL database username */
define( 'DB_USER', 'your_username' );
/** MySQL database password */
define( 'DB_PASSWORD', 'your_password' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
  1. Find your database details in your hosting control panel (often under “MySQL Databases”).
  2. Make sure the DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST values match exactly. Note: most hosts use “localhost” for DB_HOST, but some require a specific server name or IP.
  3. If anything doesn’t match, update wp-config.php, save the file, and reload your site.

Suggested Read: SQLite vs MySQL vs PostgreSQL (Detailed Comparison)

3. Repair WordPress Database via phpMyAdmin or WP-CLI

If your database tables are corrupted, WordPress cannot read them. Luckily, WordPress includes a built-in repair tool. This process fixes errors without deleting your data.

  1. Open your wp-config.php file.

Add this line just above /* That's all, stop editing! Happy publishing. */:

define('WP_ALLOW_REPAIR', true);
  1. Save the file, then go to:  http://runcloud-demo.com/wp-admin/maint/repair.php.
  2. On that page, click “Repair Database.”
Fix the error establishing a database connection
  1. When it finishes, delete the line you added in wp-config.php. Leaving it in place is a security risk.

Suggested Read: How to Install phpMyAdmin Easily Using RunCloud

4. Reset or Update WordPress Database User and Password

Sometimes the database user loses permissions even if the credentials are correct. To fix this:

  1. Open the “MySQL Databases” section in your hosting control panel. RunCloud users can do this directly from the dashboard.
  1. Locate the database user for your WordPress site.
  2. Reset the password or create a new user with a strong password.
  1. Make sure this user has full privileges for your WordPress database. In RunCloud, use the “Assign and Revoke Users” option to manage this.
  1. Update the new password in your wp-config.php file under DB_PASSWORD.

Suggested Read: How to Identify and Kill Queries with the MySQL Command-Line Tool

5. Reinstall or Replace Corrupted WordPress Core Files

If a WordPress core file is corrupted, replacing it with a clean copy usually fixes the issue. Don’t worry – this won’t affect your themes, plugins, or content.

  1. Download the latest version of WordPress from WordPress.org.
  2. Unzip the file on your computer.
  3. Delete the wp-content folder and wp-config-sample.php file from the unzipped package.
  4. Connect to your site via FTP and upload the remaining files, overwriting the old ones.
  5. This refreshes all core files with clean copies without touching your content.

Suggested Read: How to Migrate Your Server with (Near) Zero Downtime

6. Check Hosting Server Health and MySQL Service

If the error persists, the problem may be with your hosting server rather than WordPress.

  • Check Server Status: Most hosting providers publish updates about outages or maintenance. RunCloud does not provide server status updates, so check directly with your cloud provider (e.g., AWS, Vultr, DigitalOcean).
  • Test Other Sites: If you host multiple sites on the same account, check if they are also down. If they are, it’s likely a server-wide issue.
  • Contact Support: Open a ticket with your provider. Tell them you’re seeing an “Error Establishing a Database Connection” and that you’ve already confirmed your wp-config.php credentials. Ask them to confirm that the MySQL service is running.

7. Optimize WordPress Database and Enable Caching

If the error followed a sudden traffic spike, it may disappear once traffic drops. But this is a clear sign your site needs optimization.

With RunCloud, you can enable NGINX FastCGI caching in one click, or add Redis or Memcached directly from the dashboard. This helps your site handle heavy database loads without needing command-line tools. For detailed guides, see:

Advanced Troubleshooting for WordPress Database Connection Errors

If you’ve worked through the standard checklist and are still facing the error, it’s time to investigate deeper server-level configurations and resource limitations. These advanced steps address less common but critical issues that often require more direct control over your hosting environment. 

1. Update DB_HOST in wp-config.php

Most setups use localhost for DB_HOST, but some hosts run the database on a separate server. In those cases, you’ll need to replace localhost with the hostname or IP address your provider gives you (e.g., mysql.yourhost.com).

2. Increase PHP memory

Sometimes the error appears because PHP runs out of memory while processing a heavy query. To increase it, add this line to wp-config.php:

define('WP_MEMORY_LIMIT', '256M');

However, server-level settings can sometimes override this change. Manually editing the php.ini file via SSH is the most reliable method, but it can be intimidating for many users.

RunCloud gives you direct, graphical control over your server’s PHP settings. From your web application’s dashboard, you can select a new memory_limit via a text box and other critical values like max_execution_time and upload_max_filesize. This removes the risk of syntax errors in configuration files and instantly applies the changes only to the selected application.

3. Consider migrating to better hosting

If you see this error often, the problem may be your hosting provider. Shared hosting, in particular, is prone to overloaded databases. Moving to a VPS (DigitalOcean, Vultr, AWS, etc.) provides more stability.

The migration process can be daunting. It involves server provisioning, software installation (NGINX, PHP, MySQL), security configurations, and moving your site’s files and database.

RunCloud is designed to make this transition seamless. You can connect it to any VPS provider, and it will automatically provision, optimize, and secure the server with a modern server stack in minutes. This makes managing and deploying a WordPress website and any web application easy.

Wrapping Up: Preventing Database Connection Errors in WordPress

Fixing the “Error Establishing a Database Connection” is one thing – making sure it doesn’t happen again is even more important. Regular backups, reliable hosting, proper caching, and a clean server setup reduce the chances of future downtime.

That’s where RunCloud helps. Instead of juggling FTP clients, php.ini edits, and command-line fixes, you get a simple dashboard to manage everything – from database users to PHP settings and caching – in just a few clicks.

If you want fewer headaches, faster performance, and a WordPress site that stays online even under heavy load, RunCloud is built for you.

Take control of your server – start with RunCloud today and eliminate database errors for good.

FAQs on ‘Error Establishing a Database Connection’ in WordPress

Why is my WordPress site not connecting to the database?

Your site cannot connect because the PHP code is blocked from accessing the MySQL database where your content is stored. This is most often due to incorrect database credentials in your wp-config.php file. 

How do I fix database errors in cPanel?

In cPanel, you can fix this by using the “MySQL Databases” tool to check the database username and reset its password. Then, you can use the integrated phpMyAdmin tool to select your database and run the “Repair table” command on any corrupted tables. Platforms like RunCloud offer a more modern interface for these tasks, making user management and phpMyAdmin access much quicker.

What causes database issues on localhost XAMPP?

MySQL is usually not running. Open the XAMPP control panel and check that the MySQL module is started (green status). Also, confirm that your wp-config.php settings match the XAMPP defaults – DB_USER is root, and DB_PASSWORD is blank.

How do I repair a corrupted WordPress database?

Add the define(‘WP_ALLOW_REPAIR’, true); line to your wp-config.php file to activate WordPress’s built-in database repair script. Alternatively, you can use phpMyAdmin to select all your database tables and choose the “Repair table” option. Server management panels like RunCloud provide one-click access to phpMyAdmin, simplifying the process of performing these database repairs.

Why is my database error only happening on my iPhone?

This is almost always a caching problem; your iPhone is likely viewing an old, cached version of the error page that no longer exists. Clear your mobile browser’s cache or purge your site’s caching plugin to resolve it. If you use server-level caching, a tool like RunCloud allows you to easily manage and purge the NGINX FastCGI cache directly from your dashboard.

How can I check the MySQL server status in WordPress hosting?

You’ll need to contact support on most shared hosting to confirm the MySQL server is operational. However, you can manage this using a VPS through a server panel. RunCloud provides a real-time server health monitoring dashboard that displays the status of essential services like MySQL, NGINX, and Redis.

What should I do if database errors appear intermittently?

Intermittent errors suggest your database server is temporarily overloaded, often due to high traffic spikes or slow database queries from a plugin. This is a resource issue, and you should enable an object caching solution to reduce the database load. With RunCloud’s server health monitoring, you can watch for spikes in CPU and memory usage to diagnose when your server is under strain.

Author