How to Fix WordPress Database Connection Error
A WordPress Database Connection Error occurs when WordPress cannot communicate with the database that stores your website’s content, settings, users, orders, and other important information.
Instead of loading your website normally, visitors may see a message such as:
Error establishing a database connection
This error can affect the entire website and may also prevent you from accessing the WordPress administrator area.
Fortunately, the problem is often caused by a configuration issue, unavailable database server, incorrect database credentials, corrupted database tables, or hosting resource limitations.
This guide explains how to identify and fix the WordPress database connection error step by step.
What Is a WordPress Database Connection Error?
WordPress uses a database, usually MySQL or MariaDB, to store dynamic website information.
When someone visits your WordPress website, WordPress connects to the database and retrieves information such as:
- Posts and pages
- User accounts
- Website settings
- Plugin settings
- Theme settings
- Comments
- WooCommerce products and orders
- Categories and tags
- Metadata
If WordPress cannot establish that connection, it cannot build the requested page.
The result is commonly:
Error establishing a database connection
Common Causes of the Error
Several problems can prevent WordPress from connecting to its database.
1. Incorrect database credentials
WordPress uses values stored in wp-config.php to connect to the database.
If the database name, username, password, or host is incorrect, the connection can fail.
2. Database server is unavailable
Your MySQL or MariaDB server may be temporarily unavailable because of a server problem, maintenance, or overload.
3. Incorrect database host
Most WordPress installations use localhost as the database host, but some hosting environments use a different hostname or database server address.
4. Corrupted database
One or more database tables may become damaged or corrupted.
5. Hosting resource limits
A busy website may exceed available CPU, RAM, database connections, or other hosting resources.
6. Incorrect database user permissions
The database user may no longer have permission to access the required database.
7. DNS or networking problems
On websites using a remote database server, network or DNS problems can prevent WordPress from reaching the database.
8. Recent configuration changes
Changing hosting accounts, migrating websites, modifying database credentials, or editing wp-config.php can cause connection problems.
Step 1: Confirm the Error
First, open your website in a private/incognito browser window.
You can also try:
https://example.com
and:
https://example.com/wp-admin/
If both show a database connection error, the problem is likely related to WordPress configuration, the database, or the hosting environment.
If only one page is affected, the cause may be different.
Step 2: Check Whether Your Hosting Server Is Working
Before changing WordPress files, check your hosting control panel.
Depending on your hosting environment, you may have access to:
- cPanel
- DirectAdmin
- Plesk
- Server monitoring
- Resource usage
- Database management tools
Check whether:
- Your hosting account is active
- The database server is running
- Disk space is available
- Your account has exceeded resource limits
- There is scheduled maintenance
- Other websites on the same hosting account are working
If multiple websites using the same database server are unavailable, contact your hosting provider and ask whether there is a database server issue.
Step 3: Check the wp-config.php File
One of the most common causes is incorrect database information.
Open your WordPress root directory and locate:
wp-config.php
Look for these settings:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );
Check each value carefully.
The four important settings are:
| Setting | Purpose |
|---|---|
DB_NAME | Name of the WordPress database |
DB_USER | Database username |
DB_PASSWORD | Database user’s password |
DB_HOST | Database server hostname |
Even one incorrect character can prevent WordPress from connecting.
Step 4: Verify the Database Name
Log in to your hosting control panel and open your database management section.
Depending on your hosting provider, this may be called:
- MySQL Databases
- MySQL Management
- Databases
- Database Manager
Compare the database name with the value in:
define( 'DB_NAME', 'database_name' );
Make sure they match exactly.
Remember that some hosting environments automatically add an account prefix to database names.
For example:
account_wordpress
may be the actual database name rather than:
wordpress
Step 5: Verify the Database Username
Check which database user is assigned to your WordPress database.
Compare it with:
define( 'DB_USER', 'database_user' );
The username must be correct and must have access to the database.
A common mistake during migration is creating a new database user but forgetting to update wp-config.php.
Step 6: Verify the Database Password
If the database user’s password was recently changed, WordPress will continue using the old password until wp-config.php is updated.
For example:
define( 'DB_PASSWORD', 'your_database_password' );
Make sure the password matches the password configured for the database user.
Important
Do not confuse the following passwords:
- Hosting account password
- WordPress administrator password
- Database user password
- FTP password
They can be completely different.
Step 7: Verify the Database Host
Check the DB_HOST value:
define( 'DB_HOST', 'localhost' );
On many hosting environments, localhost is correct.
However, some servers use a specific database hostname.
If your hosting provider gave you a database hostname, use that value instead.
For example:
define( 'DB_HOST', 'database.example.com' );
Do not randomly change DB_HOST. Ask your hosting provider for the correct database hostname if you are unsure.
Step 8: Check Database User Permissions
A database user may exist but not have the required privileges.
Open your hosting database management section and check whether the correct database user is assigned to the WordPress database.
The user generally needs appropriate privileges to perform WordPress operations.
If the user was recently recreated or the website was migrated, make sure the user has been assigned to the correct database.
Step 9: Repair the WordPress Database
If your credentials are correct but WordPress still cannot connect, database corruption may be involved.
Before repairing anything, create a database backup if possible.
WordPress provides a built-in database repair mechanism.
Open:
wp-config.php
Add:
define( 'WP_ALLOW_REPAIR', true );
Then visit:
https://example.com/wp-admin/maint/repair.php
You may see options for repairing and optimizing database tables.
After completing the repair, remove:
define( 'WP_ALLOW_REPAIR', true );
from wp-config.php.
Security note
Do not leave the database repair feature enabled permanently because the repair page does not require normal WordPress authentication.
Step 10: Use phpMyAdmin to Check the Database
Many hosting providers include phpMyAdmin.
Open phpMyAdmin from your hosting control panel and select the WordPress database.
You should see tables such as:
wp_posts
wp_users
wp_options
wp_terms
wp_comments
The actual table prefix may be different.
If the tables appear normally, the database itself may still be accessible.
If phpMyAdmin reports errors when opening the database or tables, investigate those errors before making additional changes.
Step 11: Check Database Tables for Errors
In phpMyAdmin, select the WordPress database.
You can inspect the status of individual tables.
If a table reports an issue, the Repair table option may be available depending on the storage engine and server configuration.
Always create a backup before performing database repairs.
Do not manually delete database tables simply because they appear unfamiliar.
Plugins frequently create their own tables.
Step 12: Check Your Hosting Disk Space
A full disk can cause unexpected database problems.
Check your hosting account’s disk usage.
Look for:
- Website files
- Backup files
- Logs
- Email storage
- Cache files
- Large media files
- Temporary files
If your disk is completely full, the database server may not be able to perform normal operations.
Free unnecessary space and then test the website again.
Step 13: Check Hosting Resource Limits
High traffic or resource-intensive applications can sometimes cause database connection problems.
Check your hosting account for:
- CPU usage
- RAM usage
- Database connections
- Entry processes
- PHP workers
- I/O usage
- Process limits
This is particularly important for:
- WooCommerce stores
- High-traffic websites
- Membership websites
- Large WordPress installations
- Websites with many plugins
- Websites running heavy queries
If resource usage repeatedly reaches the hosting limits, increasing WordPress’s PHP memory alone may not resolve the problem.
Step 14: Check the Database Server Status
If your database credentials are correct and the database appears healthy, the database server itself may be unavailable.
On a managed hosting plan, contact your hosting provider and ask them to verify:
- MySQL/MariaDB service status
- Database server connectivity
- Database server logs
- Maximum connection limits
- Current server load
- Whether your database account has been restricted
On a VPS or dedicated server, you may need to check the database service yourself.
For example, depending on the operating system and database software, administrators may check the service status using system management tools.
Do not restart database services on a production server without understanding the impact.
Step 15: Check Recent Website Migrations
Database connection errors are especially common immediately after website migrations.
After moving WordPress to a new hosting server, verify:
- The database was imported successfully.
- The database name is correct.
- The database user exists.
- The database password is correct.
- The database user has access to the database.
DB_HOSTis correct.- The database server is accessible.
wp-config.phpcontains the new database information.
For example, if the new hosting account uses:
newaccount_wordpress
but wp-config.php still contains:
oldaccount_wordpress
WordPress will not connect to the correct database.
Step 16: Check the Table Prefix
The database table prefix is defined in wp-config.php.
For example:
$table_prefix = 'wp_';
Your database tables should normally use the corresponding prefix.
A custom installation might use:
$table_prefix = 'site_';
If the prefix was changed incorrectly, WordPress may not find its expected tables.
Do not change the table prefix unless you understand how WordPress database tables are structured.
Step 17: Check for a Recently Changed Plugin
Although plugins do not usually directly cause basic database connection errors, a plugin can create heavy database queries or contribute to database-related problems.
If the error appeared immediately after installing or updating a plugin, temporarily disable the plugin.
If you cannot access WordPress Admin, use the hosting File Manager or FTP.
Go to:
wp-content/plugins/
Temporarily rename the suspected plugin’s folder.
For example:
example-plugin
can be changed to:
example-plugin-disabled
Then test the website.
Step 18: Check the WordPress Debug Log
WordPress debugging can provide additional information.
Open:
wp-config.php
and temporarily use:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
Then check:
wp-content/debug.log
Look for errors generated at the same time the database problem occurred.
After troubleshooting, disable debugging on the live website:
define( 'WP_DEBUG', false );
Step 19: Check for Database Connection Limits
A database server can reject new connections when it reaches its configured connection limit.
This may happen when:
- Traffic suddenly increases
- Plugins generate excessive queries
- Long-running queries remain active
- Multiple applications share the database server
- Server resources are exhausted
If you manage your own VPS or dedicated server, inspect the database server’s connection and error logs.
On shared hosting, ask your hosting provider to check the database server and account limits.
Step 20: Look for Temporary Server Problems
Sometimes the problem is temporary.
If you have not changed anything and the website suddenly displays:
Error establishing a database connection
check whether:
- Your hosting provider is experiencing an outage
- The database server is restarting
- Server maintenance is underway
- Your hosting account has been suspended
- The server has reached resource limits
If the issue disappears without any configuration changes, monitor the website and investigate server logs if the problem returns.
Step 21: Restore a Working Backup
If the database has become severely corrupted and other recovery methods do not work, restoring a known-good backup may be necessary.
Ideally, you should have backups of:
- WordPress files
- WordPress database
- Uploaded media
- Configuration files
Choose a backup from a point when the website was known to work.
Before restoring:
- Preserve the current database if possible.
- Create a backup of the current state.
- Identify the approximate time the problem began.
- Restore the appropriate backup.
- Test the website.
- Investigate what caused the failure.
Do not immediately overwrite the only copy of your current database.
How to Troubleshoot the Error Efficiently
A systematic approach is much safer than randomly changing settings.
Use this sequence:
Database Connection Error
↓
Check Hosting Status
↓
Check Resource Usage
↓
Verify wp-config.php
↓
Check DB Name
↓
Check DB User
↓
Check DB Password
↓
Check DB Host
↓
Check User Permissions
↓
Test Database in phpMyAdmin
↓
Check Database Tables
↓
Check Error Logs
↓
Repair Database if Necessary
↓
Restore Backup if Required
Common Database Connection Error Scenarios
Scenario 1: Error After Changing the Database Password
If you recently changed the database user’s password, update:
define( 'DB_PASSWORD', 'new-password' );
in wp-config.php.
Test the website again.
Scenario 2: Error After Website Migration
Check the database name, username, password, and host in wp-config.php.
Also verify that the database has been imported and that the user has appropriate access.
Scenario 3: Error During a Traffic Spike
Check your hosting resource usage and database connection limits.
A temporary increase in traffic may expose limitations that were not noticeable under normal traffic.
Scenario 4: Error After Changing Hosting
Confirm that:
- DNS points to the correct server.
- The database exists on the new server.
wp-config.phpcontains the new database credentials.- The database user has access.
- The database server is accepting connections.
Scenario 5: Error Appears Randomly
Intermittent database connection errors can indicate:
- Server resource exhaustion
- Database server instability
- Connection limits
- Long-running queries
- Hosting problems
- Network problems with remote databases
Keep a record of when the errors occur and review server logs around those times.
How to Prevent WordPress Database Connection Errors
Maintain Regular Backups
Back up both your WordPress files and database.
For important websites, consider keeping backups in a separate location from the hosting server.
Use Reliable Hosting
Choose hosting with appropriate CPU, RAM, database resources, and monitoring for your website’s traffic.
Keep WordPress Updated
Keep WordPress, plugins, and themes updated and compatible.
Remove Unnecessary Plugins
Every additional plugin can introduce code and database activity.
Remove plugins you no longer use.
Monitor Resource Usage
Regularly review hosting resource statistics, especially for WooCommerce and high-traffic websites.
Avoid Unnecessary Database Changes
Do not manually modify or delete database tables unless you understand their purpose.
Test Major Changes
Use a staging environment before performing major WordPress, PHP, plugin, or database changes on a production website.
Monitor Database Size
A growing database is not automatically a problem, but unnecessary revisions, logs, transients, and plugin-generated data can increase database size over time.
Important wp-config.php Settings
A typical WordPress installation contains database configuration similar to:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );
These values must correspond to the actual database configuration on your server.
Do not copy database credentials from another WordPress installation unless they are actually configured for the current website.
What Not to Do
When troubleshooting a database connection error, avoid making destructive changes without a backup.
Do not:
- Delete the WordPress database
- Delete unknown database tables
- Randomly change database credentials
- Change
DB_HOSTwithout knowing the correct value - Remove
wp-config.php - Reinstall WordPress immediately
- Restore an old backup without preserving the current data
- Change database server settings without understanding their effects
A database may contain years of website content, customer information, orders, and configuration data.
WordPress Database Connection Error Checklist
Use this checklist when troubleshooting:
- Confirm the exact error message
- Test the frontend and
/wp-admin/ - Check hosting/server status
- Check available disk space
- Check CPU and RAM usage
- Open
wp-config.php - Verify
DB_NAME - Verify
DB_USER - Verify
DB_PASSWORD - Verify
DB_HOST - Check database user permissions
- Open the database in phpMyAdmin
- Check database tables
- Check PHP/server error logs
- Check the WordPress debug log
- Check for recent migrations or configuration changes
- Check database connection limits
- Repair damaged tables when appropriate
- Restore a known-good backup if necessary
- Disable temporary debugging after troubleshooting
Final Thoughts
The WordPress Database Connection Error means that WordPress cannot communicate with the database it needs to build the website. The cause can range from incorrect credentials in wp-config.php to database corruption, insufficient server resources, database connection limits, or a temporary hosting problem.
Start with the simplest checks: verify the database credentials, confirm the database host, check user permissions, and make sure the database server is available. If those settings are correct, investigate database health, server resources, logs, and recent configuration changes.
Most importantly, create a backup before performing database repairs or other potentially destructive operations. Regular backups, resource monitoring, reliable hosting, and careful website maintenance can greatly reduce the risk of prolonged database-related downtime.