Estimated reading time: 28 minutes
The “Error Establishing a Database Connection” in WordPress means your site can’t communicate with its MySQL database, making your entire website inaccessible. The most common fix is verifying and correcting the database credentials (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST) in your wp-config.php file.
If you’re seeing this error right now, your site is down for everyone, including you. But here’s the good news: your content is safe. It’s still sitting in your database, exactly where you left it. WordPress just can’t reach it.
This error is one of the most common WordPress issues, and it’s almost always fixable without any specialized tools. In most cases, you can resolve it in under 30 minutes.
In this guide, we’ll walk through what causes the error, how to diagnose it quickly, and eight step-by-step fixes ordered from the most common solution to the least. We’ll also cover how to prevent it from happening again.
Table of contents
What Does “Error Establishing a Database Connection” Mean?
WordPress stores everything on your site in a MySQL database: posts, pages, comments, user accounts, plugin settings, theme options, and more. The only things not in the database are your media files and the code files themselves.
Every time someone visits a page on your site, WordPress runs PHP code that connects to MySQL, pulls the relevant content from the database, and assembles it into the page the visitor sees. This happens on every single page load.
When that connection between PHP and MySQL fails, WordPress can’t retrieve anything. It has no content to display, no settings to load, and no way to build a page. Instead, it shows a white screen with a single line of text: “Error establishing a database connection.”
This error typically takes down your entire site. Both the front end (what visitors see) and the back end (your WordPress admin dashboard at /wp-admin) become inaccessible. You won’t be able to log in, edit content, or make changes through the dashboard.
One thing to be aware of: if your site uses page caching, visitors might still see cached versions of your pages for a while after the error starts. This can be misleading. You might think the site is partially working when it’s actually fully disconnected from the database. New visitors or anyone who clears their cache will see the error immediately.
The important thing to understand is that this is a connection problem, not a data problem. Your content hasn’t been deleted or damaged (in most cases). Once you restore the connection, your site comes right back.
What Causes This Error?
Several things can break the connection between WordPress and your database. Here are the most common causes, ordered by how likely they are to be the culprit.
Incorrect Database Credentials in wp-config.php
This is the number one cause. WordPress stores four pieces of database information in a file called wp-config.php: the database name (DB_NAME), the database username (DB_USER), the database password (DB_PASSWORD), and the database host (DB_HOST).
If any of these four values are wrong, WordPress can’t log in to the database. It’s the same as trying to enter a building with the wrong key.
This commonly happens after a site migration to a new host, after a hosting provider resets database passwords during maintenance, or after someone manually edits wp-config.php and introduces a typo.
Database Server Is Down or Overloaded
Your MySQL database runs on a server. If that server crashes, runs out of memory, or gets overwhelmed by too many simultaneous connections, WordPress can’t reach it.
This is especially common on shared hosting plans, where your database shares server resources with hundreds of other websites. A traffic spike on your site (or even on someone else’s site on the same server) can push the database server past its limits.
Corrupted Database Tables
WordPress is a dynamic system. Every time you install a plugin, activate a theme, publish a post, or update a setting, WordPress reads from and writes to multiple database tables. Over time, these tables can become corrupted due to server crashes, interrupted writes, or buggy plugin code.
When this happens, you might see a variation of the error: “One or more database tables are unavailable. The database may need to be repaired.”
Corrupted WordPress Core Files
The PHP files that make up WordPress itself can get damaged too. This typically happens after a failed update, an incomplete file transfer during migration, or a malware infection. If the core files responsible for initiating the database connection are corrupted, the connection fails even when your credentials and database are fine.
Incorrect Site URL in the Database
This one is less common but worth knowing about, especially after migrations. WordPress stores your site’s URL in the wp_options database table. If these values don’t match your actual domain (for example, after moving to a new domain or switching between HTTP and HTTPS), it can cause redirect loops that manifest as a database connection error.
Upgrade Your Website with a Premium WordPress Theme
Find a theme that you love and get a 10% discount at checkout with the FLASH10 code
Choose your theme
Quick Diagnostic: Where Does the Error Appear?
Before you start troubleshooting, take 30 seconds to check where the error shows up. This tells you a lot about what’s causing it and where to start.
| Error appears on… | Most likely cause | Start with… |
|---|---|---|
| Front end AND wp-admin | Database credentials or server issue | Step 2: Verify credentials in wp-config.php |
| Front end only | Database connection settings or URL mismatch | Step 4: Check if the database server is running |
| wp-admin only | Plugin or theme conflict, file permissions | Step 6: Check for corrupted core files |
Pro Tip: Try logging into phpMyAdmin through your hosting control panel. If phpMyAdmin loads and you can see your database tables, the MySQL server is running fine. That narrows the problem to your wp-config.php credentials or your WordPress files.
How to Fix “Error Establishing a Database Connection” (Step by Step)
Work through these fixes in order. They’re arranged from the most common solution (and simplest to try) to the least common. Test your site after each step.
Step 1: Back Up What You Can
Before changing anything, try to take a backup. If you can access your hosting control panel, use its built-in backup tool. If your host offers one-click backups, now is the time to use one.
If you can’t access anything at all, don’t worry. The fixes below involve small, reversible changes. But as a general rule, always back up your WordPress site before troubleshooting.
Step 2: Verify Your Database Credentials in wp-config.php
Since incorrect credentials cause this error more often than anything else, start here.
You need to compare the database credentials in your wp-config.php file against the actual credentials in your hosting account. Here’s how:
1. Open wp-config.php
Access your site’s files through your hosting panel’s File Manager or connect via FTP/SFTP using a client like FileZilla. Navigate to your WordPress root directory (usually public_html or www). Open wp-config.php.
2. Find the database credentials
Look for these four lines near the top of the file:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_username' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );
3. Cross-reference with your hosting panel
Log into your hosting control panel (cPanel, Plesk, or whatever your host provides). Go to MySQL Databases and verify:
- The database listed in your panel matches DB_NAME exactly
- The database user matches DB_USER exactly
- The user is assigned to the correct database with All Privileges
4. Fix any mismatches
If any value is wrong, update it in wp-config.php and save the file. Pay attention to these common traps:
- Trailing spaces after the password or username
- Hosting prefixes: many hosts add a prefix to database names and usernames (e.g., yoursite_wp_db instead of just wp_db)
- DB_HOST value: this is localhost for most hosts, but some use a custom hostname. Check your host’s documentation if you’re not sure
- Recently changed passwords: if your host reset your database password, wp-config.php still has the old one
After saving, reload your site. If this was the problem, your site should come right back.
Step 3: Test the Database Connection Manually
If you’ve double-checked the credentials and they look correct, test the connection directly. This bypasses WordPress entirely and tells you whether the credentials actually work.
Create a file called testdb.php in your WordPress root directory with this code:
<?php
$link = mysqli_connect('DB_HOST_value', 'DB_USER_value', 'DB_PASSWORD_value', 'DB_NAME_value');
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Replace the placeholder values with your actual credentials from wp-config.php. Then visit https://yoursite.com/testdb.php in your browser.
- If it says “Connected successfully”: your credentials are correct. The problem is somewhere in your WordPress files, not the database connection. Skip to Step 5 or 6.
- If it shows an error: the credentials are wrong or the database server isn’t reachable. Go back to Step 2 and recheck everything.
Important: Delete testdb.php immediately after testing. It contains your database credentials in plain text, and anyone who finds the URL could see them.
Step 4: Check if Your Database Server Is Running
Sometimes the problem has nothing to do with your files or credentials. The MySQL server itself might be down.
Log into your hosting panel and open phpMyAdmin. If it loads and you can see your database tables listed in the sidebar, the server is running properly.
If phpMyAdmin won’t load, times out, or shows an error, the database server is likely down. This is a hosting-level issue, not something you can fix yourself.
Contact your hosting provider and let them know:
- When the error started
- That phpMyAdmin isn’t loading either
- That you’ve already verified your wp-config.php credentials
Most hosts can restart MySQL or move you to a healthier server relatively quickly.
Step 5: Repair Your WordPress Database
If the server is running and credentials are correct, your database tables might be corrupted. WordPress has a built-in repair tool for exactly this situation.
1. Enable the repair tool
Open wp-config.php and add this line just above the comment that says /* That’s all, stop editing! Happy publishing. */:
define( 'WP_ALLOW_REPAIR', true );
Save the file.
2. Run the repair
In your browser, go to:
https://yoursite.com/wp-admin/maint/repair.php
You’ll see two options: Repair Database and Repair and Optimize Database. Choose Repair and Optimize Database. Optimizing cleans up overhead in your tables, which can improve performance.
WordPress will work through each table and report the results. When it finishes, check your site.
3. Remove the repair line
Critical: Go back to wp-config.php and remove the WP_ALLOW_REPAIR line immediately. This repair endpoint doesn’t require a login, anyone who knows the URL can access it. Leaving it enabled is a security risk.
Step 6: Check for Corrupted WordPress Core Files
If the database connection tests fine but WordPress still can’t connect, the problem might be in WordPress’s own files.
Here’s how to replace potentially corrupted core files without affecting your content, themes, or plugins:
- Download a fresh copy of WordPress from wordpress.org/download
- Extract the zip file on your computer
- Connect to your site via FTP/SFTP
- Upload the wp-admin and wp-includes folders from the fresh download, overwriting the existing ones
Do not upload the wp-content folder. That’s where your themes, plugins, and uploads live. Overwriting it would remove your customizations.
This replaces all core WordPress files with clean copies. If a corrupted file was breaking the database connection, this fixes it.
For more on keeping WordPress files healthy, see our guide on how to update WordPress safely.
Step 7: Verify Database User Privileges
Even if the credentials are correct, the database user might not have permission to actually interact with the database. This can happen after hosting migrations or when your host makes changes to their MySQL configuration.
In cPanel, go to MySQL Databases and scroll to the section that shows which users are assigned to which databases. Click the database user and make sure All Privileges is checked.
If the user wasn’t assigned to the database at all, add it. If it was assigned but with limited privileges, grant full access and save.
Step 8: Contact Your Hosting Provider
If none of the above steps resolved the error, the issue is likely on the server side, beyond what you can fix through file edits and database tools.
When you reach out to your host, give them this information up front:
- The exact error message
- When it first appeared
- What you’ve already tried (credentials verified, repair tool run, core files replaced)
- Whether the error appears on the front end, wp-admin, or both
A good hosting provider can check MySQL logs, restart services, and diagnose server-level problems that aren’t visible from your end. Issues like insufficient server memory, exceeded MySQL connection limits, or hardware failures all require host-side intervention.
If your host’s response is slow or unhelpful, or if you’re experiencing database errors regularly, it might be worth considering a more reliable WordPress hosting provider.
How to Prevent Database Connection Errors
Once your site is back online, take a few steps to reduce the chances of this happening again.
Keep Regular Backups
Automated daily backups are your safety net. If something goes wrong, you can restore a working version of your site in minutes rather than spending hours troubleshooting. Make sure your backup solution includes both your files and your database.
Update WordPress, Themes, and Plugins
Outdated software can introduce bugs that corrupt database tables or cause conflicts. Keep everything updated, but test updates on a staging site first, especially for major releases.
Choose Reliable Hosting
If you’re on a cheap shared hosting plan and experiencing database errors frequently, your site may have outgrown that environment. Managed WordPress hosts and VPS plans offer dedicated database resources and better stability under traffic spikes.
Optimize Your Database Periodically
Over time, WordPress databases accumulate overhead: post revisions, spam comments, transient options, and orphaned metadata from deleted plugins. This bloat doesn’t just slow your site down. It can increase the chances of table corruption, especially on shared hosting where resources are limited.
Use phpMyAdmin’s built-in optimize function to clean up table overhead, or install a database cleanup plugin that can remove old revisions, auto-drafts, and expired transients on a schedule. Running this monthly is usually enough for most sites.
Monitor Your Site’s Uptime
Use an uptime monitoring service that checks your site every few minutes and alerts you when it goes down. Catching a database connection error within minutes means you can fix it before it impacts your search rankings or visitor experience.
FAQ
It means WordPress can’t connect to the MySQL database where all your site’s content is stored. Without that connection, WordPress can’t load any pages, and your entire site goes down. The most common cause is incorrect database credentials in your wp-config.php file.
No. Your posts, pages, media, and all other content are still in the database. The error means WordPress can’t reach the database, not that the data has been erased. Once you fix the connection, everything comes back exactly as it was.
It’s difficult. Most fixes require editing wp-config.php or uploading files, which means you need FTP/SFTP access or your hosting panel’s File Manager. If you don’t have access to either, contact your hosting provider’s support team. They can usually make the changes for you.
Site migration is one of the most common triggers. Your new hosting environment almost certainly has different database credentials, a different database name, username, password, or host value. When you move your files but don’t update the credentials in wp-config.php to match the new host, WordPress tries to connect with the old information and fails. Update all four DB values to match your new hosting account.
No. The database repair page at /wp-admin/maint/repair.php does not require any login or authentication. Anyone who knows that URL can access and run the repair tool on your database. Always remove the define(‘WP_ALLOW_REPAIR’, true); line from wp-config.php immediately after you finish repairing.
Yes. An attacker who gains access to your server could modify the database credentials in wp-config.php, delete database tables, or inject malware into core files, all of which can trigger this error. If you suspect a security breach, change all your passwords (WordPress admin, database, FTP, hosting panel), run a malware scan, and restore from a clean backup. For more on protecting your site, see our guide on how to secure your WordPress site from hackers.
If your site stays down for an extended period, search engine crawlers like Googlebot can’t access your pages. Short downtime (a few hours) usually doesn’t cause lasting damage. But if the error persists for days, search engines may temporarily remove your pages from results. The longer it takes to fix, the harder it is to recover your rankings. This is why speed matters when troubleshooting this error.
The “Error Establishing a Database Connection” is a specific error. It tells you exactly what’s wrong: WordPress can’t connect to MySQL. The White Screen of Death is a more general problem. It shows a blank screen with no error message and is usually caused by PHP errors, memory limits, or plugin/theme conflicts rather than a database issue. The troubleshooting steps for each are different.
Wrapping Up
The “Error Establishing a Database Connection” looks intimidating, but it usually comes down to one of a handful of fixable issues. Start with the most common cause: double-check your database credentials in wp-config.php. If that’s not it, work through the remaining steps.
Here’s a quick reference:
- Back up before changing anything
- Verify credentials in wp-config.php against your hosting panel
- Test the connection with a PHP script
- Check if MySQL is running via phpMyAdmin
- Repair the database using WP_ALLOW_REPAIR
- Replace core files with a fresh WordPress download
- Check user privileges in your hosting panel
- Contact your host if nothing else works
Once your site is back online, protect yourself by keeping regular backups, updating your software, and monitoring your site’s uptime. These habits won’t just prevent database errors. They’ll keep your entire WordPress site running more reliably.
For more WordPress troubleshooting guides, check out our articles on fixing the WordPress critical error and resolving the “Briefly Unavailable for Scheduled Maintenance” error.
