How to Fix WordPress Database Connection Error
The WordPress Database Connection Error is one of the most common problems that can make a website completely inaccessible. Instead of loading the website, visitors may see a message such as “Error establishing a database connection.”
WordPress stores important information—including posts, pages, users, settings, and other website data—in a database. When WordPress cannot communicate with that database, the website cannot load its content correctly.
The good news is that this error is usually fixable. The cause may be incorrect database credentials, a database server outage, corrupted tables, insufficient hosting resources, or configuration problems.
This guide explains how to diagnose and fix a WordPress database connection error step by step.
What Causes a WordPress Database Connection Error?
Common causes include:
- Incorrect database name
- Incorrect database username or password
- Incorrect database hostname
- Corrupted database tables
- Database server downtime
- Too many database connections
- Insufficient hosting resources
- Incorrect
wp-config.phpsettings - Problems after migrating a website
- Incorrect database user permissions
- Damaged WordPress files
- Server or MySQL/MariaDB configuration problems
Before making major changes, create a complete backup of your WordPress files and database if you still have access to them.
1. Check Whether the Error Affects the Entire Website
First, determine whether the problem affects:
- The entire website
- Only the WordPress dashboard
- Specific pages
- The database administration tool
Try opening:
https://example.com/
and:
https://example.com/wp-admin/
If both return a database connection error, the issue is likely related to the database configuration, server, or database service.
If only certain functionality is affected, investigate plugins, themes, or specific database queries.
2. Check Your Database Credentials
The first file to inspect is:
wp-config.php
This file contains the database connection details used by WordPress.
Look for:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );
Verify each value against the database information provided by your hosting account.
The four important settings are:
DB_NAMEDB_USERDB_PASSWORDDB_HOST
A single incorrect character can prevent WordPress from connecting to the database.
Important
Do not share your database password publicly or paste it into support forums.
3. Verify the Database Name
Log in to your hosting control panel and open the database management section.
Depending on your hosting environment, you may find databases under:
- cPanel → MySQL Databases
- DirectAdmin → MySQL Management
- Hosting control panel → Databases
Compare the database name with:
define( 'DB_NAME', 'database_name' );
Some hosting providers automatically add an account prefix to database names.
For example, the actual database might be:
accountname_wordpress
while wp-config.php contains:
wordpress
In that situation, WordPress will not be able to connect.
4. Verify the Database Username
Check that the username in wp-config.php matches an actual database user.
For example:
define( 'DB_USER', 'accountname_wpuser' );
Then check your hosting control panel to make sure that this user exists.
A database user existing by itself is not enough. It must also be assigned to the correct database.
5. Verify the Database Password
An incorrect database password is another common cause.
If you recently changed the database user’s password in your hosting control panel, update the password in wp-config.php.
For example:
define( 'DB_PASSWORD', 'your_database_password' );
Make sure there are no accidental spaces or incorrect characters.
If you are unsure about the password, reset the database user’s password through your hosting control panel and update wp-config.php with the new value.
6. Check the Database Host
Most WordPress installations use:
define( 'DB_HOST', 'localhost' );
However, this is not universal.
Some hosting providers use a different database hostname, such as a dedicated database server or internal hostname.
If your hosting provider gives you a specific database host, use that value instead of assuming localhost.
Contact your hosting provider if you are unsure.
7. Test the Database User’s Permissions
The database user must have appropriate permissions for the WordPress database.
In your hosting control panel, verify that the database user is assigned to the correct database.
Depending on the hosting panel, you may see an option such as:
Add User to Database
or:
Assign User to Database
Make sure the user has the permissions required by your WordPress installation.
If the user was accidentally removed from the database, WordPress may immediately begin showing a database connection error.
8. Check Whether the Database Server Is Running
Sometimes the problem has nothing to do with WordPress.
Your MySQL or MariaDB service may be unavailable because of:
- Server maintenance
- Service failure
- High server load
- Resource exhaustion
- Database crashes
- Hosting infrastructure problems
If you have server access, check the database service.
For example, on a Linux server using systemd, you can check the service status with:
systemctl status mysql
or:
systemctl status mariadb
The exact service name depends on your server configuration.
If you use managed hosting and do not have root access, contact your hosting provider and ask whether the MySQL/MariaDB service is operational.
9. Check Your Hosting Resource Usage
A WordPress database may become unavailable when the hosting account reaches its resource limits.
Check your hosting panel for:
- CPU usage
- RAM usage
- Disk space
- Database connections
- PHP workers
- Entry processes
- I/O usage
A website experiencing a sudden traffic spike can also exhaust available resources.
If the server is repeatedly reaching its limits, simply restarting the database may only provide a temporary solution.
10. Check Available Disk Space
A server with a full filesystem can cause database problems.
Check whether the server has enough free disk space.
Pay particular attention to:
- Database storage
- Website files
- Server logs
- Temporary directories
- Backup files
If disk space is completely exhausted, remove unnecessary files safely and investigate what caused the disk to fill up.
Do not delete system files or database files manually unless you know exactly what they contain.
11. Repair the WordPress Database
If your database credentials are correct but WordPress still cannot access certain tables, the database may be corrupted.
WordPress provides a built-in database repair feature.
Open:
wp-config.php
Add:
define( 'WP_ALLOW_REPAIR', true );
Then visit:
https://example.com/wp-admin/maint/repair.php
You may see options such as:
- Repair Database
- Repair and Optimize Database
After completing the repair, remove the WP_ALLOW_REPAIR line from wp-config.php.
Important
The repair page does not require normal WordPress authentication, so do not leave it enabled after troubleshooting.
12. Check the Database With phpMyAdmin
Most hosting environments provide phpMyAdmin or another database management tool.
Open the database associated with your WordPress installation.
Look for tables such as:
wp_posts
wp_users
wp_options
wp_comments
wp_terms
Your table prefix may be different from wp_.
If phpMyAdmin can open the database normally, the database server is likely responding, and the problem may instead be related to WordPress configuration or permissions.
If phpMyAdmin also reports database server errors, investigate the database service or hosting environment.
13. Repair Corrupted Tables in phpMyAdmin
If specific WordPress tables appear corrupted, phpMyAdmin may provide a repair option for supported table types.
Steps
- Open phpMyAdmin.
- Select the WordPress database.
- Select the affected tables.
- Choose Repair table from the available operations.
Create a backup before making database repairs.
Do not randomly modify or delete tables because doing so can cause permanent data loss.
14. Check the Database Table Prefix
WordPress normally uses a table prefix such as:
$table_prefix = 'wp_';
But many websites use a custom prefix.
For example:
$table_prefix = 'site_';
Make sure the prefix in wp-config.php matches the tables actually present in your database.
If the database contains:
site_posts
site_options
site_users
but wp-config.php contains:
$table_prefix = 'wp_';
WordPress will look for the wrong tables.
15. Check for Database Problems After Migration
Database connection errors are particularly common after moving WordPress from one server to another.
After a migration, verify:
- Database name
- Database username
- Database password
- Database hostname
- Database user permissions
- Table prefix
- Database import
- PHP version
- MySQL/MariaDB availability
Make sure the database was imported completely.
A partially imported database can cause missing-table or database-query errors.
16. Check the WordPress Database Credentials After Changing Hosting
If you recently changed hosting providers, your old database details may no longer work.
For example, your previous server may have used:
DB_HOST = localhost
while the new server may require another database host.
Likewise, the database name and username may have changed during migration.
Update wp-config.php using the credentials provided by the new hosting environment.
17. Check for Too Many Database Connections
A high-traffic WordPress website can sometimes reach the database server’s connection limit.
You may see errors related to:
Too many connections
Possible causes include:
- Sudden traffic spikes
- Poorly optimized plugins
- Inefficient database queries
- Insufficient server resources
- Incorrect database configuration
If this happens regularly, investigate slow queries, plugins, caching, database configuration, and server capacity instead of simply increasing connection limits.
18. Check WordPress Debug Logs
If the connection appears intermittent or occurs only during certain actions, enable WordPress logging.
In wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
Then check:
/wp-content/debug.log
Look for messages related to:
- Database queries
- Connection failures
- Missing tables
- SQL errors
- Plugin failures
- Theme errors
Disable debugging after troubleshooting on a production website.
19. Check MySQL or MariaDB Error Logs
If you have root access to the server, database logs can provide more information than WordPress logs.
Depending on your server configuration, check the MySQL or MariaDB error log for:
- Database crashes
- Corrupted tables
- Authentication failures
- Storage problems
- Connection limits
- InnoDB errors
The exact log location varies by Linux distribution and database configuration.
20. Check Recent Plugin or Theme Changes
A plugin or theme can trigger database errors by running invalid or resource-intensive queries.
If the problem began immediately after installing or updating a plugin:
- Disable the plugin.
- Test the website.
- Check the database logs.
- Update the plugin if a newer compatible release exists.
- Contact the plugin developer if the issue continues.
If you cannot access the dashboard, temporarily rename the plugins directory through File Manager or FTP.
21. Check Database Configuration on Managed WordPress Hosting
Managed WordPress hosting may use a database service that is separate from the web server.
In this environment, you may not have access to MySQL or MariaDB configuration files.
Instead, check:
- Hosting status
- Database credentials
- Database hostname
- Resource usage
- Provider outage notices
If everything in wp-config.php is correct, contact your hosting provider and ask them to test the database connection from the web server.
22. Restore the Database From a Backup
If the database has become seriously corrupted and repair attempts fail, restoring a known-good backup may be necessary.
Before restoring:
- Create a backup of the current database if possible.
- Identify the latest known-good backup.
- Confirm that it contains the required WordPress tables.
- Restore the database.
- Verify the database credentials.
- Test the website.
If the database is actively receiving new orders, comments, registrations, or other important data, be especially careful before restoring an older backup because newer data may be lost.
23. Common Error Messages and Their Meaning
Error establishing a database connection
Usually indicates that WordPress cannot connect to MySQL/MariaDB.
Check:
- Database credentials
- Database hostname
- Database service
- User permissions
Access denied for user
Usually indicates incorrect credentials or insufficient database permissions.
Check the database username and password.
Unknown database
The database name specified in wp-config.php does not exist or cannot be accessed.
Too many connections
The database server has reached its connection limit.
Investigate traffic, queries, plugins, and server resources.
Table doesn’t exist
A required WordPress table may be missing or the table prefix may be incorrect.
MySQL server has gone away
The database connection may have been interrupted because of server resources, timeouts, packet limits, or database service problems.
24. Quick Troubleshooting Checklist
Use this checklist when WordPress displays a database connection error:
☐ Check whether the entire website is affected
☐ Verify DB_NAME
☐ Verify DB_USER
☐ Verify DB_PASSWORD
☐ Verify DB_HOST
☐ Check database user permissions
☐ Check whether MySQL/MariaDB is running
☐ Check hosting resource usage
☐ Check available disk space
☐ Verify the table prefix
☐ Open the database in phpMyAdmin
☐ Check for corrupted tables
☐ Review WordPress debug.log
☐ Review MySQL/MariaDB error logs
☐ Check recent plugin/theme changes
☐ Check migration settings
☐ Restore a backup if necessary
How to Prevent Database Connection Errors
Regular maintenance can significantly reduce database-related problems.
Keep Reliable Backups
Maintain automated backups of both:
- WordPress files
- WordPress database
Test your backups periodically to make sure they can actually be restored.
Monitor Server Resources
Monitor CPU, RAM, disk space, database connections, and other hosting limits.
Keep WordPress Updated
Use supported versions of:
- WordPress
- PHP
- Plugins
- Themes
- MySQL/MariaDB
Avoid Poorly Maintained Plugins
Plugins that generate inefficient database queries can cause performance problems as a website grows.
Use reputable, actively maintained plugins and remove plugins you no longer need.
Optimize the Database Carefully
Remove unnecessary data such as:
- Expired transients
- Old revisions
- Spam comments
- Unused plugin data
However, always back up the database before performing database cleanup.
Conclusion
The WordPress Database Connection Error generally occurs when WordPress cannot communicate correctly with its MySQL or MariaDB database.
The first things to check are the database credentials in wp-config.php, database user permissions, database hostname, and whether the database service is running. If those settings are correct, investigate corrupted tables, server resources, database connection limits, and recent website changes.
For migrated websites, carefully verify every database setting because even a small configuration difference between hosting environments can prevent WordPress from connecting.
With regular backups, reliable hosting, updated software, and proper monitoring, database connection problems can be diagnosed quickly and their impact minimized.
