Database Connection Error; When WordPress Suddenly Goes Offline
Your site was working fine five minutes ago. Now, instead of the homepage, you see a plain white sentence: Error establishing a database connection. Your visitors see exactly the same thing. Not a 404 page, not a 500 error, not a page saying "we'll be back soon." Just this sentence.
This error means WordPress cannot access MySQL. Not that the database is necessarily corrupted. In eight out of ten cases, the problem lies elsewhere. Let's go through the five real causes in order of likelihood.
First Test: Is the MySQL Service Actually Running?
Log in via SSH and run this command:
systemctl status mysql
If the service is inactive, check its status:
systemctl start mysql
systemctl enable mysql
Here's where people make a mistake: many don't re-enable the MySQL service after a server restart. They don't run enable, and the site works until the next server restart. After the restart, the error returns. If the service is running but the error persists, check the log:
tail -50 /var/log/mysql/error.log
Look for the phrases Out of memory or Too many connections. Both have clear meanings: the server doesn't have enough RAM, or the number of concurrent connections has exceeded the limit.
Testing the Connection with a Short Script
Before tampering with wp-config.php, run a direct test. Create a file named db-test.php in the site's root directory:
<?php
$conn = @new mysqli('localhost', 'DB_USER', 'DB_PASSWORD', 'DB_NAME');
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
echo 'Connected successfully';
$conn->close();
?>
Take the values for DB_USER, DB_PASSWORD, and DB_NAME from the wp-config.php file. Now navigate to https://yourdomain.com/db-test.php in your browser.
Two scenarios can occur. If you see the message Connected successfully, the connection is established and the problem lies elsewhere. If you see the error Access denied for user, the username or password is incorrect. This is the simplest case, and the solution is simple too: fix the password.
But if you see the error Unknown database, the database doesn't exist. This usually happens after migrating a site or restoring from a backup. Open the wp-config.php file and carefully check the database name. An extra space or a lowercase/uppercase letter can trigger this error.
Is the Problem in wp-config.php or in MySQL Itself?
Now that you've run the test script, you know where the problem is. If the connection works with the script but WordPress still throws an error, three possibilities remain.
1. Duplicate Definitions in wp-config.php
Some caching or security plugins add database definitions to the wp-config.php file. If define('DB_NAME', ...) appears twice in the file, PHP only reads the first one. Open the file and look for duplicate definitions. Remove any that are redundant.
2. Incorrect Database Host
The DB_HOST value is usually localhost. But some hosting services use a separate address. If you've migrated the site from one host to another, check this value. Get the new address from your new host's control panel. In the ServerNet documentation and knowledge base, we've provided a table of default values for each hosting type.
3. Corrupted Database Tables
This is the scenario everyone fears. If the test script establishes a connection and wp-config.php is intact, the likelihood of corrupted tables increases. When WordPress cannot access the wp_options table, it shows this exact error. No other error, no further explanation. Just this sentence.
Repairing a Corrupted Table; The Command That Saves Your Site
MySQL has a built-in repair tool. Log in via SSH and run this command:
mysqlcheck -u root -p --auto-repair --optimize --all-databases
This command checks all databases, repairs corrupted tables, and optimizes healthy ones. If you only want to check the WordPress database:
mysqlcheck -u root -p --auto-repair your_database_name
Read the output. If a table encounters the error Table is marked as crashed, that's where the problem is. The command above usually repairs it. If it doesn't get repaired, you'll need to use a backup. Here's where people make a mistake: some don't check the site after the repair and assume the job is done. A repaired table might get corrupted again. If the error returns within 24 hours, it's a hardware issue, and you should check the disk.
Why Does This Error Appear Suddenly?
The database connection error usually appears all at once, not gradually. Consider three common scenarios:
- Sudden server restart: If the server restarts due to high RAM usage, MySQL might not start up properly. Check the
/var/log/sysloglog for the phraseOut of memory. - Disk full: When the disk is full, MySQL cannot write and drops the connection. Check available space with
df -h. If it's below 5%, this is the problem. - Attack or unusual traffic: If the number of concurrent connections exceeds
max_connections, MySQL rejects new connections. Check the current value with this command:
mysql -u root -p -e "SHOW VARIABLES LIKE 'max_connections';"
The default value is usually 151. If your site genuinely needs more than this, you'll need to change the value in /etc/mysql/mysql.conf.d/mysqld.cnf. But first, make sure the problem isn't from heavy queries rather than the number of connections.
When Everything Seems Fine But the Error Persists
Consider a scenario where the service is running, the test script establishes a connection, wp-config.php is intact, and the tables have been repaired. But the error is still there. This situation is rare, but it happens.
The usual culprit is cache. WordPress or a caching plugin has stored the result of the previous error in memory. Clear the cache. If you're using Redis or Memcached, restart the service:
systemctl restart redis-server
systemctl restart memcached
If the error persists, check DNS. Sometimes your site works correctly on the server, but visitors are directed to a different server. Use dig yourdomain.com to see the IP address and compare it with your server's IP. Read the complete guide to DNS configuration for a new site in this article.
Prevention; What You Should Do Now
You've experienced this error and don't want it to happen again. Do three things:
- Automated database backups: If you don't have a backup, set one up now. Not next week—now. A simple cron job that backs up the database every night is sufficient.
- Automated monitoring: Don't wait for users to tell you the site is down. Set up an uptime monitoring service that checks the site every 5 minutes and emails you if it detects an error. Read the practical guide to website uptime monitoring in this article.
- Keep error documentation: Every time you resolve this error, write down the cause and solution. Next time, you'll fix it ten minutes sooner.
If your server doesn't have sufficient resources and you frequently encounter this error, it might be time to upgrade. ServerNet's WordPress hosting with dedicated resources and automated monitoring eliminates this category of errors. But before taking any action, run the tests in this article. Eight times out of ten, the problem is solved with a simple command.
Frequently Asked Questions
What does the "Error establishing a database connection" mean?
It means WordPress cannot connect to the MySQL database. This error usually doesn't mean data loss; rather, it indicates a disruption in the connection between WordPress and MySQL. The cause could be a stopped service, incorrect password, or a corrupted table.
How can I fix the WordPress database connection error without SSH access?
If you don't have SSH access, use your hosting control panel. Most panels like cPanel or DirectAdmin have a Restart MySQL option. If not, contact your hosting support and ask them to restart the MySQL service. After that, clear your site's cache.
Does a corrupted database table cause data loss?
No. A corrupted table doesn't mean data loss. The mysqlcheck --auto-repair command usually repairs the table without losing data. Only in rare cases where the disk is damaged might data be lost. That's why regular backups are essential.
Why does this error appear after migrating the site to a new host?
It's usually due to an error in wp-config.php. The database name, username, or password is different on the new host. The DB_HOST value might also have changed. Update the wp-config.php file with the new host's values, and if you're unsure, ask your new host's support for the exact values.
Comments 0
No comments yet — be the first!