Short answer: The WordPress message Error establishing a database connection means PHP could not log in to the MySQL or MariaDB database. Fix it by checking DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php, testing them with the mysql client, then confirming the database server is running, the disk has space and connections are not exhausted.
Key takeaways
- WordPress reads four constants from wp-config.php, described in the wp-config.php documentation .
- This is the step that saves the most time.
- Error 1045 with correct-looking credentials usually means the user does not exist for that host, has a different password, or lacks rights on the database.
- If the error appears only at busy times, the server may be reaching max_connections .
This error takes the whole site offline, front end and admin, so it is one of the most stressful WordPress problems. I fix it regularly on client VPS servers and shared hosting accounts, and the causes fall into a handful of groups. The method below finds the right one in minutes instead of guessing: read the config, test the login yourself, then look at the server.

From my own work: When this error appears on a VPS that worked yesterday and nobody changed anything, my first command is not in WordPress at all. It is systemctl status mariadb, followed by the kernel log. On small servers, a stopped database killed by the out-of-memory killer is by far the most common cause I find.
Common causes and how to recognise them
| Cause | Typical clue | Fix section |
|---|---|---|
| Wrong credentials in wp-config.php | Started after a migration, password change or edit | Check config |
| Database server stopped | All sites on the server fail; mysql client cannot connect | Server |
| Out of memory | Happens at busy times or during backups | Server |
| Disk full | Database will not start; df -h shows 100% |
Server |
| User lacks privileges | Access denied for the WordPress user | Privileges |
| Too many connections | Error comes and goes under traffic | Connections |
| Corrupted tables | Admin says tables are unavailable | Repair |
Step 1: check the database settings in wp-config.php
WordPress reads four constants from wp-config.php, described in the wp-config.php documentation. Read them with WP-CLI or grep from the site root:
# Read the database settings WordPress is using (from the site root)
wp config get DB_NAME
wp config get DB_USER
wp config get DB_HOST
# Or look at the file directly
grep -E "DB_(NAME|USER|PASSWORD|HOST)" wp-config.php
Watch for typos, a database name with the old host’s prefix after a migration, and passwords containing quotes that break the PHP string. On some hosts DB_HOST is not localhost but a separate host name or a socket path. My guide to wp-config.php security hardening covers the other settings in this file and how to protect it.
Step 2: test the same credentials with the mysql client
This is the step that saves the most time. Log in from the server with exactly the values WordPress uses:
# Try the exact same credentials from the server
mysql -u wp_user -p -h localhost wp_database -e "SELECT 1;"
# Typical answers:
# ERROR 1045 (28000): Access denied for user 'wp_user'@'localhost' -> wrong user or password
# ERROR 1049 (42000): Unknown database 'wp_database' -> wrong DB_NAME
# ERROR 2002 (HY000): Can't connect to local server through socket -> server down or wrong host
# ERROR 1040 (HY000): Too many connections -> connection limit reached
If this works, the credentials and server are fine and the problem is in PHP or WordPress, such as a missing mysqli extension after a PHP upgrade or a different DB_HOST. If it fails, the error number points to the cause. The MySQL manual’s page on troubleshooting connection problems explains each case in depth.
From my own work: After a migration, I always test the database login from the command line before even opening the browser. Nine times out of ten, a failed migration is just a database user that exists on the new server but was never granted rights to the imported database.
Step 3: make sure the database server is running
# Is the database server running?
sudo systemctl status mariadb # or: mysql
sudo systemctl start mariadb
# Why did it stop?
sudo journalctl -u mariadb -n 50 --no-pager
sudo dmesg -T | grep -i -E "out of memory|killed process"
# Is the disk full?
df -h /var/lib/mysql
If MariaDB or MySQL is stopped, start it and read the journal to learn why it stopped. Three reasons dominate on VPS servers:
- Out of memory: the kernel log shows the database process was killed. Reduce PHP-FPM workers, tune the database buffer pool, and add swap; see my guide to MariaDB tuning for WordPress, which also covers swap.
- Disk full: the database cannot write and refuses to start or crashes. Free space first; my guide to finding large files when a Linux disk is full lists the usual suspects, such as old backups and logs.
- Crash recovery: after a power loss or kill, InnoDB recovers on start. Give it time, and read the log before forcing anything.
Step 4: fix the database user and privileges
Error 1045 with correct-looking credentials usually means the user does not exist for that host, has a different password, or lacks rights on the database. MySQL treats 'wp_user'@'localhost' and 'wp_user'@'127.0.0.1' as different accounts.
-- Run as the database admin (sudo mysql)
CREATE USER IF NOT EXISTS 'wp_user'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON wp_database.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
-- Check what a user may do
SHOW GRANTS FOR 'wp_user'@'localhost';
Grant rights only on the WordPress database, never global rights, and use a long random password. If credentials changed and nobody on your team changed them, treat it as a possible compromise.
Step 5: check connection limits
If the error appears only at busy times, the server may be reaching max_connections. The MySQL manual’s Too many connections page explains the limit. Check usage:
-- Current and maximum connections
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Max_used_connections';
SHOW VARIABLES LIKE 'max_connections';
-- Who is connected right now
SHOW PROCESSLIST;
Raising the limit is a short-term fix. Long-running queries holding connections are the usual root cause; enable the slow query log, add page caching so most visitors never reach PHP, and use a persistent object cache. The slow query log, with a threshold of one or two seconds, shows which queries hold connections longest.
Step 6: repair damaged tables
If the front end shows the error but wp-admin says one or more database tables are unavailable, a table may be damaged. WordPress has a built-in repair page, and WP-CLI has wp db check:
// wp-config.php: temporary, remove after use
define( 'WP_ALLOW_REPAIR', true );
// then open https://example.com/wp-admin/maint/repair.php
# Or from the command line
wp db check
wp db repair
Take a backup first with wp db export or mysqldump if the server lets you, and remove WP_ALLOW_REPAIR as soon as you finish, because the repair page does not require a login.
Turn on debug logging if the cause is still unclear
When the credentials work in the mysql client but WordPress still fails, log PHP errors to a file, as described in Debugging in WordPress:
// wp-config.php: log errors to wp-content/debug.log instead of the screen
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
Reload the page and read wp-content/debug.log and the PHP-FPM error log. Missing PHP extensions, a plugin with its own database connection, or a drop-in such as db.php from a caching plugin can all produce the same message.
From my own work: One stubborn case I fixed came from a leftover db.php drop-in from a caching plugin that had been deleted. WordPress kept loading it and failing. Renaming wp-content/db.php brought the site straight back. It is worth checking that folder for drop-ins whenever the usual checks pass.
How to prevent it from happening again
- Monitor the site with a keyword check so you know before customers do; see my Uptime Kuma guide.
- Size memory honestly: PHP-FPM workers, database buffers and swap that fit the VPS.
- Watch disk space with alerts at 80% and rotate logs and old backups.
- Back up the database daily and test restores; my guide to automated MySQL backups on a VPS shows a working setup.
- Enable the database at boot with
systemctl enable mariadband setRestart=on-failureif your distribution does not. - Document credentials in a password manager, not only in wp-config.php.
Error establishing a database connection checklist
- Read DB_NAME, DB_USER, DB_PASSWORD and DB_HOST from wp-config.php.
- Test the same values with the mysql client and note the error code.
- Check that MariaDB or MySQL is running and why it stopped.
- Check the kernel log for out-of-memory kills.
- Check free disk space.
- Check the user exists for the right host and has rights on the database.
- Compare connections with max_connections and find slow queries.
- Repair tables after a backup, then remove WP_ALLOW_REPAIR.
- Check debug.log and wp-content drop-ins if everything else passes.
- Add monitoring, backups and memory headroom.
Want this done on your own server or site?
I am Saizul Amin, a web developer and technical SEO consultant. I set up and harden VPS servers, migrate WordPress sites, fix indexing problems and build automations. See my portfolio or get in touch.
FAQ
What does Error establishing a database connection mean in WordPress?
WordPress could not connect to its MySQL or MariaDB database. The credentials in wp-config.php may be wrong, the database server may be stopped or overloaded, the database user may lack permissions, or tables may be damaged.
How do I fix Error establishing a database connection?
Check DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php, test them with the mysql command, make sure MariaDB or MySQL is running and the disk is not full, then check connection limits and repair tables if needed.
Why does my database keep stopping on a VPS?
On small VPS servers the most common reason is memory: the Linux out-of-memory killer stops MariaDB or MySQL when RAM runs out. Check the kernel log, tune PHP-FPM and database memory, and add swap.
Can a hacked site cause a database connection error?
Yes, sometimes. Attackers may change wp-config.php or database passwords, or malware may overload the server. If credentials changed without your knowledge, treat it as a compromise, change all passwords and clean the site.
Should I use localhost or 127.0.0.1 for DB_HOST?
Both usually work on a single server. localhost uses a Unix socket and 127.0.0.1 uses TCP. If one fails, try the other, and make sure the database user is allowed from that host, because MySQL treats them as different hosts for permissions.
Sources
- WordPress Developer Resources: wp-config.php
- WordPress Developer Resources: Debugging in WordPress
- WP-CLI: wp db check
- MySQL 8.4 Reference Manual: Too many connections
- MySQL 8.4 Reference Manual: Troubleshooting problems connecting to MySQL

Saizul Amin is a web developer, AI automation engineer and technical SEO consultant from Dhaka, Bangladesh (B.Sc. in CSE, BUBT). Since 2017 he has worked in digital marketing, WordPress, IT infrastructure and server administration, including the bilingual SHR Bangladesh website for DGHS, WordPress sites for UK and US clients, and the ShobFree tools platform. He runs this blog on his own Ubuntu VPS with nginx, Redis and CloudPanel, and tests the guides here on it. Portfolio: saizul.com

