Home FixHow to Fix ‘Error Establishing a Database Connection’ in WordPress

How to Fix ‘Error Establishing a Database Connection’ in WordPress

by Knowledge Base Linux
How to Fix Error Establishing a — How to Fix 'Error Establishing a Database Connection' in WordPress

Seeing Error Establishing a Database Connection instead of your website means WordPress cannot talk to its MySQL or MariaDB database. Every page, post and setting lives in that database, so WordPress has nothing to display. This error looks scary, but it is one of the most well-understood WordPress problems. Work through the causes below in order — most sites are fixed within the first three steps. This article covers How to Fix Error Establishing a in detail.

What Causes the Error Establishing a Database Connection? – How to Fix Error Establishing a

  • Wrong credentials in wp-config.php — database name, username, password or host changed (very common after a migration or a password reset).
  • The MySQL/MariaDB service is down — crashed, stopped after an update, or killed by the out-of-memory (OOM) killer.
  • Corrupted database tables — often after an unclean shutdown or a full disk.
  • The server ran out of resources — no free RAM or disk space for MySQL to run.
  • Too many connections — traffic spikes exhaust max_connections.
Repairing a MySQL database to fix the error establishing a database connection

Step 1: Check Whether the Database Service Is Running – How to Fix Error Establishing a

Before editing anything, confirm MySQL or MariaDB is alive:

sudo systemctl status mysql
# or, on MariaDB systems:
sudo systemctl status mariadb

If the service is stopped, start it and watch what happens:

sudo systemctl start mysql
sudo journalctl -u mysql -n 50 --no-pager

If it fails to start, the journal output usually names the reason — a full disk (df -h) and InnoDB corruption are the classics. Also check free memory with free -h: on small VPS servers the OOM killer frequently terminates MySQL first because it is the largest process.

Step 2: Verify the Credentials in wp-config.php

Open wp-config.php in your site’s root directory and note these four values:

define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );

Test them directly from the shell — this separates a WordPress problem from a database problem:

mysql -u database_user -p -e "USE database_name; SHOW TABLES;"

If the login fails, reset the database user’s password in your control panel (or with ALTER USER as root) and update wp-config.php to match. If the login works, the credentials are fine and the issue is inside the database itself.

Step 3: Repair the Database with WordPress’ Built-In Tool

WordPress ships a repair mode that fixes corrupted tables. Add this line to wp-config.php, just before the “That’s all, stop editing” comment:

define( 'WP_ALLOW_REPAIR', true );

Then visit https://your-site.com/wp-admin/maint/repair.php and run Repair Database. When it finishes, remove the line again — the repair page is accessible without a login while it is enabled. If you prefer working in the shell, the same job can be done with mysqlcheck -u root -p --auto-repair --all-databases. Our guide to installing WP-CLI on Linux also shows wp db repair, which does the same thing from the terminal.

Step 4: Rule Out a Corrupted WordPress Core or Plugin

If the database checks out, test whether WordPress itself is damaged. From the site root, run:

wp core verify-checksums
wp plugin list

A checksum mismatch means core files were modified or corrupted — reinstall core with wp core download --force (this never touches your content or wp-config.php). To test plugins, rename the wp-content/plugins folder temporarily; if the site loads, rename it back and reactivate plugins one by one.

Step 5: Check Connection Limits and Server Load

Intermittent errors during traffic peaks point at connection limits. Inspect the current usage:

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

If Threads_connected sits near max_connections, raise the limit carefully in /etc/mysql/my.cnf and restart MySQL — but also look for the real cause, such as a plugin opening persistent connections or a botnet hammering wp-login.php.

How to Prevent the Error Establishing a Database Connection

Take regular database backups (our tutorial on automating Linux backups with rsync and cron is a good starting point), monitor memory so the OOM killer never picks MySQL, and keep enough free disk space for InnoDB logs. The official WordPress support documentation also lists this error among the most common issues, with the same credential-first approach.

How to Tell Whether It Is the Website or the Whole Server

One quick experiment narrows the scope dramatically: check whether other sites on the same server still work, and whether command-line tools can reach MySQL. If every site shows the same error and mysql from the shell also fails, the database server itself is down or out of resources — fix MySQL, not WordPress. If only one site is affected while the shell login with the same credentials succeeds, the problem is inside that site’s configuration or tables, so focus on Steps 3 and 4. If the error appears only under load — mornings are fine, campaigns break it — you are looking at connection or memory limits rather than corruption. Writing down which pattern you see before changing anything saves the random restarts and config edits that often turn a ten-minute outage into an hour of archaeology.

Conclusion

The “Error Establishing a Database Connection” message means WordPress cannot reach MySQL. Verify the service is running, test the wp-config.php credentials from the shell, repair the tables if needed, and only then dig into core files and connection limits. Followed in order, these steps resolve the error in the overwhelming majority of cases.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.