Skip to content

How to fix "Error establishing a database connection" in WordPress

WordPress reached your web server but could not open its database. Your posts and settings are still there. In most cases the cause is a database name, user, password or host in wp-config.php that no longer matches.

By
WP Ministry
Published
Tested on
WordPress 7.1.3, PHP 8.3.35

In short

  • Nothing has been deleted. The error is about reaching the database, not about what is in it.
  • Check the four database lines in wp-config.php against your host's control panel first.
  • If /wp-admin shows a message about tables instead, the database needs a repair, not new credentials.
  • If the details are right and the error comes and goes, the database server is refusing connections and your host has to look.

This message means WordPress could not log in to its database. WordPress keeps every post, page, user and setting in a MySQL or MariaDB database, and it needs four details to reach it: the database's name, a user, that user's password and the server's address. When any of them is wrong, or the database server does not answer, WordPress has nothing to build a page from and shows this message instead.

Your content has not been deleted. It is still in the database, and the site comes back as soon as WordPress can connect again.

Find out which cause you have

Open /wp-admin on your site, for example https://example.com/wp-admin.

  • If you see the same message, the problem is the connection itself: the details in wp-config.php, or the database server. Start with the first fix.
  • If you see "One or more database tables are unavailable. The database may need to be repaired.", WordPress connected but found a damaged table. Go straight to the repair fix.

Where it goes wrong

A page request passes through each of these in turn. This one comes from the database.

  1. Browser
  2. DNS
  3. HTTPS
  4. CDN or firewall
  5. Web server
  6. PHP
  7. WordPress
  8. Database and files (this error comes from here)

What causes it

  • The database details in wp-config.php no longer match

    Common

    The database name, user, password or host was changed at the host, or the site was moved and the old values came with it.

    Fix: Check the database details in wp-config.php

  • The database server is down or refusing connections

    Common

    The server that runs MySQL or MariaDB has stopped, is restarting, or has reached its limit on simultaneous connections.

    Fix: Have the host check the database server

  • One or more database tables are damaged

    Sometimes

    A table was left in a broken state, often after the server ran out of disk space or was stopped mid-write.

    Fix: Repair the database tables

How to fix it

Check the database details in wp-config.php

  • Easy
  • Back up first
  • About 10 minutes
  • Steps tested on WordPress 7.1.3
  1. Step 1: Open wp-config.php

    It is in the folder WordPress is installed in, beside the wp-content folder. Use your host's file manager, or connect over SFTP.

  2. Step 2: Find the four database lines

    They look like this. The values will be your own.

    wp-config.php
    define( 'DB_NAME', 'your_database_name' );
    define( 'DB_USER', 'your_database_user' );
    define( 'DB_PASSWORD', 'your_database_password' );
    define( 'DB_HOST', 'localhost' );
  3. Step 3: Compare each one with your host's control panel

    Look for a section named Databases or MySQL Databases. The database name and the user must match exactly, including any prefix your host adds, such as account_wp123. Make sure that user is attached to that database.

  4. Step 4: Reset the password if you are not sure of it

    You cannot read an existing database password, but you can set a new one for the user in the control panel. Put the same new password in the DB_PASSWORD line.

  5. Step 5: Check the host

    Most hosts use localhost. Some use a separate server name, sometimes with a port, such as mysql.example.com:3307. Your host's documentation or the database section of the control panel shows the right value.

  6. Step 6: Save the file and reload the site

    If a page loads, one of the four details was the cause. If nothing changes straight away, wait a few seconds and reload: PHP can go on using its stored copy of the file for a moment after you save.

If you have WP-CLI, you can set a detail without opening the file. This sets the password:

bash
wp config set DB_PASSWORD 'your_database_password'

Repair the database tables

  • Takes care
  • Back up first
  • About 10 minutes
  • Steps tested on WordPress 7.1.3
  1. Step 1: Turn the repair page on

    Add this line to wp-config.php, above the line that says "That's all, stop editing".

    wp-config.php
    define( 'WP_ALLOW_REPAIR', true );
  2. Step 2: Run the repair

    Visit /wp-admin/maint/repair.php on your site and choose Repair Database. WordPress checks each table and reports what it fixed.

  3. Step 3: Turn the repair page off

    Delete the line you added and save the file.

  4. Step 4: Reload the site

    If the page loads, a damaged table was the cause.

If you have WP-CLI, the same check and repair run from the command line and need no change to wp-config.php.

bash
wp db check
wp db repair

To undo it: Remove the WP_ALLOW_REPAIR line from wp-config.php.

Have the host check the database server

  • Easy
  • No risk
  • About 15 minutes

If the details are right and the tables are sound, WordPress is asking correctly and the database server is not answering. That is not something you can fix from inside WordPress.

  1. Step 1: Check your host's status page

    A database outage on shared hosting usually affects many sites and is usually announced.

  2. Step 2: Contact your host's support

    Tell them the site shows "Error establishing a database connection", that you have confirmed the details in wp-config.php, and when it started. Ask whether the database server is running and whether the account has reached a limit on connections.

  3. Step 3: If it happens at busy times

    A site that fails only under load is running out of database connections. A caching plugin reduces how often WordPress asks the database, and a larger hosting plan raises the limit. Which one you need depends on what is using the connections, and that is worth having someone look at.

When to get help

If the details in wp-config.php are right, the repair page reports no problems and the error is still there, the cause is on the database server. Only your host, or someone with access to the server, can see why.

Common questions

Have I lost my posts?

No. The message means WordPress cannot reach the database. It says nothing about what the database holds. A backup is still worth having before you change anything.

Why did it start without my changing anything?

The usual reasons are a password changed at the host, a site moved to a new server with its old details, a database server restarted by the host, or a traffic spike that used up the available connections.

Can a plugin cause this?

Not directly. A plugin cannot change the details in wp-config.php. A plugin that makes a very large number of database requests can use up the connection limit, which shows the same message.

More on this subject

Would you rather we fixed it?

Quick Fix is $49. One issue, one site, up to about an hour. No fix, no fee. 30-day warranty. It starts with a free diagnosis.