How to Fix “Error Establishing a Database Connection” in WordPress

How to Fix “Error Establishing a Database Connection” in WordPress

The “Error Establishing a Database Connection” message means WordPress cannot connect to your MySQL database — the database that stores all your posts, pages, settings, user accounts, and (if you run WooCommerce) your orders and customer data. The most common causes are incorrect database credentials in wp-config.php, a corrupted database that needs repair, or a database server that is down or overloaded. Most cases can be resolved in under 20 minutes.

This error is particularly alarming because it affects your entire site — every page, every post, the admin dashboard, everything. Unlike errors that affect specific pages, a database connection failure takes down everything that depends on the database, which in WordPress means essentially the entire site.

What Causes This Error?

Incorrect database credentials. The username, password, database name, or host specified in your wp-config.php file does not match what your database server expects. This can happen after a hosting migration, a password change, or a manual edit to wp-config.php that introduced a typo.

Corrupted database. One or more database tables have become corrupted — damaged by a server crash, an interrupted write operation, a disk failure, or a plugin that wrote malformed data. WordPress cannot read the corrupted tables and fails to connect.

Database server is down. The MySQL or MariaDB server that hosts your database is not running. This can happen due to a server crash, a hosting provider maintenance window, or resource exhaustion on a shared hosting server.

Database server is overloaded. On shared hosting, your database server is shared with many other sites. If another site on the same server is consuming excessive database resources, your connections may be refused or timed out.

Exceeded connection limits. Every hosting plan has a maximum number of simultaneous database connections. If your site receives a traffic spike or a plugin opens excessive connections, you can hit this limit.

Step 1: Verify Your Database Credentials

The most common cause of this error is incorrect database credentials. Open your wp-config.php file (located in your WordPress root directory) via FTP, SFTP, or your hosting file manager. Look for these four lines:

define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_username');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');

Verify each value matches what your hosting provider expects. You can find the correct values in your hosting control panel — typically under “MySQL Databases” or “Database Management.” Common issues include a database password that was changed in the hosting panel but not updated in wp-config.php, a database username that was deleted or modified, a database name with a typo (watch for underscores and case sensitivity), and a DB_HOST value that should be something other than “localhost” — some hosts use an IP address, a hostname like “mysql.yourdomain.com,” or a socket path.

If you find and correct a mismatch, save wp-config.php and reload your site.

Step 2: Check If the Error Affects the Admin Area

Try loading your WordPress admin at yourdomain.com/wp-admin/. If the admin loads but the front end shows the database error, the issue may be related to your site URL settings in the database — a different problem that is not actually a database connection failure.

If you see a message saying “One or more database tables are unavailable. The database may need to be repaired” when accessing wp-admin, proceed to Step 3.

Step 3: Repair the Database

WordPress has a built-in database repair tool. To enable it, add this line to your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Then visit yourdomain.com/wp-admin/maint/repair.php in your browser. You do not need to be logged in to access this page. Click “Repair Database” for a basic repair, or “Repair and Optimize Database” for a more thorough fix. WordPress will attempt to repair all corrupted tables.

After the repair completes, reload your site. If it works, remove the WP_ALLOW_REPAIR line from wp-config.php — leaving it enabled is a security risk because anyone can access the repair page.

If the built-in tool cannot repair the database, you can try using phpMyAdmin (available in most hosting control panels). Open phpMyAdmin, select your WordPress database, select all tables, and choose “Repair table” from the dropdown. This uses MySQL’s native repair command, which can sometimes fix issues the WordPress tool cannot.

Step 4: Check If the Database Server Is Running

If your credentials are correct and the database is not corrupted, the MySQL server itself may be down.

On shared hosting: You cannot restart MySQL yourself. Log in to your hosting control panel and check for any server status notifications or maintenance alerts. Contact your host’s support team and report the database error — they can check the MySQL server status and restart it if necessary.

On a VPS or dedicated server: If you have SSH access, check the MySQL service status with sudo systemctl status mysql (or mariadb on MariaDB systems). If it is stopped, restart it with sudo systemctl restart mysql. Check the MySQL error log at /var/log/mysql/error.log to understand why it stopped — common causes include running out of disk space, running out of memory, or a crash caused by a corrupted table.

Step 5: Check for Exceeded Connection Limits

If the error appears intermittently — sometimes your site works fine, sometimes it shows the database error — you may be hitting your hosting plan’s maximum database connection limit. This commonly happens during traffic spikes, when plugins open multiple simultaneous database connections, or when a poorly written plugin does not close connections properly.

Check your hosting control panel for database connection usage statistics. If you are consistently near or at the limit, consider upgrading your hosting plan, installing an object caching solution like Redis (which reduces database queries), or identifying and replacing plugins that create excessive database connections.

Step 6: Restore from Backup

If none of the above steps resolve the issue — if the database is severely corrupted, if critical tables are missing, or if the cause is unclear and you need your site back online immediately — restore from your most recent backup.

If you use a backup service, contact your provider (or if you are on a WP Ministry care plan, our team) to restore the database to the last known good state. If you use a backup plugin like UpdraftPlus, access the plugin’s backup storage and restore the database component. If you have a backup through your hosting provider, use their restoration tool.

This is why daily automated backups with off-site storage are non-negotiable. Without a backup, a corrupted database can mean permanent data loss. Read our guide on setting up automatic WordPress backups.

Step 7: Contact Your Hosting Provider

If you have verified your credentials, attempted repair, confirmed the MySQL server is running, and still see the error — the issue is likely at the hosting infrastructure level. Contact your hosting provider’s support team with the following information: the exact error message, when the error started, what you have already tried, and your site URL. They can investigate server-level issues that are not visible or accessible from your side.

How to Prevent This Error

Keep daily backups. Our backup service creates daily backups of your database and files, stored off-site, so you can restore to any point in the last 30–120 days.

Use a quality hosting provider. Shared hosting with oversold servers is the most common environment for database connection issues. Managed WordPress hosts or VPS plans provide more reliable database performance.

Monitor your site continuously. Our maintenance service checks your site every 60 seconds. If a database error occurs, our team is alerted immediately and begins investigation — often before you even know there is a problem.

Optimise your database regularly. Removing unnecessary post revisions, cleaning expired transients, and optimising table structures keeps your database lean and reduces the likelihood of corruption. Read our database optimization guide.

Frequently Asked Questions

Can I lose my data from this error?

The error itself does not delete data. However, the underlying cause — database corruption, server disk failure — can involve data damage. If your database tables are corrupted beyond repair and you do not have a backup, you can lose the data stored in those tables. This is why automated daily backups are critical.

Why does this error appear after a hosting migration?

Hosting migrations frequently trigger this error because the database credentials (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST) change when you move to a new host. After migration, wp-config.php must be updated with the new host’s credentials. Our migration service handles this automatically.

The error appeared after I edited wp-config.php. How do I undo it?

If you accidentally introduced an error while editing wp-config.php, restore the file from your backup. If you do not have a backup of the file, carefully review your recent changes — look for missing semicolons, unclosed quotes, or incorrect values. Compare against a fresh copy of wp-config-sample.php from a WordPress download to verify the correct format.

The error is intermittent — my site goes up and down. What is happening?

Intermittent database errors are usually caused by an overloaded database server (common on shared hosting), exceeded connection limits during traffic spikes, or a slow query from a plugin that temporarily blocks other connections. Check your hosting resource usage and consider upgrading to a plan with more database resources.

Need Expert Help? Let WP Ministry Handle It

Database errors are stressful because they affect your entire site and can involve your most valuable data. If you are uncomfortable troubleshooting database issues yourself, or if you need your site back online immediately, our 24/7 team can help.

Call (901) 249-0909 for immediate assistance, or submit an emergency support request. One-time fixes start at $199. Or choose a care plan starting at $35/month and let our team monitor, maintain, and protect your WordPress database around the clock.

View our care plans →

Related Articles

How to Fix the WordPress White Screen of Death (Step by Step)

How to Fix WordPress 500 Internal Server Error (2026 Guide)

How to Optimise Your WordPress Database for Speed

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment