Error establishing a database connection

PlatformWordPress Doing it yourself15 to 45 minutes Checked

The short answer

WordPress cannot talk to the database where your content lives. The content is almost always still there. The usual reasons are wrong login details in wp-config.php after a move or password change, the database server being down or overloaded, or damaged tables.

Is this your problem?

  • Every page shows "Error establishing a database connection"
  • wp-admin says "One or more database tables are unavailable"
  • It started after moving the site, or changing a hosting password
  • The site comes and goes during busy times

What usually causes it

1
Wrong database name, user, password or host in wp-config.php

How to tell: It started after a move, a restore, or a password change in the host panel.

2
The database server is down or overloaded

How to tell: Other sites on the same host fail too, or the host status page shows an incident. It often comes and goes.

3
Damaged database tables

How to tell: wp-admin shows a different message about tables that need to be repaired.

4
The hosting account hit a limit

How to tell: The disk is full, or the host throttles the account during traffic spikes.

Before you touch anything

  • Download a copy of wp-config.php before you edit it.
  • If the host panel offers a database backup, take one.

How to fix it, step by step

  1. 1

    Check whether it is the host

    Look at your host status page and try another site on the same account. If the host is having an outage, there is nothing to fix on your side. Wait, and ask them for a time.

  2. 2

    Compare wp-config.php with the host panel

    Open wp-config.php and find these four lines. Check each value against the database and user listed in your host panel, usually under MySQL Databases. DB_HOST is often localhost, but some hosts use their own address.

    define( "DB_NAME", "database_name" );
    define( "DB_USER", "database_user" );
    define( "DB_PASSWORD", "password" );
    define( "DB_HOST", "localhost" );
  3. 3

    Reset the database password if unsure

    In the host panel, set a new password for the database user, make sure that user is attached to the database with all privileges, and put the new password in wp-config.php.

  4. 4

    Repair damaged tables

    Add this line to wp-config.php, then visit yoursite.com/wp-admin/maint/repair.php and choose Repair Database. Remove the line straight after: while it is there, anyone can open that page.

    define( "WP_ALLOW_REPAIR", true );
  5. 5

    Check the disk and limits

    In the host panel, check disk usage and resource limits. A full disk stops the database writing. Clear old backups or logs, or ask the host to raise the limit.

Stop and get help if

  • The database is missing from the host panel altogether.
  • You see tables with names you do not recognise, or an admin user you did not create.
  • The only way back is restoring a backup, and the site takes orders or bookings.
Fix it for me

Stop it happening again

  • Keep a record of database details somewhere safe when you move a site.
  • Watch uptime so you hear about outages before customers do.
  • Choose hosting sized for your traffic, not the cheapest plan.

Questions people ask

Is my content gone?

Almost never. The site cannot reach the database, but the database is usually intact.

Why does it only happen sometimes?

That points to the database server being overloaded, often on shared hosting during busy periods.

Can a hack cause this?

It can, when an attacker changes the database password. Check for unfamiliar admin users once the site is back.

Still broken? We can fix it.