WordPress
How to fix the WordPress error establishing a database connection

Short answer
WordPress shows “Error establishing a database connection” when your site’s files can’t reach the database — usually from wrong wp-config.php credentials, an unresponsive database server, or corrupted tables. Worked through in order, it’s normally fixed in 15 to 20 minutes without losing any data.
It looks alarming, but in most cases your database itself is fine; the problem almost always sits in the connection details or server access. The first move isn’t deleting files — it’s checking wp-config.php and your database status in the hosting panel, in that order.
Why WordPress shows the error establishing a database connection
The message itself is always the same (a single line on a blank screen), but the cause can come from a few different layers. The most common triggers are:
- A database password changed on the hosting side that was never updated in wp-config.php
- A site migration where DB_HOST keeps pointing at the old server
- The database server (MySQL/MariaDB) failing to respond because of heavy load, a resource cap, or a brief outage
- Database tables corrupted by an abrupt shutdown, a bad plugin query, or a disk issue
- A database user whose privileges on that specific database were accidentally removed in the hosting panel
What they share is that the problem sits in the bridge between WordPress and the database, not in WordPress itself. That’s why the fix should always follow the same order: first the credentials, then whether the server is even up, and only then the database’s own integrity.
Notice where the error actually shows up
Before you start fixing the error establishing a database connection, check exactly where it appears — it saves time. If the error only shows on the front end but you can still log into /wp-admin/, the cause is usually narrower, like a caching plugin or one specific query. If it shows on both the site and the admin dashboard, the connection problem sits at a more basic level, and you’ll need to work through every step below in order.
This distinction matters because in the second case WordPress never reaches the database at all, so normal debugging tools like WP_DEBUG can’t kick in. That means the checks below have to be done manually, through FTP or your host’s file manager.
Check the database credentials in wp-config.php
The most common and effective fix is comparing the connection details in wp-config.php against the real database details in your hosting panel. Open wp-config.php in your site’s root directory with FTP or your host’s file manager and find these four lines:
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
In your hosting panel’s databases section (cPanel, hPanel or similar), confirm the database name and username. If you don’t remember the password, set a new one from the panel and update the same value in wp-config.php. A single stray character — an extra space, a smart quote — is enough to trigger this error, so copying and pasting the values is safer than retyping them by hand.
Confirm the database user’s privileges
If the credentials look correct and the error persists, the user’s privileges are next. On some hosting panels, creating a database user doesn’t automatically attach it to a specific database — those are two separate steps. In your panel’s “Database Users” or “MySQL Users” section, confirm the user is added to the right database with “All Privileges.”
This is a step that’s easy to miss after a server migration or a manually recreated database: the user exists, the database exists, but the privilege link between them never got made.
Check DB_HOST and whether the database server is actually up
DB_HOST is “localhost” on most shared hosting, but not always — some providers, especially managed WordPress hosts or setups with a separate database server, use an IP like 127.0.0.1 or a custom hostname like db.provider.com. You can usually find the correct DB_HOST value in your hosting provider’s setup email or the database section of your panel.
If DB_HOST is correct but the error continues, check whether the database server itself is running. Your hosting panel’s status page should confirm MySQL/MariaDB is up; on shared hosting, sudden traffic spikes or a resource cap can make the database server temporarily unresponsive. If that’s the case, there’s nothing to fix on the WordPress side — the issue sits in the hosting infrastructure.
Run WordPress’s built-in database repair tool
If the credentials and server status both check out but the error persists, the database tables themselves may be the problem. WordPress ships a built-in repair tool for this. First, temporarily add this line to wp-config.php:
define( 'WP_ALLOW_REPAIR', true );
Then visit yoursite.com/wp-admin/maint/repair.php in your browser; WordPress offers “Repair Database” or “Repair and Optimize Database.” The second option takes a bit longer but also optimizes the tables. Once you’re done, remove the line you added to wp-config.php — left in place, the tool stays accessible to anyone without authentication, which is a real security risk.
Check whether the database tables are actually corrupted
If the repair tool reports a table it can’t fix, you’ll need to check manually through phpMyAdmin in your hosting panel. Open your database, select all tables, and run “Repair Table.” Some table engines, InnoDB in particular, can behave differently here; if that happens, restoring your last known-good backup is safer than trying to fix a table row by row.
A mismatched table prefix
A less obvious but real cause is the table prefix. If you changed WordPress’s default prefix (wp_) during setup, or the site was migrated from a different install, check that the $table_prefix value in wp-config.php matches the actual table names in the database (for example ab12_options instead of wp_options). A mismatched prefix can produce the same error establishing a database connection even when the connection itself succeeds, simply because WordPress can’t find the right tables.
If nothing works: restoring from backup and contacting your host
If you’ve worked through every step above and the error establishing a database connection is still there, the cause is most likely server-side: MySQL crashed, a hardware failure, or a server configuration issue. At that point, the safest move is to clearly share what you’ve checked and the values in wp-config.php (without the password) with your hosting provider’s technical support — their server-side access can surface details you can’t see from the panel.
If support can’t resolve it, or the database is too corrupted to recover, restoring from your most recent backup is the last resort. That’s why a regular, automated backup stored off-site is the one real safety net against this error’s worst-case scenario.
Avoiding the error establishing a database connection next time
Most of this is preventable. Make it a habit to update wp-config.php the moment you change the database password in your hosting panel; add DB_HOST and the table prefix to your checklist during any site migration; keep an eye on your hosting plan’s resource quota, especially as traffic grows; and keep a regular off-site backup. Our guide to the WordPress white screen of death follows a similar discipline for a related problem — both errors trace back to the same three sources: connection or access, server resources, and file integrity.
For an up-to-date, official reference on the DB_NAME, DB_USER, DB_PASSWORD and DB_HOST constants, see WordPress’s own wp-config.php documentation. If you’re dealing with a different WordPress issue around outbound email, our guide to WordPress not sending email covers that separately.
Next step
The error establishing a database connection looks alarming, but it’s usually resolved by verifying a few lines in wp-config.php, with little risk of data loss if you work through it carefully. If the problem keeps recurring on the server side, or your setup has outgrown its current hosting, our group company Web Tasarım Ofisi supports WordPress builds, database maintenance and technical troubleshooting.
To talk through your site’s technical setup or a custom software need, take a look at our custom software service or get in touch with Argo Ajans.
Frequently asked questions
What does "Error establishing a database connection" mean in WordPress?
It means your site's files cannot reach your database. It's usually caused by incorrect credentials or host values in wp-config.php, an unresponsive database server, or corrupted database tables.
Does this error cause data loss?
In most cases, no. The problem is almost always about connection or access, not the data itself. Still, take a current backup before running the repair tool or editing any files, so a wrong move is always reversible.
Which values in wp-config.php should I check?
Confirm DB_NAME, DB_USER, DB_PASSWORD and DB_HOST match the real database details in your hosting panel exactly; DB_HOST in particular is often not "localhost."
How do I run WordPress's database repair tool?
Temporarily add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, then visit yoursite.com/wp-admin/maint/repair.php. Remove that line again once you're done, or the tool stays publicly accessible.
When should I contact my hosting provider?
If your wp-config.php values are correct, the repair tool doesn't fix it, and the table prefix matches, the cause is most likely server-side (MySQL down, a resource cap, or a network issue) — ask your host to check the database server's status.