DB connection 📝 WordPress

WordPress: Error Establishing a Database Connection

WordPress can’t connect to its MySQL/MariaDB database with the credentials in wp-config.php, or the database server is down.

Seen on: WordPress MySQL PHP

Meaning

Every WordPress page needs the database. This error appears when the DB host is unreachable, credentials are wrong, the database doesn’t exist, or the server has run out of connections or memory.

Common causes

  • Wrong DB_NAME, DB_USER, DB_PASSWORD or DB_HOST in wp-config.php
  • MySQL/MariaDB service stopped or crashed (often out of memory)
  • Database user lacks privileges on the database
  • Too many connections / host resource limits on shared hosting
  • Corrupted tables (admin may show “One or more database tables are unavailable”)
  • Site migrated without updating credentials or host

⚡ Quick fix

  1. Verify the four DB constants in wp-config.php against your hosting panel
  2. Restart MySQL/MariaDB and check its error log
  3. Test the credentials with a tiny PHP script or the mysql CLI
  4. Repair tables: add define('WP_ALLOW_REPAIR', true); and visit /wp-admin/maint/repair.php (remove afterwards)

Detailed fix by platform

WordPress

  1. wp-config.php values to check:
    php
    define('DB_NAME', 'wp_database');
    define('DB_USER', 'wp_user');
    define('DB_PASSWORD', 'correct-password');
    define('DB_HOST', 'localhost');   // some hosts need 127.0.0.1, a socket, or a remote hostname

MySQL

  1. sudo systemctl status mysql and sudo journalctl -u mysql. If it was OOM-killed, reduce innodb_buffer_pool_size or add swap.

Code examples

Test credentials from the server

bash
mysql -h localhost -u wp_user -p wp_database -e "SELECT 1;"

How to diagnose

  1. Credentials — Do wp-config values match?
  2. Service — Is the DB server running?
  3. Privileges — Does the user have access?
  4. Resources — Memory/connection limits hit?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.