PHP error guide
SQLSTATE HY000 connection refused: meaning and fix
Error summary
Check service health, bind address, container hostname, and port before changing credentials. The TCP target has no reachable MySQL listener or a network policy rejects it.
What it means
Check service health, bind address, container hostname, and port before changing credentials. The TCP target has no reachable MySQL listener or a network policy rejects it.
What the error means
This message means that the TCP target has no reachable MySQL listener or a network policy rejects it. The exact signature distinguishes sqlstate hy000 connection refused from a generic application failure.
Why PHP produces it
The engine or service reports “SQLSTATE HY000 connection refused” because its required precondition was not met. Check service health, bind address, container hostname, and port before changing credentials.
PHP version notes
The sqlstate hy000 connection refused wording here is based on PDO with MySQL/MariaDB; exact server wording varies; punctuation and exception class names can differ on older branches or vendor builds.
Most common causes
- The immediate input or configuration reaches the specific condition: the TCP target has no reachable MySQL listener or a network policy rejects it
- The code path assumes the prerequisite for sqlstate hy000 connection refused has already been satisfied.
- For sqlstate hy000 connection refused, development and production differ in version, extension, permissions, paths, or service configuration.
- An earlier operation returned an unchecked value that is consumed by the line reporting sqlstate hy000 connection refused.
Minimal examples
BAD — reproduces the problem
$dsn = "mysql:host=localhost;port=3307";
FIXED — safer pattern
$dsn = "mysql:host=db;port=3306;dbname=app;charset=utf8mb4";
Step-by-step diagnosis
- Copy the complete “SQLSTATE[HY000] [2002] Connection refused” text and retain the first application stack frame.
- Reproduce sqlstate hy000 connection refused in the same SAPI and environment listed for this page.
- Before changing sqlstate hy000 connection refused, inspect the preceding value or directive and verify its type, path, version, and permissions.
- Apply the narrow correction—check service health, bind address, container hostname, and port before changing credentials—then repeat the original request once.
Fixes
Correct the failing prerequisite
Check service health, bind address, container hostname, and port before changing credentials
$dsn = "mysql:host=db;port=3306;dbname=app;charset=utf8mb4";
Fail explicitly at the boundary
Validate the condition before the operation that emits sqlstate hy000 connection refused, and log a safe diagnostic without credentials or full production paths.
Common mistakes when fixing it
- Suppressing sqlstate hy000 connection refused instead of correcting its upstream condition.
- Testing sqlstate hy000 connection refused only with the CLI binary when the failing request runs under FPM or Apache.
- Changing a global setting for sqlstate hy000 connection refused before confirming the site-specific effective configuration.
How to prevent it
- Add a focused test that exercises the boundary responsible for sqlstate hy000 connection refused.
- Keep runtime versions, extensions, configuration, and deploy artifacts affecting sqlstate hy000 connection refused reproducible.
- Validate external data and service return values before they can trigger sqlstate hy000 connection refused.
Web server / environment notes
fpm, cli, docker, linux. The failure occurs where the TCP target has no reachable MySQL listener or a network policy rejects it
Tags: fpm,cli,docker,linux