How to fix “Error establishing a database connection”

WordPress cannot reach its database. Either the login details are wrong, the database server is down, or the database itself is damaged.

1–2 minutes
Assorted laptop and desktop hard drives

WordPress shows this when it cannot connect to its MySQL or MariaDB database. The cause is almost always one of three: wrong details in wp-config.php, the database server not running, or the database server out of memory.

1. Is the database server running?

sudo systemctl status mariadb
sudo systemctl status mysql

If it has stopped, start it and check why:

sudo systemctl start mariadb
sudo journalctl -u mariadb -n 50
sudo dmesg -T | grep -i 'killed process'

A killed mysqld or mariadbd means the server ran out of memory. Add memory or swap, or reduce what else is running. See how much RAM a site needs.

2. Are the login details right?

Open wp-config.php and find DB_NAME, DB_USER, DB_PASSWORD and DB_HOST. Test them directly:

mysql -u DB_USER -p -h localhost DB_NAME

If this fails, the details are wrong or the user lacks access. Reset the password as the database administrator:

sudo mysql -e "ALTER USER 'DB_USER'@'localhost' IDENTIFIED BY 'new-strong-password'; FLUSH PRIVILEGES;"

Then put the same password in wp-config.php. This often follows a migration where details were not updated.

3. Is DB_HOST right?

Usually localhost. On some hosts it is a separate server name or a socket path, given in the control panel. On a server where localhost fails but 127.0.0.1 works, MySQL is listening on TCP but not the expected socket.

4. Is the database damaged?

If you can log in to /wp-admin but the front end fails, or the error mentions corrupted tables:

wp db check
wp db repair

5. On shared hosting

Hosts limit how many database connections an account may open. Traffic spikes can briefly exhaust them. Check the control panel’s resource usage and ask the host if it happens often.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.