DEV Community

Cover image for How to Fix Error Establishing a Database Connection in WordPress
Meghna Meghwani for ServerAvatar

Posted on Originally published at serveravatar.com

How to Fix Error Establishing a Database Connection in WordPress

You’re working on your WordPress site. Everything looks fine one minute, and the next, a stark white page with a single line of text stares back at you: “Error Establishing a Database Connection”. This WordPress database connection error means your posts, pages, comments, user data, all of it lives inside that database. And WordPress is telling you it can’t reach it anymore.

I’ve been there. More importantly, I’ve fixed this error on dozens of sites, my own, client projects, and staging environments that somehow got into a bad state overnight. The good news is this: it’s almost always solvable, and it usually doesn’t take long.

This guide walks you through every real cause of this error and every real fix, ordered from the most common to the least common. I will explain what’s actually happening at each step, not just what to type, because understanding the “why” is what makes you faster the next time this happens.

TL;DR

Here’s the quick version before you dive in:

  • Most common cause: Wrong database credentials in wp-config.php
  • Second most common: Corrupted database tables from failed updates or plugin conflicts
  • Quickest test: Add WP_ALLOW_REPAIR to wp-config.php and visit the repair page
  • Always back up first, before touching anything
  • If multiple sites are down at once, it’s server issue, not a WordPress issue

What Triggers This Error?

Before reaching for fixes, it helps to understand the architecture. WordPress is a PHP application. Every time a visitor lands on your site, PHP code runs, asks your MySQL database for the content, and assembles a page to send back.

When that handshake breaks, you get the error. Here’s what typically causes it:

1. Incorrect Database Credentials

WordPress stores four critical pieces of information in your wp-config.php file, the database name, the username, the password, and the hostname. If any one of those is wrong, the connection gets refused. This is the single most common cause, responsible for the majority of cases I see.

2. Corrupted Database Tables

Over time, database tables can become corrupted, especially after a plugin fails mid-update, a theme install gets interrupted, or the server loses power at the wrong moment. When the table structure breaks, WordPress can’t read the data it needs, even if the credentials are perfect.

3. Database User Loses Permissions

A database user can exist with the right password but without the right privileges. MySQL permissions are managed separately from login credentials. If something strips those permissions, a bad migration script, a one-click “optimization” tool, or a hosting control panel glitch, WordPress gets locked out despite having correct-looking credentials.

4. The MySQL Server Itself Is Unresponsive

Sometimes the problem isn’t WordPress or your config, it’s that the MySQL service has stopped, run out of memory, or is being overwhelmed. This is a server-level problem, and it requires server-level attention.

5. Sudden Traffic Spikes Overwhelming the Database

A single plugin that goes viral on Hacker News, a social media mention from an influencer, these can flood your server with simultaneous connection requests. MySQL has a limit on concurrent connections, and when that limit fills up, new requests get refused.

6. Corrupted WordPress Core Files

This is rarer, but if a core WordPress file that handles database communication gets damaged, from a failed auto-update, for example, it can produce this exact error even when everything else is fine.

Step-by-Step: How to Fix the Error

Follow these steps in order. Each one eliminates a common cause. If step one doesn’t solve it, move to step two. By the time you reach step seven, you’ll have ruled out almost everything.

Step 1: Create a Backup Before Making Changes

Before troubleshooting database problems, create a backup of your website. Ideally, your backup should include:

  • WordPress files
  • WordPress database
  • Important configuration files

This gives you a recovery point if a repair operation or configuration change causes another problem.

Creating a Backup with ServerAvatar

If your WordPress website is managed through ServerAvatar, you can create website backup directly from the ServerAvatar dashboard.

ServerAvatar supports application backups that include the application’s files and associated database. It also provides options for storing backups to ServerAvatar storage or to your own cloud-storage provider from available options.

Important: Do not assume that a backup exists simply because backups were configured previously. Check that recent backups are actually available before proceeding.

Step 2: Verify Your Database Credentials in wp-config.php

This is one of the first things you should check. The wp-config.php file is normally located in the root directory of your WordPress installation.

Look for:

/** 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' );
Enter fullscreen mode Exit fullscreen mode

For more information about the config file and its database-related settings, see the official WordPress wp-config.php documentation

Managing Database Details with ServerAvatar

If your server is managed through ServerAvatar, you can manage databases and database users through the ServerAvatar dashboard instead of relying entirely on command-line database administration.

  • Navigate to your server panel by clicking on the server dashboard icon.

navigate to the server panel - WordPress database connection error

  • Go to the Application section, and click on your WordPress application dashboard icon.

navigate to the application panel - WordPress database connection error

  • Navigate to the File Manager section from the left sidebar, and navigate to the public_html folder.

edit wp config from file manager - WordPress database connection error

  • Open the wp-config.php file.

check and verify the details - WordPress database connection error

Check each mentioned value carefully:

  • DB_NAME : Make sure the database exists and that the name matches the database assigned to your WordPress installation.
  • DB_USER : Confirm that the database user exists.
  • DB_PASSWORD : Make sure the password matches the password configured for that database user.
  • DB_HOST : localhost is common when the database is running on the same server, but the correct value depends on your server architecture.

Do not automatically replace DB_HOST with localhost unless you know that the database is running locally.

After confirming the correct credentials, make sure the values in wp-config.php match your database configuration.

Read Full Article: https://serveravatar.com/wordpress-database-connection-error

Top comments (0)