Skip to content
Technology for businesses since 2008

Help center · WordPress

Error establishing a database connection in WordPress: how to fix it

The “Error establishing a database connection” message appears when WordPress cannot communicate with MySQL. The most common causes are incorrect details in the wp-config.php file (database name, user, password, or server), a user without permission on the database, damaged tables, or an unavailable database server. Diagnosis starts by checking wp-config.php and cPanel.

This article is for anyone with a WordPress site on cPanel hosting who needs to get it back online. It shows how to identify the cause, how to check each connection detail, how to repair tables, and when to contact support.

Before you begin

  • You need access to your hosting cPanel. The WordPress dashboard won’t open while this error is showing.
  • Before editing any file, download a copy of it. Before working with the database, make a backup using phpMyAdmin (Export tab).
  • Change one thing at a time and check your site after each change. That way, you’ll know what fixed the problem.

What does the database connection error mean?

WordPress stores posts, pages, users, and settings in a MySQL (or MariaDB) database. On every visit, it opens a connection using the details in wp-config.php. If the connection fails, it can’t build the page and shows this message.

The error doesn’t say what the cause is, so the best approach is to test the causes from the simplest to the rarest.

What are the most common causes?

Cause Typical sign
Incorrect details in wp-config.php The error appeared after you migrated the site, changed the database password, or edited the file
User without privileges on the database The user exists but isn’t linked to the database, or has lost access
Damaged tables The error appears only in /wp-admin, or the site opens and fails intermittently
MySQL server unavailable or overloaded Other sites on the same account also fail, or the error comes and goes
Disk space or resource limit reached The error appeared together with other problems on the account
Hacked site or altered files Changes to wp-config.php that you didn’t make

Step 1: check the details in wp-config.php

  1. In cPanel, open File Manager and go to your site’s folder (for a main domain, this is usually public_html).
  2. Find wp-config.php, download a copy, and open it with Edit.
  3. Note the four values:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'username' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );

On most cPanel hosting, the DB_HOST value is localhost. If you migrated your site from another host, this is one of the first things to check: the old server may have used a different address. Use the value your hosting provider gives you.

In cPanel, database and user names usually start with a prefix based on your account name, such as account_databasename. Make sure wp-config.php uses the full name, prefix included.

Step 2: compare with what exists in cPanel

  1. In cPanel, open MySQL Databases.
  2. Under Current Databases, confirm that the database named in DB_NAME exists and is spelled exactly the same.
  3. Under Current Users, confirm that the user named in DB_USER exists.
  4. On the same screen, under Current Databases, check the users column: the user must be linked to the database.

If the database exists but the user isn’t linked to it, go to step 3. If the database doesn’t exist, it was either deleted or never created: restore it from a backup (step 6).

A user can only use a database after being added to it.

  1. In MySQL Databases, in the Add User To Database section, choose the user and the database.
  2. Click Add.
  3. On the privileges screen, check ALL PRIVILEGES and save.
  4. Open your site again.

Step 4: reset the database user’s password

If the names are correct, the password may be the difference. The safest approach is to set a new password in both places at the same time.

  1. In cPanel, under MySQL Databases, go to Current Users and use the option to change the user’s password. Generate a strong password and copy it.
  2. Edit wp-config.php and enter the new password in DB_PASSWORD, between single quotes.
  3. Save the file and open your site.

If the password contains single quotes or backslashes, PHP may read it incorrectly inside wp-config.php. To avoid problems, use a long password made of letters and numbers only, without those two characters. Keep in mind that the same user may serve another site on the account. If it does, update that site’s wp-config.php as well.

Step 5: check that the database opens in phpMyAdmin

This test shows whether the database is running and whether its tables are there.

  1. In cPanel, open phpMyAdmin.
  2. Click your site’s database in the menu on the left.
  3. Check for tables such as wp_options and wp_posts. The wp_ prefix may be different, and the same prefix appears in the $table_prefix line of wp-config.php.

If the database is empty, the site was migrated without importing the data, or the wrong database is being used. If the table prefix is different from $table_prefix, correct the value in wp-config.php so the two match.

If phpMyAdmin also won’t open or shows a connection error, the problem is with the MySQL server and not your site: go to step 7.

Step 6: repair damaged tables

Damaged tables can take down the whole site or only the dashboard. WordPress has a built-in repair tool.

  1. Back up the database using phpMyAdmin.
  2. Edit wp-config.php and add this line above the /* That's all, stop editing! */ comment:
define( 'WP_ALLOW_REPAIR', true );
  1. Open yourdomain.com/wp-admin/maint/repair.php in your browser.
  2. Click Repair Database (or Repair and Optimize Database) and wait for it to finish.
  3. Remove the WP_ALLOW_REPAIR line from wp-config.php when you’re done. This page doesn’t ask for a login while the line is active, so anyone could open it.

Another option is phpMyAdmin: select the tables, open the menu below the list, and choose Repair table. If the database was lost or is badly damaged, restore a recent copy from a backup by importing the .sql file on the Import tab.

Step 7: rule out server problems

If nothing above solved it, the problem may be outside your site.

  • Other sites on the same account also fail: the MySQL server may be unavailable or overloaded.
  • The error comes and goes: this usually points to traffic spikes or a plugin that runs heavy database queries.
  • The account has reached its disk space or resource limit: the database may stop writing or accepting connections. Check disk usage in cPanel.

In these cases, contact your hosting support and share the time of the error, the domain, and what you’ve already tested. They can see the state of the server, which you can’t see from cPanel.

After the site is back

  1. If you used the repair tool, confirm that you removed WP_ALLOW_REPAIR from wp-config.php.
  2. Make a complete copy of the files and the database now that the site works.
  3. If the error appeared after a hosting change, review the guide on how to migrate a WordPress site to MigraCloud.
  4. If wp-config.php was changed without your knowledge, check whether the site was hacked: see how to use AI to remove malware from a WordPress site.
  5. If the site opens but shows a 500 error or a white screen, follow 500 error or white screen in WordPress.

To reduce the risk of new problems, keep everything up to date by following the guide on how to update WordPress, themes, and plugins safely. If you’d rather not work with the database on your own, contact MigraCloud.

Frequently asked questions

What does “Error establishing a database connection” mean?

It means WordPress couldn’t connect to MySQL. This happens because of incorrect details in wp-config.php, a user without permission on the database, damaged tables, or a database server that is down. The message doesn’t tell you which one is the cause.

How do I fix this error in WordPress?

Check the database name, user, password, and server in wp-config.php. Then compare them with what’s listed under MySQL Databases in cPanel, and confirm that the user is linked to the database with all privileges. If everything is correct, repair the tables.

The error appeared after I migrated my site. What should I check?

Check that the database was imported, that the database and user names in wp-config.php are the ones from your new hosting (with the account prefix), and that the user is linked to the database. The DB_HOST value can also change from one host to another.

How do I know if the database is damaged?

Open the database in phpMyAdmin. If the tables exist but the site fails intermittently, the error appears only in the dashboard, or phpMyAdmin reports tables in use or corrupted, it’s worth trying the repair. Make a backup first.

Is it safe to use the WordPress repair tool?

Yes, as long as you back up the database first and remove the WP_ALLOW_REPAIR line from wp-config.php afterward. While that line is active, the repair page works without a login and is open to anyone who knows the address.

Will changing the database password in cPanel take my site down?

It will, if you don’t update the same password in wp-config.php. The two must match. So change the password in cPanel and then immediately update the DB_PASSWORD field. If the same user serves other sites, update the file on each one.

Can I lose my posts because of this error?

Usually not. The error blocks access to the database, but it doesn’t delete anything. Data is only lost if the database is removed or overwritten. Even so, don’t fix anything without a backup, and keep a copy of wp-config.php and the database.

When should I contact hosting support?

Contact them if phpMyAdmin won’t open either, if several sites on the account fail at the same time, if the error comes and goes without any change from you, or if the database no longer exists. Support can check the server and your account limits, which you can’t see from the control panel.

More on WordPress

See all →

Help center

Looking for something else?

See all tutorials, organized by topic.