Skip to main content
wordpress Beginner Level 9 min read

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

A complete step-by-step troubleshooting guide to diagnose and fix WordPress database connection errors. Verify wp-config.php credentials, repair corrupted MySQL tables, and restart crashed MariaDB daemons.

SC
ServerCare360 Systems Team
Senior WordPress Infrastructure Engineer
Published: Sep 18, 2026

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

  1. Verify whether your database service is running on the server:
    systemctl status mariadb || systemctl status mysql
  2. 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';"
  3. If the command fails with Access denied, update wp-config.php with the correct username or password.
  4. If MySQL has crashed due to memory exhaustion, restart the service safely:
    sudo systemctl restart mysql

Symptoms

  • The frontend and wp-admin dashboard 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 failed or inactive (dead).
  • Server syslog shows Out of Memory (OOM) events terminating mysqld.

Common Causes

  1. Incorrect Database Credentials: DB_NAME, DB_USER, DB_PASSWORD, or DB_HOST in wp-config.php do not match the active database privileges (frequently happens after migrating hosts).
  2. Crashed MySQL/MariaDB Service: The database server ran out of physical RAM or hit disk capacity limits, forcing the Linux kernel to kill mysqld.
  3. Corrupted WordPress Database Tables: Sudden power loss, server crashes during writes, or unindexed concurrent queries corrupted wp_posts or wp_options.
  4. Incorrect Database Host Parameter: DB_HOST is set to localhost when the web host requires 127.0.0.1, a custom port (localhost:3307), or a remote IP.
  5. Connection Limits Exceeded: Maximum database connection pool exhausted (max_connections reached).

Before You Start

  • Take an immediate backup of your current wp-config.php file 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 -9 while 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_REPAIR line from wp-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:

  1. The table prefix matches $table_prefix in wp-config.php (default is wp_).
  2. 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:

  1. Frontend: The homepage loads with CSS and images intact.
  2. Backend: Log in to /wp-admin/ and verify dashboard access.
  3. Database Write Test: Publish a draft post or update an option to verify that write operations succeed without timeouts.

Common Mistakes

  1. Setting DB_HOST to localhost on external database providers: On managed cloud hosting (like AWS RDS or DigitalOcean Managed Databases), DB_HOST must be the private endpoint DNS (e.g., db-cluster.xyz.rds.amazonaws.com), not localhost.
  2. 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.
  3. 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_size in /etc/mysql/my.cnf to 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

ActionCommand
Check MySQL statussystemctl status mysql
Check MariaDB statussystemctl status mariadb
Test WP database loginmysql -u <user> -p'<pass>' -h <host> <db>
Repair tables via WP-CLIwp db repair --allow-root
Inspect MySQL error logtail -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.

Was this technical guide helpful?
SC
ServerCare360 Systems Team Author
Senior WordPress Infrastructure Engineer

Specializing in enterprise WordPress architecture, database optimization, and high-concurrency hosting.

Production Standards Verified by Lead Database Administrator
Keep Troubleshooting & Reading

Related Troubleshooting Guides

Explore All Guides
24/7 Managed Server Administration

Need Certified Engineers to Manage this Stack?

ServerCare360 provides proactive monitoring, zero-downtime migrations, and rapid SLA incident response.

View All Services
Infrastructure Support

Require Proactive Infrastructure Monitoring & Support?

Prevent recurring outages, high load spikes, and backup failures with our 24/7 remote server administration.