Seeing the plain white screen displaying “Error establishing a database connection” is one of the most common and alarming errors in WordPress administration. When this occurs, WordPress cannot communicate with its MySQL or MariaDB database, bringing your entire website, WooCommerce store, or membership portal offline.
Fixing this error requires verifying four components: database credentials in wp-config.php, whether the MySQL daemon is running on the host, user access permissions, and database table integrity.
Quick Answer
- Verify whether your database service is running on the server:
systemctl status mariadb || systemctl status mysql - Test database login credentials manually using credentials from
wp-config.php:mysql -u DB_USER -p'DB_PASSWORD' -h DB_HOST DB_NAME -e "SELECT option_value FROM wp_options WHERE option_name = 'siteurl';" - If the command fails with
Access denied, updatewp-config.phpwith the correct username or password. - If MySQL has crashed due to memory exhaustion, restart the service safely:
sudo systemctl restart mysql
Symptoms
- The frontend and
wp-admindashboard display: “Error establishing a database connection”. - The frontend displays the error, but navigating to
yourdomain.com/wp-admin/shows: “One or more database tables are unavailable. The database may need to be repaired.” - MySQL service status reports
failedorinactive (dead). - Server syslog shows Out of Memory (OOM) events terminating
mysqld.
Common Causes
- Incorrect Database Credentials:
DB_NAME,DB_USER,DB_PASSWORD, orDB_HOSTinwp-config.phpdo not match the active database privileges (frequently happens after migrating hosts). - Crashed MySQL/MariaDB Service: The database server ran out of physical RAM or hit disk capacity limits, forcing the Linux kernel to kill
mysqld. - Corrupted WordPress Database Tables: Sudden power loss, server crashes during writes, or unindexed concurrent queries corrupted
wp_postsorwp_options. - Incorrect Database Host Parameter:
DB_HOSTis set tolocalhostwhen the web host requires127.0.0.1, a custom port (localhost:3307), or a remote IP. - Connection Limits Exceeded: Maximum database connection pool exhausted (
max_connectionsreached).
Before You Start
- Take an immediate backup of your current
wp-config.phpfile before making manual edits:cp /var/www/html/wp-config.php /var/www/html/wp-config.php.bak - If you have command-line root access, never kill the database daemon with
kill -9while attempting recovery. - For enterprise mission-critical portals, explore our WordPress server support for 24/7 proactive database monitoring.
Step 1 — Check If MySQL/MariaDB Is Running
Log into your server terminal over SSH and verify the status of the database daemon:
# On Ubuntu/Debian:
systemctl status mysql
# On AlmaLinux/Rocky Linux/cPanel:
systemctl status mariadb
If the Database Is Active
If the output shows Active: active (running), your database engine is healthy. The issue is almost certainly invalid credentials or corrupted tables. Proceed to Step 2.
If the Database Has Crashed
If the output shows Active: failed or Active: inactive (dead):
sudo systemctl start mysql
Check why it crashed by inspecting the recent error log:
tail -n 40 /var/log/mysql/error.log || tail -n 40 /var/log/mariadb/mariadb.log
If the log shows Out of memory: Killed process (mysqld), see our guide on troubleshooting high CPU and memory usage.
Step 2 — Verify Database Credentials in wp-config.php
Open your website’s wp-config.php file located in the document root:
grep -E "DB_NAME|DB_USER|DB_PASSWORD|DB_HOST" /var/www/html/wp-config.php
Expected Definitions
define( 'DB_NAME', 'wp_store_prod' );
define( 'DB_USER', 'wp_dbuser' );
define( 'DB_PASSWORD', 'ComplexSecretPassword123!' );
define( 'DB_HOST', 'localhost' );
Now test these credentials directly from the command line:
mysql -u wp_dbuser -p'ComplexSecretPassword123!' -h localhost wp_store_prod -e "status"
- If connection succeeds: Credentials are correct. Proceed to Step 4 (Table Repair).
- If it returns
ERROR 1045 (28000): Access denied for user: The password or user grant is wrong. Proceed to Step 3. - If it returns
ERROR 2002 (HY000): Can't connect to local MySQL server through socket: The socket path is mismatched or the server is down.
Step 3 — Reset MySQL User Privileges and Password
If credentials failed, reset the database user password and grant full table permissions.
Log into MySQL as root:
sudo mysql
Run these SQL commands (replace placeholders with your actual details):
-- Ensure database exists
CREATE DATABASE IF NOT EXISTS wp_store_prod;
-- Update or create user with new password
CREATE USER IF NOT EXISTS 'wp_dbuser'@'localhost' IDENTIFIED BY 'ComplexSecretPassword123!';
ALTER USER 'wp_dbuser'@'localhost' IDENTIFIED BY 'ComplexSecretPassword123!';
-- Grant all privileges to the WordPress database
GRANT ALL PRIVILEGES ON wp_store_prod.* TO 'wp_dbuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Update wp-config.php with this exact password and test the website in your browser.
Step 4 — Repair Corrupted WordPress Database Tables
If your credentials are correct but yourdomain.com/wp-admin/ tells you that tables need repair, one or more database tables have suffered index or header corruption.
Method A: Built-in WordPress Repair Tool
Add this line to your wp-config.php file just above the /* That's all, stop editing! */ comment:
define('WP_ALLOW_REPAIR', true);
Save the file. Then open your browser and navigate to:
https://yourdomain.com/wp-admin/maint/repair.php
Click Repair Database (or Repair and Optimize Database).
WordPress will run table repairs across all core tables (wp_posts, wp_options, wp_users, etc.).
Important Security Step: Once the repair completes, immediately remove the
WP_ALLOW_REPAIRline fromwp-config.php. Leaving it enabled allows unauthorized visitors to trigger heavy repair scripts.
Method B: Repair via WP-CLI (Command Line)
If you have WP-CLI installed, repair all tables in seconds:
wp db repair --path=/var/www/html --allow-root
Step 5 — Verify wp_options SiteURL and Home Values
Sometimes a database connection error occurs when WordPress attempts to load options and encounters mismatched table prefixes or an incorrect siteurl.
Query the database directly using mysql:
mysql -u wp_dbuser -p'ComplexSecretPassword123!' -e "USE wp_store_prod; SELECT option_name, option_value FROM wp_options WHERE option_name IN ('siteurl', 'home');"
Verify that:
- The table prefix matches
$table_prefixinwp-config.php(default iswp_). - The URLs match your active domain with the correct HTTPS protocol.
Step 6 — Verify Resolution
Open your website in an incognito browser window and test:
- Frontend: The homepage loads with CSS and images intact.
- Backend: Log in to
/wp-admin/and verify dashboard access. - Database Write Test: Publish a draft post or update an option to verify that write operations succeed without timeouts.
Common Mistakes
- Setting
DB_HOSTto localhost on external database providers: On managed cloud hosting (like AWS RDS or DigitalOcean Managed Databases),DB_HOSTmust be the private endpoint DNS (e.g.,db-cluster.xyz.rds.amazonaws.com), notlocalhost. - Leaving whitespace or quotation typos in
wp-config.php: Copying passwords with accidental spaces or using curly/smart quotes (“ ”) instead of straight single quotes (' ') causes syntax errors. - Failing to investigate OOM killer crashes: Starting MySQL again without addressing root memory starvation ensures the database will crash again during peak traffic.
Prevention Checklist
- Add a Linux swapfile (minimum 2GB to 4GB) on small cloud VPS instances to protect the MySQL daemon from sudden OOM kills.
- Tune MySQL
innodb_buffer_pool_sizein/etc/mysql/my.cnfto fit available RAM. - Set up automated hourly database backups through our server backup management plans.
- Optimize heavy queries and transcient caches using Redis or Memcached object caching.
Quick Reference Commands
| Action | Command |
|---|---|
| Check MySQL status | systemctl status mysql |
| Check MariaDB status | systemctl status mariadb |
| Test WP database login | mysql -u <user> -p'<pass>' -h <host> <db> |
| Repair tables via WP-CLI | wp db repair --allow-root |
| Inspect MySQL error log | tail -f /var/log/mysql/error.log |
Frequently Asked Questions
Why does the error appear intermittently during high traffic?
Intermittent database connection errors mean the database is alive, but has reached its connection ceiling. MySQL rejects new incoming requests when active connections exceed max_connections (default is often 151). Increasing max_connections and configuring Redis persistent object caching resolves this.
Can a corrupted plugin cause a database connection error?
Yes. If a poorly written plugin executes unindexed recursive SQL queries that lock database tables, other processes will queue up until PHP database connection timeouts expire.
What should I do if MySQL will not start even after reboot?
Check disk space first with df -h. If the disk is 100% full, MySQL cannot write transaction logs and will refuse to start. See our guide on fixing No Space Left on Device.
How can ServerCare360 assist with WordPress database stability?
Our WordPress server support and emergency server support engineers configure high-performance database caching, tune MariaDB buffer pools, monitor query concurrency 24/7, and provide immediate incident response when databases stall.