TR EN

Fix WordPress 'Error Establishing a Database Connection' Error

The WordPress 'Error Establishing a Database Connection' occurs when the PHP runtime cannot authenticate or connect to the MySQL/MariaDB database daemon.

Root Causes & Failure Triggers

The WordPress 'Error Establishing a Database Connection' occurs when the PHP runtime cannot authenticate or connect to the MySQL/MariaDB database daemon.

  • Invalid database credentials (DB_NAME, DB_USER, DB_PASSWORD) inside wp-config.php.
  • MySQL/MariaDB daemon is stopped due to Linux OOM (Out Of Memory) killer.
  • Corrupted database tables preventing query execution.
  • Missing user privileges on the MySQL target database.

Step-by-Step Fixes

1. Check and Restart MySQL/MariaDB Service

Verify the database daemon status via terminal:

sudo systemctl status mysql
sudo systemctl restart mysql

2. Test Database Credentials via Command Line

Verify that user credentials match MySQL authorization tables:

mysql -u DB_USER -p DB_NAME

3. Trigger WordPress Native Database Repair

Add the following constant to wp-config.php:

define('WP_ALLOW_REPAIR', true);

Then visit https://yourdomain.com/wp-admin/maint/repair.php in your browser.

4. Switch DB_HOST to 127.0.0.1

Change DB_HOST from 'localhost' to '127.0.0.1' to bypass broken Unix socket routing.
💡 Live Diagnostics: Verify your server's SSL chain, DNS records, and HTTP headers using our SSL Checker, Security Headers, or Open Port Scanner tools.

Related Error Troubleshooting Guides

How to Fix Laravel 419 Page Expired (CSRF Token Mismatch)Fix Nginx 502 Bad Gateway Error with PHP-FPM / UpstreamHow to Fix CORS Error: No 'Access-Control-Allow-Origin' HeaderHow to Fix WordPress 500 Internal Server Error (.htaccess & Memory)How to Fix ERR_SSL_PROTOCOL_ERROR in Chrome & BrowsersFix ERR_TOO_MANY_REDIRECTS (Redirect Loop 310) in WordPress & CloudflareHow to Fix Nginx 504 Gateway Timeout Error (proxy_read_timeout)How to Fix SSH Connection Refused (Port 22) on Ubuntu/Linux

Frequently Asked Questions

Why is wp-admin accessible while the homepage fails?
If wp-admin displays 'One or more tables are unavailable', specific core tables are corrupt and need database repair.
Should I remove WP_ALLOW_REPAIR after fixing the site?
Yes. Leaving WP_ALLOW_REPAIR active allows anyone on the internet to trigger database repairs, creating a security risk.