Category: WordPress Troubleshooting
How to Fix Error Establishing a Database Connection in WordPress
“Error establishing a database connection” is one of the most serious-looking WordPress errors because it can take your whole website offline. Instead of showing your homepage, blog posts, products, or admin dashboard, WordPress displays a plain database connection error.
The good news is that this error is usually fixable. In most cases, WordPress cannot connect to the database because of incorrect wp-config.php details, a changed database password, a down database server, corrupted database tables, hosting resource limits, or a failed migration. This guide shows you how to troubleshoot the issue safely and bring your WordPress site back online.
TL;DR: Fastest Safe Fix
To fix “Error establishing a database connection” in WordPress, first check whether your hosting database server is online. Then open wp-config.php and verify DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST. If the details are correct, check database user permissions, repair corrupted tables, review hosting resource limits, and inspect server logs. If the error started after a migration, confirm the imported database, database prefix, hostname, and user privileges.
What Does “Error Establishing a Database Connection” Mean?
WordPress stores your posts, pages, users, settings, comments, WooCommerce orders, plugin options, and many site configurations inside a MySQL or MariaDB database. When someone visits your site, WordPress needs to connect to that database before it can load most content.
The error appears when WordPress cannot create that connection. Your WordPress files may still be present, but the application cannot reach the database or authenticate with it.
Common signs include:
- Your whole website shows “Error establishing a database connection.”
wp-adminalso shows the same database error.- The frontend is down, but hosting File Manager still works.
- The error appears after a migration or restore.
- The error appears after changing database passwords.
- The error appears randomly during traffic spikes.
- The error affects only one site on a hosting account with multiple WordPress installs.
External reference: WordPress explains this error in its official common errors documentation here: Common WordPress Errors.
What Usually Causes This Database Error?
This error is not always caused by a broken database. Often, the database is fine, but WordPress is using the wrong login details or the database server is temporarily unavailable.
Most common causes include:
- Incorrect database name: The
DB_NAMEvalue inwp-config.phpdoes not match the real database name. - Incorrect database username: The
DB_USERvalue is wrong or the database user was removed. - Incorrect database password: The database password changed but
wp-config.phpwas not updated. - Wrong database host: The
DB_HOSTvalue is wrong. Some hosts uselocalhost, while others use a separate database hostname. - Missing user privileges: The database user exists but does not have permission to access the selected database.
- Database server is down: MySQL or MariaDB may be stopped, overloaded, or temporarily unavailable.
- Corrupted database tables: Important WordPress tables may need repair.
- Failed migration: Files moved, but the database was not imported correctly or connection details were not updated.
- Hosting resource limits: The database server may be rejecting connections because of high traffic, too many connections, or account limits.
- Damaged wp-config.php file: A syntax mistake, missing quote, wrong character, or accidental edit can stop WordPress from reading the credentials correctly.
Before You Start: Do These Safety Checks
Do not delete the database or reinstall WordPress immediately. This error can often be fixed by correcting credentials or restoring database access. Reinstalling WordPress too early can make recovery harder.
Safety checklist:
- Take a backup of your current
wp-config.phpfile before editing it. - Do not drop, empty, or overwrite database tables unless you are restoring from a verified backup.
- If the site is WooCommerce, LMS, membership, or booking-based, avoid restoring an old database unless necessary because new orders, users, payments, or submissions may be lost.
- Write down what changed recently: migration, password change, restore, hosting transfer, plugin update, or server issue.
- Check whether other websites on the same hosting account are also affected.
- Use hosting File Manager, SFTP, phpMyAdmin, or your hosting database tools carefully.
If you have a database or server log, you can use the FyrePress Server Log Analyzer to identify whether the issue points to authentication failure, unavailable database server, table corruption, or connection limits.
Fix 1: Check If the Database Server Is Down
Before editing WordPress files, check whether the database server is actually online. If the database service is down, your wp-config.php details may be correct, but WordPress still cannot connect.
Quick checks:
- Open your hosting status page if your provider has one.
- Check whether other websites on the same hosting account are also showing database errors.
- Log in to your hosting control panel and open phpMyAdmin.
- If phpMyAdmin fails to load or shows a MySQL/MariaDB connection issue, the server may be down.
- Ask your host if MySQL/MariaDB is running normally on your account or server.
If the database server is down, this is usually a hosting-side issue. Contact your hosting provider and share the exact error, affected domain, and approximate time the error started.
Fix 2: Verify Database Details in wp-config.php
The most important file to check is wp-config.php. This file sits in your WordPress root folder and tells WordPress which database to connect to.
Open wp-config.php and find these lines:
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
What each value means:
DB_NAME: the exact name of your WordPress database.DB_USER: the database username assigned to that database.DB_PASSWORD: the password for the database user.DB_HOST: the database server hostname, oftenlocalhost, but not always.
Compare these values with the database information shown in your hosting control panel. Be careful with spelling, prefixes, underscores, capital letters, quotes, and spaces. A single wrong character can break the connection.
External reference: WordPress explains the wp-config.php file and its database connection role here: Editing wp-config.php.
FyrePress tool: You can use the FyrePress wp-config.php Builder to generate clean, reviewable WordPress configuration constants.
Fix 3: Confirm the Database Name Is Correct
Many database connection errors happen after migrations because the database name on the new hosting account is different from the old one. Some hosts add account prefixes to database names.
Example:
Old database name:
wordpress
New hosting database name:
u123456_wordpress
If wp-config.php still contains the old database name, WordPress will not connect to the new database.
How to check:
- Open your hosting control panel.
- Go to MySQL Databases or Database Manager.
- Find the database assigned to your WordPress site.
- Copy the exact database name.
- Paste it into the
DB_NAMEvalue inwp-config.php. - Save the file and reload the site.
If you have multiple databases, open phpMyAdmin and check which one contains familiar WordPress tables such as wp_posts, wp_options, wp_users, and wp_usermeta.
Fix 4: Confirm the Database Username and Password
If the database name is correct, the next step is checking the database username and password. This issue commonly happens when a password is changed in the hosting panel but not updated in wp-config.php.
What to do:
- Open your hosting control panel.
- Go to MySQL Databases or Database Users.
- Find the database user assigned to your WordPress database.
- Reset the database user password if you are not sure it is correct.
- Update
DB_USERandDB_PASSWORDinwp-config.php. - Save the file and reload the site.
Be careful when copying passwords. Extra spaces, smart quotes, missing symbols, or pasted line breaks can cause authentication failures.
Correct format:
define( 'DB_USER', 'u123456_wpuser' );
define( 'DB_PASSWORD', 'YourStrongDatabasePasswordHere' );
If you reset the password, update only the database password in wp-config.php. Do not change your WordPress admin password here; these are different credentials.
Fix 5: Check Database User Privileges
The database user must have permission to access and modify the WordPress database. If the user exists but is not assigned to the database, WordPress may fail to connect or fail during certain actions.
How to check privileges:
- Open your hosting control panel.
- Go to MySQL Databases.
- Find the database user.
- Make sure the user is assigned to the correct database.
- Grant all required privileges for that database.
- Save changes and reload the site.
Most WordPress sites need the database user to be able to read, insert, update, delete, create, alter, and manage WordPress tables. If your hosting panel has an “All Privileges” option for the selected database user, that is commonly used for standard WordPress installations.
External reference: WordPress’ database creation guide explains creating a database and database user with privileges for WordPress here: Creating a Database for WordPress.
Fix 6: Check the Database Host Value
Many WordPress installations use localhost as the database host, but not all hosts do. Managed WordPress platforms, cloud hosting, external database servers, and some control panels may use a different hostname, port, or socket.
Common examples:
define( 'DB_HOST', 'localhost' );
define( 'DB_HOST', '127.0.0.1' );
define( 'DB_HOST', 'localhost:3306' );
define( 'DB_HOST', 'mysql.examplehost.com' );
Check your hosting database page for the correct hostname. If you recently migrated the site, the old host may have used localhost, while the new host may require a specific database server address.
If you are unsure, ask your host: “What should I use as the WordPress DB_HOST value for this database?”
Fix 7: Repair the WordPress Database
If your database credentials are correct and the server is online, some WordPress database tables may be corrupted. WordPress has a built-in database repair mode you can enable temporarily.
Add this line to wp-config.php before the final stop-editing comment:
define( 'WP_ALLOW_REPAIR', true );
Then open this URL in your browser:
https://yourdomain.com/wp-admin/maint/repair.php
Choose Repair Database. If needed, choose Repair and Optimize Database. After the repair finishes, immediately remove this line from wp-config.php:
define( 'WP_ALLOW_REPAIR', true );
Do not leave repair mode enabled. The repair page can be accessed without logging in while the constant is active.
If repair fails or reports serious table damage, contact your host or restore the database from a clean backup.
Fix 8: Check If the Database Tables Exist
After a migration or restore, WordPress files may be present but the database may be empty, missing, or imported into the wrong database.
How to check in phpMyAdmin:
- Open phpMyAdmin from your hosting panel.
- Select the database listed in
DB_NAME. - Check whether WordPress tables exist.
- Look for tables such as
wp_posts,wp_options,wp_users, andwp_usermeta.
Your table prefix may not be wp_. Some sites use a custom prefix such as wpx7_ or site1_. The active table prefix is defined in wp-config.php:
$table_prefix = 'wp_';
If the database is empty or missing expected WordPress tables, import the correct database backup. If the tables exist but use a different prefix, update $table_prefix carefully to match the real table prefix.
Fix 9: Check for Hosting Resource Limits
If the error appears randomly and disappears after refreshing, your database credentials may be correct, but your hosting account may be hitting resource limits.
Signs of resource-related database errors:
- The error appears during traffic spikes.
- The error appears during imports, backups, or cron jobs.
- The error happens during WooCommerce checkout or admin reports.
- The site works sometimes and fails sometimes.
- Other sites on the same server also feel slow.
- Hosting logs mention too many connections, timeouts, or killed queries.
Ask your host to check MySQL connection limits, slow queries, account CPU/RAM limits, PHP workers, and database server load. On shared hosting, the fix may require reducing heavy plugins, optimizing queries, disabling aggressive crawlers, or upgrading to a plan with more resources.
Fix 10: Check Server and Database Logs
Logs can tell you whether the issue is an authentication failure, unavailable database host, corrupted table, too many connections, missing database, or permission problem.
Where to check:
- Hosting control panel error logs
- MySQL or MariaDB error logs
- PHP error logs
- LiteSpeed, Apache, or Nginx logs
- Cloud hosting database logs
- WooCommerce logs if the issue affects checkout or orders
Common log clues:
Access denied for user
Unknown database
Too many connections
Can't connect to local MySQL server
Table './database/wp_options' is marked as crashed
Lost connection to MySQL server during query
Each message points to a different fix. “Access denied” usually means wrong user, password, or privileges. “Unknown database” means the database name is wrong or missing. “Too many connections” points to server limits or traffic spikes. A crashed table points to database repair or restore.
Fix 11: Review Recent Migrations or Restores
This error is very common after moving WordPress to a new host or restoring a backup. In migrations, the files and database must match each other.
Migration checklist:
- Was the database imported successfully?
- Does
DB_NAMEmatch the newly imported database? - Does
DB_USERexist on the new hosting account? - Was the database password updated in
wp-config.php? - Is
DB_HOSTcorrect for the new host? - Does
$table_prefixmatch the imported table prefix? - Are the database user privileges assigned to the right database?
- Did the migration tool skip or fail during database import?
External reference: WordPress’ migration documentation includes updating wp-config.php with the new MySQL database name, user, and password during migration: Migrating WordPress.
Fix 12: Enable WordPress Debugging Carefully
WordPress debug mode is more commonly used for PHP errors, but it can still help reveal database-related warnings or plugin issues that appear near the same time as the connection error.
Add these lines to wp-config.php before the final stop-editing comment:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
Then reload the affected page once and check:
wp-content/debug.log- Hosting PHP error logs
- Database logs in your hosting panel
- Server logs for MySQL/MariaDB connection failures
External reference: WordPress documents these debugging constants here: Debugging in WordPress.
Important: keep WP_DEBUG_DISPLAY set to false on live sites so visitors do not see technical errors.
Fix 13: Ask Your Host the Right Questions
If your credentials look correct and the database still does not connect, contact hosting support with specific questions. This makes support faster and avoids generic replies.
Ask your host:
- Is the MySQL/MariaDB service running normally?
- Is my database server overloaded or rejecting connections?
- Is the database user able to connect to the selected database?
- Are there “access denied,” “too many connections,” or crashed table errors in the logs?
- What is the correct
DB_HOSTvalue for this database? - Was the database password, hostname, or server recently changed?
- Are there account-level CPU, RAM, I/O, or database connection limits being hit?
This is especially important on shared hosting, managed WordPress hosting, cloud hosting, and recently migrated websites.
Best Fix Method by User Type
The best recovery path depends on your access level and how your site is hosted.
For beginners
Start by contacting your host and asking them to confirm whether the database server is running. Then ask them to verify your database name, username, password, hostname, and user privileges against your wp-config.php file.
For site owners with hosting access
Open wp-config.php, compare database details with your hosting panel, reset the database user password if needed, assign privileges again, and check phpMyAdmin to confirm the database tables exist.
For developers
Test database credentials directly, inspect MySQL/MariaDB logs, confirm socket/host/port behavior, check table prefix, review migration integrity, inspect slow queries and connection limits, and repair or restore corrupted tables only after taking a fresh backup.
For WooCommerce and membership sites
Avoid restoring an old database unless absolutely necessary. New orders, users, subscriptions, payments, bookings, form entries, and membership activity may be lost. Fix credentials, server access, or table corruption first.
How to Prevent Database Connection Errors
Database connection errors are easier to prevent when your WordPress configuration, hosting resources, and backup process are managed carefully.
Prevention checklist:
- Keep a secure backup of your working
wp-config.phpfile. - Document the correct database name, user, host, and table prefix after migrations.
- Use strong database passwords and update
wp-config.phpimmediately after password changes. - Do not delete database users or change privileges unless you understand the impact.
- Use staging before migrations, restores, and major site changes.
- Monitor hosting resource limits if the error appears during traffic spikes.
- Optimize heavy plugins, slow queries, and large WooCommerce databases.
- Keep verified database backups, not just file backups.
- Check server logs after migrations and restores.
- Use reliable hosting for sites with orders, users, bookings, or high traffic.
Related FyrePress guide: Create a WordPress Staging Site Before Updates.
Final Recommendation
The safest way to fix “Error establishing a database connection” is to start with the basics: database server status, wp-config.php credentials, database user permissions, and table existence. These checks solve many cases without touching your content.
If those details are correct, move deeper into database repair, hosting resource limits, migration issues, and server logs. Do not reinstall WordPress or overwrite the database until you know what failed. In most cases, the exact cause is visible in the database, PHP, or hosting error logs.
Frequently Asked Questions
What causes Error Establishing a Database Connection in WordPress?
The most common causes are incorrect database name, username, password, or host in wp-config.php, missing database user privileges, a down database server, corrupted database tables, failed migration, or hosting resource limits.
Can I fix the database connection error without wp-admin?
Yes. This error usually blocks wp-admin, but you can fix it through hosting File Manager, SFTP, phpMyAdmin, MySQL database tools, and hosting support by checking wp-config.php and database access.
Where are WordPress database connection details stored?
WordPress database connection details are stored in the wp-config.php file. The main constants are DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.
Does this error mean my WordPress database is deleted?
Not always. In many cases, the database still exists but WordPress cannot connect because the credentials, host, permissions, or database server are wrong or unavailable.
How do I repair the WordPress database?
You can temporarily add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, then visit /wp-admin/maint/repair.php and run the repair tool. Remove the constant immediately after the repair finishes.
Why does the database connection error appear randomly?
Random database connection errors often point to hosting resource limits, too many database connections, traffic spikes, slow queries, overloaded MySQL/MariaDB service, or unstable database hosting.
Should I restore a backup to fix this error?
Restore a backup only if credentials, permissions, database server status, table repair, and migration checks do not fix the issue. For WooCommerce, membership, booking, and LMS sites, restoring an old database can remove new activity.
How can I prevent this error in the future?
Keep verified database backups, document wp-config.php credentials after migrations, avoid changing database users randomly, monitor hosting resources, use staging, and check logs after major hosting or database changes.