WNWEBSNEXUS

PHP & WordPress

“Error Establishing a Database Connection” in WordPress: How to Fix It

“Error establishing a database connection” means WordPress started but couldn’t connect to the database where every post, page, user and setting is stored, so it can’t build any page. Your data is almost always still there. The cause is usually wrong login details, a database server that is down or overloaded, or damaged tables. Work through the checks below in order.

Related service: Website maintenance & support

By WebsNexus · November 1, 2026 · 4 min read · Updated November 1, 2026

Part of Fix-it library

Causes, how to confirm each one, and the fix

CauseHow to confirm itFix
Wrong database detailsSettings in wp-config.php don’t match the hosting panelCorrect DB_NAME, DB_USER, DB_PASSWORD or DB_HOST
Database server downEvery site on the server fails; the test script can’t connectRestart MySQL/MariaDB or contact your host
Too many connectionsErrors come and go with traffic; MySQL logs “Too many connections”Find slow queries and bots; raise limits or upgrade hosting
Corrupted tablesThe front end fails but wp-admin asks for a repair, or only some pages failRepair tables with WP_ALLOW_REPAIR or wp db repair
Moved to a new hostThe error started right after a migrationUpdate the database details and DB_HOST for the new server

Is the error on the whole site or only the front end?

Open both your homepage and /wp-admin. If both show the same error, WordPress can’t reach the database at all, so check the connection details and the database server. If wp-admin shows a different message, saying one or more database tables are unavailable and may need to be repaired, the connection works and the tables are damaged. Skip to the repair step.

How do you check the database settings in wp-config.php?

Open wp-config.php in your site’s root folder and find the four database settings. Compare each one with the database and user listed in your hosting panel, for example MySQL Databases in cPanel. A single wrong character in the password is enough to cause the error. Passwords are case-sensitive.

DB_HOST is usually localhost, but some hosts use a separate database server with its own address and sometimes a port. Your host’s documentation or welcome email lists the right value. If you changed the database user’s password in the panel, update wp-config.php to match.

wp-config.php: the four settings to check
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );

How do you test the database connection directly?

Upload a small PHP file with a random name to your site’s root, fill in the same four values as wp-config.php, and open it in a browser. If it prints “Connected”, the details are right and the problem is in WordPress, usually damaged tables. If it shows an error, the message tells you whether the user, password or host is wrong, or whether the server can’t be reached.

Delete the file as soon as you have the answer: it contains your database password.

db-test-7f2c.php (delete it straight after use)
<?php
$db = mysqli_connect( 'localhost', 'database_user', 'database_password', 'database_name' );
echo $db ? 'Connected' : 'Error: ' . mysqli_connect_error();

Is the database server down?

If the details are right but nothing can connect, the MySQL or MariaDB server is probably stopped or overloaded. On shared hosting you can’t restart it yourself: contact your host and tell them the exact error and the time it started. On a VPS, check and restart the service, and look at its log for the reason it stopped, often a full disk or the server running out of memory.

sudo systemctl status mysql      # or: mariadb
sudo systemctl restart mysql
df -h                            # a full disk stops MySQL

How do you repair the WordPress database?

WordPress has a built-in repair tool. Add the line below to wp-config.php, then open /wp-admin/maint/repair.php on your site and choose Repair Database. With SSH, wp db repair does the same using the details in wp-config.php.

Remove the line as soon as the repair has finished. The repair page doesn’t require a login, so anyone who finds it can run it while the setting is on.

wp-config.php (remove after use)
define( 'WP_ALLOW_REPAIR', true );
wp db check
wp db repair

What if the error comes and goes with traffic?

An error that appears at busy times and then clears usually means the database is refusing new connections because it has reached its limit, or the server is running out of memory. MySQL reports this as “Too many connections”. Common triggers are traffic spikes, aggressive bots and slow plugin queries holding connections open.

Block abusive bots, add page caching so fewer requests reach the database, and look for slow queries. If the site has simply outgrown its plan, move to hosting with more resources.

Did the error start after moving to a new host?

After a migration, wp-config.php often still holds the old host’s database details, or the database was never imported. Create the database and user on the new host, import the database export, then update all four settings in wp-config.php. If the domain also changed, update the site URL in the database before testing.

When should you restore a backup?

Restore a database backup if tables can’t be repaired or data is missing. Restore the most recent backup from before the problem, ideally on a staging copy first. Then find the cause, such as a full disk or a failed update, so the error doesn’t return.

Get a free estimate in 24 hours

Free, no obligation. We reply within 24 hours.

Your detailsStep 1 of 2

We can sign an NDA before you share customer data.

Frequently asked questions

Have I lost my website’s data?

Almost never. The error means WordPress can’t reach the database, not that the database is gone. Once the connection works, your content comes back.

Why does the error appear only sometimes?

Intermittent errors usually mean the database server is overloaded or has reached its connection limit at busy times. Caching, blocking bad bots and fixing slow queries help; so does more powerful hosting.

What should DB_HOST be?

Usually localhost. Some hosts run the database on a separate server and give you its address, sometimes with a port. Check your host’s documentation.

Is it safe to leave WP_ALLOW_REPAIR on?

No. The repair page doesn’t need a login, so remove the setting from wp-config.php as soon as the repair is done.

Can a hack cause this error?

It can, for example if an attacker changed wp-config.php or the database password. If the details were changed and you didn’t do it, treat the site as compromised and have it checked.

Why did it start after I changed my hosting password?

Some panels change the database user’s password too, or you may have changed it yourself. Update DB_PASSWORD in wp-config.php to the new value.

Sources

Related articles

What now

Need a site that performs globally?

Tell us what you are building and we’ll come back with a clear scope and fixed price.