Error fix

SQLSTATE[HY000] [2002] Connection refused in Laravel

Laravel cannot reach the database server at the configured host, port or socket.

The error message

SQLSTATE[HY000] [2002] Connection refused

Possible causes: why does "SQLSTATE[HY000] [2002] Connection refused" appear?

  • Database server not running or on another port. The MySQL manual explains error 2002: normally no MySQL server is running on the system, or the client uses a wrong Unix socket file or TCP/IP port. A firewall that blocks the port has the same effect.
  • Wrong DB_HOST with Docker Compose. By default, Compose puts the services on one network, and each container reaches the others by their service name. DB_HOST should then be the name of the database service.
  • Cached configuration. After php artisan config:cache the .env file is no longer loaded. A changed DB_HOST or DB_PORT only takes effect after the cache is cleared or rebuilt.

How do you fix it?

  1. Check that the database service is running and on which port it listens.
  2. Set DB_HOST and DB_PORT in .env (with Docker Compose: the name of the database service).
  3. Run php artisan config:clear; in production run php artisan config:cache again afterwards.

Sources

Sources checked on 2026-12-03. German version: SQLSTATE[HY000] [2002] Connection refused beheben

Related errors

Still getting the error?

Your Laravel project gets checked, and you learn honestly what causes it. Free 30-minute call.