WordPress

Troubleshooting the "Error Establishing a Database Connection" in WordPress

By the Domain India teamPublished 6 min read
Knowledge base article
Contents (6 sections)

"Error establishing a database connection" means WordPress could not log in to its MySQL database, so it has nothing to show. On Domain India shared hosting the cause is almost always one of four lines in wp-config.php, or a database user that was never linked to the database. This page walks through the WordPress fix in the order that finds the problem fastest.

For every other database error

This page covers the WordPress message only. For "Access denied", "max_user_connections", crashed tables, import errors and collation problems on any website, see the full guide: Database troubleshooting.

Key takeaways

Open wp-config.php in your site's main folder and check four values: DB_NAME and DB_USER with your account prefix (for example acme_wp), DB_PASSWORD as the database user's password (not your panel password), and DB_HOST as localhost. Then make sure the user is added to the database with all privileges. If the details work in phpMyAdmin but the site still fails, repair the database; if several sites fail at once, open a support ticket.

1. Check the four settings in wp-config.php

Open File Manager in your control panel, go to the folder where WordPress is installed (usually public_html), and edit wp-config.php. Near the top you will find:

php
define( 'DB_NAME', 'acme_wp' );
define( 'DB_USER', 'acme_wpuser' );
define( 'DB_PASSWORD', 'the-database-user-password' );
define( 'DB_HOST', 'localhost' );
  1. DB_HOST.
    For a site on the same hosting account as its database, this is localhost. Do not use your domain name, the server IP address or 127.0.0.1.
  2. DB_NAME.
    Copy it exactly from your control panel's database list. It starts with your account username and an underscore, for example acme_wp, not just wp. Names are case-sensitive.
  3. DB_USER.
    The database user also has the prefix, for example acme_wpuser. It is a separate login from your control panel account.
  4. DB_PASSWORD.
    The password you set for that database user. Your cPanel, DirectAdmin or client-area password will not work here. If the password contains a quote, $ or #, check it is quoted correctly, or set a simpler new one.

Did something change recently? If you or a developer reset the database user's password in the panel, WordPress keeps using the old one until you update wp-config.php. After a migration the account prefix usually changes with the new username, so all the names change too.

2. Test the details in phpMyAdmin

Open phpMyAdmin from your control panel and try the same database user and password. If they work there, the details are right and the mistake is in wp-config.php. If they fail there too, reset the database user's password in the panel and put the new one in wp-config.php.

3. Make sure the user is added to the database

Creating a database and a user does not link them. In cPanel, open Manage My Databases, go to Add User To Database, pick the user and the database, and tick ALL PRIVILEGES. In DirectAdmin, open Account Manager › Databases and check the database's user list. In Webuzo, check the Database User(s) tab under Database › Manage Databases. If an installer reports that it could not find the database, see user is not assigned to database.

4. Repair the database if the login works

If the credentials are right but WordPress still fails, or the admin area says tables need repairing, a table may be damaged. Export a backup first (how to back up a MySQL database using phpMyAdmin), then either:

  • in cPanel, open Manage My Databases and use Repair Database; or
  • in phpMyAdmin, tick the damaged table and choose Repair table from the With selected menu.

WordPress also has its own repair page. Add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, open yourdomain.com/wp-admin/maint/repair.php, run the repair, then remove the line straight away, because that page works without a login.

Reinstalling WordPress rarely fixes this error

Replacing core files does not change your database details. Only do it if the files are damaged, and never delete wp-content or wp-config.php. See restoring WordPress core files.

5. When it is not your settings

If the details are correct and several sites on your account show the error at the same moment, the database service itself may have a problem. Open a support ticket with the domain, the exact error and the time it started. Never include your database password. If the error comes and goes under traffic together with "Resource Limit Is Reached", see checking your hosting resource usage.

Support is on 24/7 live chat, and tickets get a first response within 15 minutes; how long a fix takes depends on the problem. We do not offer phone support.

Frequently asked questions

What should DB_HOST be in wp-config.php on Domain India shared hosting?

Use localhost. WordPress and its database run on the same hosting account, so you do not need the server IP address or your domain name as the database host.

Why does my WordPress database name have my username in front of it?

cPanel and DirectAdmin add your account username and an underscore to every database and database user name, for example acme_wp. DB_NAME and DB_USER in wp-config.php must use the full name including the prefix.

Can I use my cPanel password as DB_PASSWORD?

No. DB_PASSWORD is the password of the database user, which is separate from your control panel and client-area passwords. If you don't know it, reset the database user's password in the panel and put the new one in wp-config.php.

How do I repair a WordPress database?

Back up the database first. Then use Repair Database in cPanel's Manage My Databases, or Repair table in phpMyAdmin. WordPress also has a repair page at wp-admin/maint/repair.php once you add WP_ALLOW_REPAIR to wp-config.php; remove that line as soon as you finish.

The details are correct but the error is still there. What next?

Check that the database user is added to the database with all privileges. If it is and several sites fail at once, open a support ticket with the exact error and the time it started, without your password.

Ready to fix it? Work through the full database troubleshooting guide for other errors, check your settings with how to connect to the MySQL database, or open a support ticket if your site still won't connect.

WordPress still can't reach its database?

Send us the domain, the exact error and when it started, and we will check it with you. Never include your database password.

Open a support ticket

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app