Troubleshooting

Fix "Error establishing a database connection" in WordPress

Fix the WordPress database connection error: check wp-config.php credentials, the cPanel database user and privileges, repair tables and rule out server limits.

BeginnercPanel hosting3 min read

WordPress shows "Error establishing a database connection" when it cannot log in to MySQL. Your content is almost always still safe in the database; WordPress just cannot reach it. Nine times out of ten the cause is a wrong name, user or password in wp-config.php.

Before you start

  • Access to cPanel and File Manager.
  • Think about what changed: a migration, a restored backup, a changed database password or a new cPanel username.

Check the credentials in wp-config.php

  1. Open cPanel → Files → File Manager and edit wp-config.php in the site folder.
  2. Note the values of DB_NAME, DB_USER, DB_PASSWORD and DB_HOST.
  3. Make sure DB_HOST is localhost.

Compare with cPanel

  1. Open cPanel → Databases → Manage My Databases.
  2. Under Current Databases confirm the database exists with exactly the name in DB_NAME, including the cpaneluser_ prefix.
  3. Under Current Users confirm the user exists, and that the database's Privileged Users column lists it.
  4. If the user is not listed, use Add User To Database and grant ALL PRIVILEGES.

Reset the database password

If you are not sure the password is right, set a new one: in Current Users click Change Password next to the user, generate a strong password, save it, and paste the same value into DB_PASSWORD. Watch out for quote characters in the password; if it contains a single quote, either regenerate it or escape it.

Test the login with phpMyAdmin

Open cPanel → Databases → phpMyAdmin. If you see the database with its tables (such as wp_options), the data is there. If the database is empty, the import failed during a migration and must be repeated or restored from cPanel → JetBackup → Database Backups.

Check the table prefix

If credentials are right but WordPress starts its installer instead, $table_prefix in wp-config.php does not match the table names. Look at the tables in phpMyAdmin: if they start with wpab_, the prefix must be 'wpab_'.

Repair corrupted tables

If the dashboard says "One or more database tables are unavailable", add this line to wp-config.php:

define( 'WP_ALLOW_REPAIR', true );

Visit https://yourdomain.com/wp-admin/maint/repair.php, click Repair Database, then remove the line again, because the repair page needs no login. You can also select tables in phpMyAdmin and choose Repair table.

Intermittent errors

If the error appears only at busy times, the account may be hitting resource limits or a plugin may open too many connections. Check cPanel → Metrics → Resource Usage, enable LiteSpeed Cache so fewer requests reach the database, and contact support if MySQL itself seems unavailable for all your sites.

Frequently asked questions

Did I lose my posts?

Very unlikely. The error happens before WordPress reads any content. Once the connection works again, everything reappears.

Can I use 127.0.0.1 instead of localhost?

Use localhost on MdmHost. It connects through the local socket, which is what cPanel database users are set up for.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.