UnderHost
Knowledgebase Docs

Error establishing a database connection: fix it

Fix the "Error Establishing a Database Connection" in WordPress and other PHP applications. Covers wrong credentials, corrupt database, and resource limits.

On this page

"Error Establishing a Database Connection" means your application couldn't connect to MySQL. The most common cause after a migration or fresh install is incorrect credentials in the configuration file. After that, check for database corruption and resource limits.

Wrong database credentials

For WordPress, open wp-config.php in File Manager and verify these four values match exactly what's configured in cPanel → MySQL Databases:

wp-config.php
define( 'DB_NAME',     'username_dbname' );   // Full database name with prefix
define( 'DB_USER',     'username_dbuser' );   // Full database username with prefix
define( 'DB_PASSWORD', 'yourpassword' );     // Database user password
define( 'DB_HOST',     'localhost' );         // Always localhost on shared hosting

Common mistakes:

  • Using the database name without the cPanel username prefix (e.g. mydb instead of cpanelusername_mydb)
  • Using the wrong password (changed in cPanel but not updated in wp-config.php)
  • Using a remote MySQL host instead of localhost
  • Database user not assigned to the database with All Privileges
Test credentials in phpMyAdmin

In cPanel → phpMyAdmin, try logging in with the database username and password. If phpMyAdmin login fails, the credentials are wrong-update them in cPanel → MySQL Databases and then update wp-config.php to match.

Corrupted database tables

If credentials are correct but you still get the error, a table may be corrupted. Check phpMyAdmin → select your database → look for any table showing "In use" or an error status. Repair it:

  1. In phpMyAdmin, select all tables in your database
  2. From the "With selected:" dropdown, choose Repair table
  3. Then run Optimize table

Too many connections

If the error is intermittent (happens during high traffic), the server may be hitting MySQL connection limits. Signs:

  • Error appears under load but resolves on its own
  • Site works fine most of the time but crashes during traffic spikes

Solutions: enable Redis object caching (reduces database queries), upgrade your hosting plan for more resources, or move to a VPS where connection limits are higher.

After a migration

The database connection error is extremely common after migrating a site. The old host's database credentials are usually still in the config file. Update wp-config.php with the new database name, username, and password created on the new server (see How to migrate your website).

Related: Databases in cPanel | WordPress 500 error | Fixing a 500 Internal Server Error | Migrate website

Was this article helpful?

Still troubleshooting?

Use UnderHost tools for quick checks, or open a support ticket when the issue needs account or server access.

Related articles

Back to Troubleshooting