How to Fix "Error Establishing a Database Connection" in WordPress

The six causes of WordPress's database connection error — credentials, MySQL down, full disk, corrupt table, too many connections, host — with a fix for each.

Published
Reading time
3 min

WordPress shows this message when PHP cannot open a connection to MySQL. It looks alarming, and it usually has a boring cause you can find in five minutes. Work through the list in order; it is ordered by how often each one is the answer.

1. The credentials in wp-config.php are wrong

Open wp-config.php in the site root and look at the four lines:

php
define('DB_NAME', 'example_db');
define('DB_USER', 'example_user');
define('DB_PASSWORD', 'the-password');
define('DB_HOST', 'localhost');

Compare them with the database in Plesk (Databases: the name and user are shown; reset the password there if you are not sure of it and update the file). This is the cause after almost every migration and after every "I changed the database password to be safe".

Test from the command line, if you have shell access:

bash
mysql -u example_user -p'the-password' -h localhost example_db -e "SELECT 1;"

Access denied means credentials; Unknown database means the name; a result of 1 means the credentials are fine and the problem is elsewhere.

2. MySQL is not running

bash
sudo systemctl status mariadb     # or mysql

If it is stopped or failed, start it and read why it died:

bash
sudo systemctl start mariadb
sudo journalctl -u mariadb -n 50

The commonest reason for MySQL to have died on a small server is being killed by the kernel's out-of-memory handler during a traffic spike — journalctl -k | grep -i "out of memory" confirms it. That is a sizing problem: fewer PHP workers (tuning PHP-FPM), a smaller InnoDB buffer pool, swap, or more RAM.

On shared hosting you cannot see this; if the panel's own database tools also fail, the host's MySQL is down and a ticket is the fix.

3. The disk is full

MySQL refuses to write when the disk is full and connections start failing shortly after.

bash
df -h

Anything at 100% is the answer. Clear space (finding what fills the disk), then restart MySQL.

4. A corrupt table

Often after a crash or a full-disk event. The site may load the front page but not the admin, or show the error intermittently. Add to wp-config.php temporarily:

php
define('WP_ALLOW_REPAIR', true);

Visit https://example.com/wp-admin/maint/repair.php, click Repair Database, then remove the line — the page is accessible to anyone while it is set. From the command line:

bash
mysqlcheck -u root -p --auto-repair --check example_db

5. Too many connections

Under heavy load, MySQL hits max_connections (default 151) and refuses new ones. The error is intermittent and appears at peak times. Confirm:

bash
mysql -u root -p -e "SHOW STATUS LIKE 'Max_used_connections'; SHOW VARIABLES LIKE 'max_connections';"

If the used count equals the limit, raise it modestly in the MySQL config (max_connections = 300) — but the real fix is usually a page cache so fewer requests reach the database, or PHP-FPM workers capped below what MySQL can serve.

6. The database host changed

On a new server or after a host's migration, the database may live at a different host name — 127.0.0.1 instead of localhost (which uses a socket rather than TCP), or a separate database server. Check Plesk's database page for the host, and try 127.0.0.1 in DB_HOST if localhost fails; the socket path may differ.

Reading the real error

WordPress hides the reason by default. Turn on debug logging in wp-config.php:

php
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

Then read wp-content/debug.log — it shows the actual MySQL error (Access denied, Can't connect to local MySQL server through socket, Too many connections) which maps directly to the sections above. Turn it off again afterwards.

When it is a hack

Rarely, wp-config.php has been altered by malware, or the database user was deleted by an attacker. If the credentials look wrong and you did not change them, treat it as an incident: what to do when your site is hacked.

On VPSPioneer shared plans, causes 2, 3 and 5 are monitored and handled on the server before you see them; on a managed VPS the same, plus we will find cause 1 for you after a migration if a ticket says "database error".

#wordpress#mysql#troubleshooting#database

Keep reading

More from WordPress

All guides

WordPress

WordPress Staging on Plesk: Clone, Test, Then Sync to Live

Use WordPress Toolkit to clone a site to staging, test updates safely, then sync files or database back to live without losing orders or comments.

3 min read →

WordPress

How to Install WordPress on Plesk with WordPress Toolkit

Install WordPress on Plesk in two minutes with WordPress Toolkit, then set the day-one options: SSL, updates, hardening, backups and staging.

3 min read →

WordPress

How to Speed Up WordPress on Plesk in 20 Minutes

Six changes that make WordPress on Plesk noticeably faster — PHP-FPM, OPcache, Nginx caching, WebP images, database cleanup and a CDN — with exact settings.

3 min read →