How to Fix Error Establishing a Database Connection in WordPress (6 Ways)

How to Fix ‘Error Establishing a Database Connection’ in WordPress
Your site is down and all you see is one line: “Error establishing a database connection.” No login page, no dashboard, nothing. It looks scary, but in most cases the fix is simple — WordPress just cannot log in to its own database.
This guide takes you through the fixes in the right order, from the most common cause to the last resort. Follow them one by one and stop as soon as your site comes back.
What this error actually means
WordPress is two things: the files (themes, plugins, images) and the database (your posts, pages, settings and users). Every time someone visits your site, WordPress uses the login details stored in a file called wp-config.php to connect to the database and build the page. If that connection fails — wrong password, database down, or database damaged — WordPress stops and shows this error instead of your site.
Common causes: Wrong database details in wp-config.php — especially after you changed a password, migrated the site, or moved hosts. Database server is down or overloaded. Corrupted database. Corrupted WordPress files. Wrong database host value — most sites use localhost, but some hosts use a different address.
BACK UP FIRST: Before you change anything, take a backup if you can. In your hosting control panel, export the database from phpMyAdmin (Export > Quick > SQL) and download a copy of wp-config.php to your laptop. If your dashboard is down, ask your host for the latest backup. Never edit wp-config.php live without keeping the original copy.
Fix 1 — Check the database details in wp-config.php
This is the most common cause. Open your hosting control panel or FTP and go to your site’s root folder (usually public_html). Open wp-config.php. Find these four lines:
define( 'DB_NAME', 'your_database_name' ); define( 'DB_USER', 'your_database_user' ); define( 'DB_PASSWORD', 'your_database_password' ); define( 'DB_HOST', 'localhost' );
In your hosting panel, open MySQL Databases and note the exact database name and username. Spelling, underscores and prefixes must match exactly. If you recently changed the database password, update DB_PASSWORD in wp-config.php with the new one. Save and reload. If it loads, you are done.
Fix 2 — Test the database connection
In your hosting panel, open phpMyAdmin and click your site’s database name. If phpMyAdmin opens and you can see your tables (wp_posts, wp_options, wp_users), the database exists and is running. If phpMyAdmin will not open, or the database name is missing, the problem is on the server side.
Fix 3 — Check if the database server is down
Try opening your site in a private / incognito window, and also try yoursite.com/wp-admin. If both show the same error, it is not a browser cache issue. Check your host’s status page for an outage. Wait 10–15 minutes and reload. Check DB_HOST in wp-config.php — most hosts use localhost, but some use an address like mysql.yourhost.com or localhost:3306. Use exactly what your host shows.
Fix 4 — Repair a corrupted database
Open wp-config.php and add this line just above the line that says “That’s all, stop editing!”:
define( 'WP_ALLOW_REPAIR', true );
Save, then visit yoursite.com/wp-admin/maint/repair.php — you do not need to be logged in. Click “Repair Database” first. If that does not fix it, click “Repair and Optimize Database”. When it finishes, reload your site. Important: go back to wp-config.php and delete the WP_ALLOW_REPAIR line. Leaving it in place lets anyone run the repair page without logging in.
Fix 5 — Check for corrupted WordPress files
Download a fresh copy of WordPress from wordpress.org for the same version as your site, and unzip it on your laptop. Using FTP or File Manager, upload the fresh wp-admin and wp-includes folders to your site root, overwriting the old ones. Do NOT overwrite wp-content and do NOT overwrite wp-config.php. Reload your site.
Fix 6 — Contact your host (with the right information)
If you have done Fixes 1–5 and the error is still there, contact your host. Copy-paste this message and fill in your details: Hi, my WordPress site [yourdomain.com] is showing ‘Error establishing a database connection’ since [date/time]. I have checked the DB_NAME, DB_USER and DB_PASSWORD in wp-config.php, tested the database in phpMyAdmin, and run the WordPress database repair. The site is still down. Please check if the MySQL server is running, if my database is suspended or over quota, and restore from backup if needed. Thank you. Ask them to confirm: (1) MySQL server status, (2) correct DB_HOST value, (3) whether a backup restore is needed.
How to reduce the chance of it happening again: Keep one automatic backup switched on and test a restore once. After any host migration or password change, update wp-config.php in the same session. Update WordPress, themes and plugins one at a time. Avoid editing wp-config.php or database tables directly unless you have just taken a backup.
Quick checklist — work through it in order: 1 Backup taken, 2 Credentials checked, 3 Connection tested, 4 Server checked, 5 Database repaired and WP_ALLOW_REPAIR removed, 6 Core files refreshed, 7 Host contacted.
FAQs: What does this error mean? It means WordPress could not log in to its database using the details in wp-config.php, or the database server did not respond. Will I lose my posts? Usually, no — posts stay in the database while the connection is broken. Where do I find my correct database name, username and password? In your hosting control panel under MySQL Databases. Compare exactly with DB_NAME, DB_USER and DB_PASSWORD in wp-config.php. Is the repair page safe? Yes, when you add WP_ALLOW_REPAIR, run the repair, then delete the line immediately. I tried everything and the site is still down — what next? Contact your host using the message in Fix 6.
That’s it. Work in order, back up first, and remove the repair line when you finish. Most sites come back at Fix 1 or Fix 4.
One Comment