PHP error guide

SQLSTATE HY000 MySQL socket not found: meaning and fix

Error summary

Use the correct unix_socket path or 127.0.0.1 for TCP intentionally. A localhost DSN selects a Unix socket path that does not exist in this runtime.

What it means

Use the correct unix_socket path or 127.0.0.1 for TCP intentionally. A localhost DSN selects a Unix socket path that does not exist in this runtime.

What the error means

This message means that a localhost DSN selects a Unix socket path that does not exist in this runtime. The exact signature distinguishes sqlstate hy000 mysql socket not found from a generic application failure.

Why PHP produces it

The engine or service reports “SQLSTATE HY000 MySQL socket not found” because its required precondition was not met. Use the correct unix_socket path or 127.0.0.1 for TCP intentionally.

PHP version notes

The sqlstate hy000 mysql socket not found 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: a localhost DSN selects a Unix socket path that does not exist in this runtime
  • The code path assumes the prerequisite for sqlstate hy000 mysql socket not found has already been satisfied.
  • For sqlstate hy000 mysql socket not found, 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 mysql socket not found.

Minimal examples

BAD — reproduces the problem

php
$dsn = "mysql:host=localhost;dbname=app";

FIXED — safer pattern

php
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=app";

Step-by-step diagnosis

  1. Copy the complete “SQLSTATE[HY000] [2002] No such file or directory” text and retain the first application stack frame.
  2. Reproduce sqlstate hy000 mysql socket not found in the same SAPI and environment listed for this page.
  3. Before changing sqlstate hy000 mysql socket not found, inspect the preceding value or directive and verify its type, path, version, and permissions.
  4. Apply the narrow correction—use the correct unix_socket path or 127.0.0.1 for TCP intentionally—then repeat the original request once.

Fixes

Correct the failing prerequisite

Use the correct unix_socket path or 127.0.0.1 for TCP intentionally

php
$dsn = "mysql:host=127.0.0.1;port=3306;dbname=app";

Fail explicitly at the boundary

Validate the condition before the operation that emits sqlstate hy000 mysql socket not found, and log a safe diagnostic without credentials or full production paths.

Common mistakes when fixing it

  • Suppressing sqlstate hy000 mysql socket not found instead of correcting its upstream condition.
  • Testing sqlstate hy000 mysql socket not found only with the CLI binary when the failing request runs under FPM or Apache.
  • Changing a global setting for sqlstate hy000 mysql socket not found before confirming the site-specific effective configuration.

How to prevent it

  • Add a focused test that exercises the boundary responsible for sqlstate hy000 mysql socket not found.
  • Keep runtime versions, extensions, configuration, and deploy artifacts affecting sqlstate hy000 mysql socket not found reproducible.
  • Validate external data and service return values before they can trigger sqlstate hy000 mysql socket not found.

Web server / environment notes

fpm, cli, docker, linux. The failure occurs where a localhost DSN selects a Unix socket path that does not exist in this runtime

Tags: fpm,cli,docker,linux

Categories