Skip to content
webfixed

Guides / WordPress

Error establishing a database connection: how to fix it

By Michael Broadbridge. Last checked 14 November 2026 on WordPress 7.1.

Short answer

This message means WordPress cannot reach its database. Usually the details in wp-config.php no longer match the database, often after a password change or a move to new hosting, or the host’s database server is down. Check DB_NAME, DB_USER, DB_PASSWORD and DB_HOST against your hosting control panel, then ask your host whether the database server is running.

Symptoms

You may see one of these messages:

Error establishing a database connection

Cannot select database

One or more database tables are unavailable. The database may need to be repaired.

  • Every page of the site shows "Error establishing a database connection" as a heading on an otherwise blank page.
  • The dashboard shows a longer message asking whether the username, password and hostname are correct and whether the database server is running.
  • The dashboard says "One or more database tables are unavailable. The database may need to be repaired."
  • It started after a move to new hosting or a change of database password, or it comes and goes at busy times.

Causes, most likely first

  1. Wrong database details in wp-config.php

    The database name, username, password or host in wp-config.php do not match the real database. WordPress’s own documentation names this as the usual cause. It often follows a password change in the hosting control panel.

  2. The database server is down

    On most hosting the database runs as a separate service, sometimes on a separate server. If it has stopped or is being worked on, every site that uses it shows this error.

  3. A recent move to new hosting

    After a migration, wp-config.php can still point at the old host’s database, the database may not have been imported, or the database user may not have been given access to it.

  4. A hosting limit

    WordPress’s documentation notes that a host can shut a database down when it goes over its quota. MySQL also refuses new connections when all permitted connections are in use, which shows as an error that comes and goes at busy times.

  5. Damaged database tables

    If the dashboard says one or more database tables are unavailable, WordPress has connected but cannot read a table it needs, and the tables need repairing.

How to fix it

Read the longer message in the dashboard

Visitors only see the short message. WordPress shows a more detailed one on the dashboard address, and the wording tells you which part to check.

  1. Open your site’s address with /wp-admin/ on the end, for example yourdomain.co.uk/wp-admin/.
  2. If the message asks whether you have the correct username and password, the correct hostname, and whether the database server is running, check the details in wp-config.php next, then the database server.
  3. If the heading says "Cannot select database", WordPress logged in to the database server but could not open the database named in wp-config.php. Check DB_NAME, and check that the database user has permission to use that database.
  4. If it says one or more database tables are unavailable, the connection works and the tables need repairing. Go to the repair steps below.

Check the database details in wp-config.php

wp-config.php sits in the main folder of your site, next to the wp-content folder. Download a copy before you edit it, because a typing mistake in this file stops the site loading.

  1. In your hosting control panel, open the MySQL databases section, sometimes called Databases. Note the database name and the user assigned to it.
  2. Open wp-config.php in the file manager’s editor and find the four lines that start define( 'DB_NAME', define( 'DB_USER', define( 'DB_PASSWORD' and define( 'DB_HOST'.
  3. Compare DB_NAME and DB_USER with the control panel letter by letter. On some hosts both start with your account name and an underscore, such as account_wp1, and the full name is needed.
  4. You cannot read the existing password in most control panels. Set a new password for the database user there, then paste it into the DB_PASSWORD line, keeping the single quotes around it.
  5. DB_HOST is localhost on most hosting and rarely needs changing. Some hosts use a separate server name, or a port number on the end such as localhost:3307. Your host’s control panel or support team gives the correct value.
  6. Save the file and reload the site.

If the site shows a PHP error straight after you save, a quote mark or semicolon has been lost. Put your downloaded copy back and try again.

Check whether the database server is running

  1. Open phpMyAdmin from your hosting control panel. If it cannot connect to the database either, the fault is at the host and not in WordPress.
  2. Check your host’s status page for database or server problems.
  3. If you have other sites on the same hosting account, check them. If they show the same error, the database server is the likely cause.
  4. Contact your host. Ask whether the database server is running, whether your database is over its size quota, and whether your account is hitting a limit on database connections.

After moving to new hosting

  1. Open phpMyAdmin on the new host and check the database has tables in it. If it is empty, the database export from the old host still needs importing.
  2. Check that wp-config.php on the new host has the new host’s database name, user, password and host, not the old ones.
  3. In the new host’s MySQL databases section, check the database user has been added to the database with full privileges. A user that exists but has no permission on the database produces the "Cannot select database" message.

Repair damaged tables

Only do this if the dashboard says one or more database tables are unavailable. Take a backup of the database first, from your host’s backup tool or by exporting it in phpMyAdmin.

  1. Open wp-config.php and add this line above the line that begins "That’s all, stop editing": define( 'WP_ALLOW_REPAIR', true );
  2. Visit yourdomain.co.uk/wp-admin/maint/repair.php in your browser.
  3. Click Repair Database. Repair and Optimize Database also tidies the tables, but it takes longer and locks the database while it runs.
  4. WordPress lists each table as okay, repaired, or failed to repair.
  5. Remove the WP_ALLOW_REPAIR line from wp-config.php as soon as you have finished.

The repair page works without logging in, because a damaged database often stops logins too. Anyone who finds the address can use it while the line is in wp-config.php, which is why WordPress asks you to remove it once the repair is done.

If that didn’t work

  1. Some tables cannot be repaired

    If the repair page reports tables it could not fix, ask your host to run a repair on the database, or restore a database backup from before the problem started.

  2. The error comes and goes

    This points to a connection limit or an overloaded database server, often during heavy traffic or bot activity. Your host can see how many connections the account is using.

  3. The site has been hacked

    WordPress’s documentation lists a hacked site as a possible cause once wp-config.php and the hosting have been ruled out. Scan the site with a tool such as Sucuri SiteCheck.

When to get help

Stop if the database details are correct and the host says the server is running, if the repair page reports tables it could not fix, or if phpMyAdmin shows an empty database after a migration. Get help quickly if this is a shop, because no orders can be placed while the database is unreachable.

What a developer would check

  • A test connection to the database with the exact details from wp-config.php, outside WordPress.
  • The database server’s error log and the account’s connection and size limits.
  • Table status and repair in phpMyAdmin or on the command line.
  • Whether the site’s files have been changed, if the error appeared with no change to hosting or passwords.

Describe the problem in your own words and WebFixed gives you an instant fixed quote. Nothing is charged until you accept it.

Related questions

Have I lost my website content?
Usually not. The message means WordPress cannot reach the database, not that the database has gone. Once the connection details or the server are put right, posts, pages and orders reappear. If tables are damaged, take a backup before repairing them.
Why does the dashboard show a different message from the home page?
WordPress shows visitors only the short heading. On the dashboard address it shows the full explanation, including whether it could reach the server, open the database, or read the tables, which tells you where to look.
Can I leave WP_ALLOW_REPAIR switched on?
No. The repair page needs no login while it is on, so anyone could run it. WordPress says to enable it only when needed and to remove it once the problem is solved.

Written by Michael Broadbridge, Founder of Webgro. First published 14 November 2026. Last checked 14 November 2026 on WordPress 7.1.