Error Establishing a Database Connection: Fixes

Written by

in

Lab-tested: PHP 8.4.27 + TCP database endpoint · 2 Dec 2026 · Reproduced and fixed in our lab

Fix Box — Try This First (10 Seconds)

This message means your app cannot reach its database. It is a connection problem first, a content problem never.

  1. Check whether the database server is actually running.
  2. Verify host, port, database name, user, and password in your config.
  3. Test the connection from the web server itself, then reload.

The Exact Symptoms

WordPress shows one line: “Error establishing a database connection”. There is no partial page and no admin access.

Every page fails the same way, because every page needs the database. That uniformity is itself a clue.

What We Reproduced in the Lab

Our PHP test opened a TCP connection to a database port where nothing was listening. The connection was refused instantly.

The script returned HTTP/1.1 500 Internal Server Error with the WordPress message. The log recorded the real reason:

DB connect failed: Connection refused (111)

Error 111 means the host answered, but no service accepted the connection. The database process was down, or the port was wrong.

The Fixes

Point the app at a live database endpoint. In our lab, a listening endpoint on the correct port turned the same script green.

The fixed request returned HTTP/1.1 200 OK. The only change was the target port in the connection call.

<?php
// Test from the web server, before blaming credentials
$fp = @fsockopen("127.0.0.1", 3306, $errno, $errstr, 2);
if (!$fp) { echo "DB unreachable: $errstr ($errno)"; exit; }
echo "DB port reachable";

On real WordPress, check DB_HOST, DB_NAME, DB_USER, and DB_PASSWORD in wp-config.php. Then confirm the MySQL/MariaDB service is running on that host.

Decision Table

Signal Cause First fix
Connection refused (111) DB down or wrong port Start DB, verify port
Access denied for user Wrong user/password or grants Fix credentials, re-grant
Unknown database DB_NAME typo or DB dropped Create/select correct DB
Connection timed out Firewall or wrong host Open port, verify DB_HOST

What Did Not Work

Restarting PHP did nothing in our lab. The script was healthy; the database endpoint simply was not there.

Editing page content also cannot help. Nothing renders at all until the connection succeeds.

Prevention

Monitor the database port separately from the web port. A site can look “up” to a web check while every page fails.

Database failures often follow host migrations or password rotations. See also PHP 500 Error and WordPress 500 Error.

Confirm the live status with the 500 Error Checker. For the full picture, start at 500 Internal Server Error.

FAQ

Is my content deleted when I see this? Almost never. The data is usually intact; the app just cannot reach it.

Why did it break without me changing anything? Database restarts, host maintenance, or credential rotation on the server side are common triggers.

Can a plugin cause this message? Rarely. This message comes from the core connection step, before plugins load.

What if only the admin fails? That is a different problem. This error takes down the whole site uniformly.

How fast should a healthy connect be? Milliseconds on the same host. A two-second timeout already means something is wrong.

Sources and date

Lab tests run 2 Dec 2026 on PHP 8.4.27. References checked live on the same date. Honest limit: the lab had no MySQL server or PDO driver, so the failure was reproduced at the TCP layer with the exact WordPress message; the fix proved reachability, not a full MySQL login.

  • RFC 9110 §15.6.1: https://www.rfc-editor.org/rfc/rfc9110.html
  • MDN — 500: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/500
  • PHP — fsockopen: https://www.php.net/manual/en/function.fsockopen.php
  • WordPress — Common errors: https://wordpress.org/documentation/article/common-wordpress-errors/

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


More posts