Offer Free .in, .co.in or .com domain for year one with web or WordPress hosting on a 12-month+ term, plus free migration. See the offer
WordPress

How to fix "Error establishing a database connection"

This message means WordPress couldn't log in to its MySQL database, so it can't build a single page. Your posts, products and orders are almost always still there. Work through the checks below in order; most sites are back before Step 3.

  • Updated
  • 8 min read
  • Level Intermediate

Before you start

  • Glance at the status page. If every site on your account shows the error at once, the database service may be the problem, and it will be listed there.
  • Your cPanel login. Everything below uses File Manager, Manage My Databases and phpMyAdmin.
  • Before editing wp-config.php, make a copy of it in File Manager so you can undo any change.
Hands-on time10–20 minutes
Your contentAlmost always intact

What caused it? Start with what changed

The error has a handful of causes, and what happened just before it appeared usually points to the right one:

When it startedLikely causeGo to
Right after moving or restoring the siteWrong values in wp-config.php, often the hostStep 1
After changing a password in cPanelThe database user's password no longer matchesStep 2
wp-admin says tables are unavailableCrashed or damaged tablesStep 4
Comes and goes at busy timesThe account is hitting its resource limitsStep 5

Step 1: Check the database details in wp-config.php

Open cPanelFilesFile Manager, go to the site's folder (public_html, or the addon domain's own folder) and edit wp-config.php. Find these four lines:

define( 'DB_NAME', 'yourus3r_wp123' );
define( 'DB_USER', 'yourus3r_wp123' );
define( 'DB_PASSWORD', 'the-database-password' );
define( 'DB_HOST', 'localhost' );
  • DB_NAME and DB_USER on cPanel always start with your cPanel username and an underscore. A name without that prefix came from another host and doesn't exist here.
  • DB_HOST is localhost on our shared, WordPress and reseller hosting. A site moved from elsewhere may still say something like mysql.oldhost.com or an IP address; change it.
  • Quotes and spaces: each value sits between straight single quotes, with no space inside them. Pasting from a word processor can turn them into curly quotes, which PHP doesn't accept.

Save, reload the site in a private window, and if the error remains, keep the file open: you'll need it in the next step.

Step 2: Check the database user in cPanel

Go to cPanelDatabasesManage My Databases (called MySQL Databases in older cPanel). Under Current Databases, find the database named in DB_NAME and look at the Privileged Users column.

cPanel›Databases›Manage My Databases
Current Databases2 databases
DatabaseSizePrivileged Users
yourus3r_wp12348.2 MByourus3r_wp123
yourus3r_shop112.7 MBNone
Example. The second database has no user attached, so WordPress can't log in to it.
  1. If the user isn't listed, attach it

    Scroll to Add User To Database, pick the user and the database, click Add, tick ALL PRIVILEGES and click Make Changes.

  2. Reset the user's password

    Under Current Users, click Change Password next to the user in DB_USER, generate a strong password and copy it.

  3. Paste the same password into wp-config.php

    Replace the DB_PASSWORD value, save, and reload the site. This one change fixes the error more often than anything else.

Step 3: Test the connection directly

Still failing? A four-line test file shows MySQL's own error message instead of WordPress's generic one. In File Manager, create dbtest.php in the same folder as wp-config.php, paste this with your own values, and open https://yourdomain.in/dbtest.php:

<?php
mysqli_report(MYSQLI_REPORT_OFF);
$db = @new mysqli('localhost', 'yourus3r_wp123', 'the-database-password', 'yourus3r_wp123');
echo $db->connect_errno ? 'Failed: ' . $db->connect_error : 'Connected';
The page saysMeaningFix
Access denied for userWrong user or password, or the user isn't attachedStep 2
Unknown databaseThe name is wrong, or the database was never importedStep 1 or Step 6
Too many connectionsThe account's connection limit was reachedStep 5
Connection refusedMySQL isn't answeringCheck status, then open a ticket
ConnectedThe login works; the problem is inside the databaseStep 4

Delete dbtest.php as soon as you've read it. It contains your database password, and anyone who guesses the address could run it.

Step 4: Repair damaged tables

If the login works, or wp-admin says "One or more database tables are unavailable", a table was damaged, often by a full disk or an interrupted update. Two ways to repair it:

A. phpMyAdmin

Open DatabasesphpMyAdmin, click the database on the left, tick Check all below the table list, and choose Repair table from the drop-down.

B. WordPress's own repair page

Add the line below to wp-config.php above That's all, stop editing!, open /wp-admin/maint/repair.php and click Repair Database.

define( 'WP_ALLOW_REPAIR', true );    // remove this line when you're done

The repair page works without logging in, so remove the line straight after. If a table won't repair, restore the database as in Step 6.

Step 5: Check whether the account hit its limits

An error that appears at busy times and clears by itself means the site is running out of room, not that anything is broken. Open cPanelMetricsResource Usage: if it reports the account reached its limits for entry processes, memory or CPU at the times of the error, that's the cause. The usual fixes, cheapest first:

  • Turn on LiteSpeed Cache. Cached pages don't touch the database at all; set it up in five minutes.
  • Stop bots hammering logins. Floods on wp-login.php and xmlrpc.php each open a database connection. Block XML-RPC if no app uses it.
  • Prune plugins that run heavy queries on every page, such as statistics and related-post plugins.
  • Upgrade if traffic has simply grown. WP Pro adds a Redis object cache, which cuts database queries sharply. Compare WordPress hosting plans.

Step 6: Restore the database from a backup

If the database is missing, empty or won't repair, roll it back. Your account is backed up automatically, and WordPress plans keep 14 days (WP Starter) or 30 days (WP Pro and WP Business). In cPanelJetBackup 5Database Backups, pick a date from before the problem, tick the database and click Restore. Only that database is replaced; files and email stay as they are.

First export the current database from phpMyAdmin (Export, Quick, Go), so nothing written since the backup is lost for good. Restore files, databases or email from a backup has the full walkthrough.

Database connection troubleshooting

WordPress shows the install screen instead

The connection works, but WordPress can't find its tables. Compare $table_prefix in wp-config.php with the table names in phpMyAdmin: if the tables start with wpxy_ but the file says wp_, change the file. Don't run the installer, which would start an empty site.

It broke after I changed my cPanel password

Some manual installs use the main cPanel username as DB_USER, and changing the cPanel password can change that MySQL password too. Create a dedicated database user, attach it as in Step 2, and put it in wp-config.php.

Only the staging copy shows the error

A staging site has its own database and its own wp-config.php. Check them the same way, or delete the staging copy and create a fresh one from the live site.

The error came back after a PHP version change

The new PHP version may be missing the mysqli extension. In SoftwareSelect PHP Version, make sure mysqli and pdo_mysql are ticked; see Change PHP version and extensions.

Was this guide helpful?Your answer stays in this browser; nothing is sent unless you write to us below.

Related guides

All 21 guides

Site still down? Ask an engineer.

Tickets and email are answered 24/7 in English and Hindi. Tell us the domain, when the error started and what changed before it. We'll never ask for your passwords.