Website errors

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

WordPress shows this message when it cannot log in to its MySQL database. In most cases a value in wp-config.php is wrong or the database user lost its privileges; less often the database server is down or a table needs repair.

Updated 3 min readApplies to: cPanel

What is happening

Every WordPress page is built from data in a MySQL (or MariaDB) database. When WordPress cannot log in to that database, it stops and shows "Error establishing a database connection". The usual causes, most common first:

  1. A wrong database name, username, password or host in wp-config.php
  2. The database user is not assigned to the database, or lost its privileges
  3. The database server is down or overloaded
  4. Damaged database tables

If you see "One or more database tables are unavailable" instead, jump to the repair step.

Before you start: Download a copy of wp-config.php before editing it, and keep it private; it contains your database password.

Steps to fix it

  1. Open wp-config.php in the site's root folder (usually public_html) using cPanel File Manager and find these lines:

    define( 'DB_NAME', 'cpuser_wp123' );
    define( 'DB_USER', 'cpuser_wpuser' );
    define( 'DB_PASSWORD', 'your-strong-password' );
    define( 'DB_HOST', 'localhost' );
    
  2. Compare the names with Databases > MySQL Databases in cPanel. The database and user must appear there with exactly the same spelling, including the cpanelusername_ prefix that cPanel adds. After a migration this prefix is the most common mismatch.

  3. Reset the database user's password if you are not sure it is right. In MySQL Databases, find the user under "Current Users", choose Change Password, set a strong password, and paste the same value into DB_PASSWORD. Keep the quotes around it.

  4. Assign the user to the database. In MySQL Databases, under "Add User to Database", select the user and the database, click Add, tick ALL PRIVILEGES and save. If the user is already listed against the database, open it and check the privileges are still ticked.

  5. Check DB_HOST. On cPanel it is normally localhost. Do not change it to your domain name or IP unless your host has told you to.

  6. Test whether the database server answers. Open Databases > phpMyAdmin. If phpMyAdmin also fails, or every database site on your account is down at the same time, the database service itself may be down or overloaded. That needs the host, so go to the ticket section below.

  7. Repair damaged tables. In phpMyAdmin, select your database, tick all tables, and choose Check table, then Repair table for any that report errors. Alternatively, add this line to wp-config.php:

    define( 'WP_ALLOW_REPAIR', true );
    

    Visit https://yourbusiness.in/wp-admin/maint/repair.php, run Repair Database, then remove the line immediately.

  8. Check resource limits if the error comes and goes under traffic. Too many simultaneous connections or a heavy plugin can exhaust what the account allows. The steps in fixing 503 errors apply here too.

How to confirm it worked

  • The home page and /wp-admin/ load without the error.
  • Posts, pages and settings look as they did before.
  • In phpMyAdmin, Check table shows "OK" for all tables.

If you need to create a fresh database and user, for example to restore a backup, follow creating a MySQL database in cPanel.

If it still fails

Open a support ticket and include:

  • The site URL and when the error started (IST)
  • Whether phpMyAdmin opens and whether other sites on the account work
  • The database name and username from wp-config.php (never the password)
  • What changed recently: migration, password change, plugin update, restore
  • Any database related lines from error_log or wp-content/debug.log

If you are choosing a new home for a busy WordPress site, see the resources on our WordPress hosting page.

Frequently asked questions

What should DB_HOST be on cPanel hosting?

Usually localhost, because the database runs on the same server as the website. Only change it if your host has told you to use a different database host.

Why did this start after I migrated my site?

On cPanel, database and user names start with your cPanel username and an underscore. After a move to a new account the prefix changes, so the old names in wp-config.php no longer match.

Is my content lost?

Almost always no. The message means WordPress cannot connect, not that the data is gone. Fix the connection and your posts and pages return.

Is it safe to leave WP_ALLOW_REPAIR on?

No. The repair page does not require a login while the setting is on, so remove the line as soon as the repair finishes.

Sources and further reading

Contact us