Help

How to fix “Error establishing a database connection” in WordPress

WordPress shows “Error establishing a database connection” when it cannot connect to its database. The cause is usually a wrong database name, user, password or host in wp-config.php. It can also be a user without access to the database, damaged tables, or a database server that is down or overloaded.

Updated · HostUpIndia team

What causes the error

WordPress reads its database details from wp-config.php on every page load. If one is wrong, or the database cannot answer, the site stops with this message.

  • A wrong value in wp-config.php: DB_NAME, DB_USER, DB_PASSWORD or DB_HOST.
  • A database user that is not added to the database, or has no privileges on it.
  • Damaged (corrupted) database tables.
  • A database server that is down or overloaded.
  • A site moved to new hosting while wp-config.php still has the old details.

Check wp-config.php in cPanel

Most cases end here. Compare the four values with what cPanel lists. What each line does: wp-config.php explained.

  • Sign in to cPanel and open File Manager.
  • Open the folder WordPress is in, usually public_html.
  • Download a copy of wp-config.php, so you can undo a change.
  • Edit wp-config.php and note DB_NAME, DB_USER, DB_PASSWORD and DB_HOST.
  • Open cPanel › Manage My Databases (MySQL Databases in older cPanel) and check that the database and user names match exactly, including the prefix cPanel adds, such as username_.
  • Set DB_HOST to localhost, the usual value on cPanel, unless your host gives you another.
  • Save the file and reload your site.

Check the database user

If the names match, reset the user’s password and its access to the database.

  • In Manage My Databases, under Add User To Database, choose the user and the database, then click Add.
  • Tick All Privileges and click Make Changes.
  • Under Current Users, click Change Password next to the user and set a new password. Leave out ' and \, which can break wp-config.php.
  • Paste the new password into DB_PASSWORD in wp-config.php and save.
  • Open phpMyAdmin in cPanel and click the database. If its tables are listed, such as wp_options, your data is there. If the list is empty, restore a backup.

Repair the WordPress database

Damaged tables cause the same error on the site. The admin area often says more.

  • Back up the database first: open phpMyAdmin, choose the database, click Export and save the file.
  • Open /wp-admin on your site. If it says “One or more database tables are unavailable”, the tables need a repair.
  • Add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, above the “stop editing” comment.
  • Go to /wp-admin/maint/repair.php and click Repair Database.
  • Remove the line from wp-config.php when it finishes. The repair page needs no sign-in while it is there.
  • Or repair it in cPanel: in Manage My Databases, under Modify Databases, choose the database and click Repair Database.

Restore a backup or contact your host

If the values are right and a repair does not help, the database itself or the server is the problem.

  • Export the current database in phpMyAdmin, so you can undo the restore.
  • Restore the database from a backup made before the error started. Changes made since that backup are lost.
  • If you moved the site, import its database at the new host and put the new details in wp-config.php.
  • If the error comes and goes, or other sites on the account show it too, the database server may be down or overloaded. Open a support ticket with your host.
  • If the site shows “There has been a critical error on this website” instead, see how to fix a WordPress critical error.

WordPress hosting with 30-day backups

HostUpIndia WordPress hosting keeps daily backups for 30 days, so you can restore a database from before the error. WordPress comes pre-installed on LiteSpeed, with NVMe storage, cPanel and free SSL.

From ₹149 a month on 3-year billing. Prices exclude 18% GST. See WordPress hosting plans.