Laradock - SQLSTATE[HY000] [2002] Connection refused (SQL: select count(*) as aggregate from `users` where `email` =

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Laradock Database Connections: Solving SQLSTATE[HY000] Connection Refused Errors

As senior developers working with containerized environments like Docker and orchestration tools like Laradock, managing service-to-service communication—especially database connections—can often introduce subtle but frustrating errors. One of the most common pitfalls is the SQLSTATE[HY000] [2002] Connection refused error when your Laravel application attempts to communicate with its MySQL database.

This post dives deep into why this error occurs specifically within a Laradock setup, how to diagnose the networking issue, and provide the definitive fix for connecting your Laravel application successfully to your Dockerized database.

Understanding the Connection Refused Error in Docker

The error SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known is fundamentally a networking issue. It means that the PHP process running your Laravel application cannot resolve the hostname specified in your database configuration (DB_HOST) into an actual IP address it can connect to.

In a standard Docker setup managed by Laradock, services communicate internally via Docker networks. The problem usually arises from one of two scenarios:

  1. Incorrect Hostname: Using localhost or 127.0.0.1, which refers to the PHP container itself, not the MySQL container on the network.
  2. Network Misconfiguration: The service names used in the .env file do not match the actual service names defined in your docker-compose.yml file (which Laradock manages).

Diagnosing the Laradock Setup Conflict

The provided examples clearly illustrate the conflict:

When you initially set up the environment, using DB_HOST=mysql, it fails to connect properly across all operations. When you switch to DB_HOST=127.0.0.1, migrations succeed but user signups fail with a different error (often related to permissions or application logic failure after the initial connection is established).

This discrepancy points directly to how your Docker network maps services:

  • mysql: This refers to the service name defined in your docker-compose.yml. Inside a Docker network, this name acts as the hostname for services within that network.
  • 127.0.0.1 (or localhost): This refers to the IP address of the PHP container itself. It is useless for connecting to an external service running in a separate container.

The key takeaway is that services inside Docker containers must reference each other by their service names, not by loopback addresses.

The Correct Laradock Configuration Strategy

To ensure seamless database connectivity, you must align your Laravel .env file with the service names defined in your infrastructure setup. If you are using Laradock, it manages these internal networking details for you based on your configuration files.

Step 1: Verify Docker Service Names

Ensure your docker-compose.yml (or Laradock equivalent) defines a service named mysql. This name is the hostname PHP should use to reach the database container.

Step 2: Correcting the Database Connection

For reliable communication within the Laradock environment, always use the defined service name as the DB_HOST.

Incorrect (Leads to Connection Refused):

DB_HOST=127.0.0.1  # Incorrect for inter-container communication

Correct (Uses Docker Networking):

DB_CONNECTION=mysql
DB_HOST=mysql      # Use the service name defined in docker-compose/Laradock setup
DB_PORT=3306
DB_DATABASE=cd_eloquent
DB_USERNAME=cristian
DB_PASSWORD=cristian

This approach ensures that the DNS resolution works correctly across the Docker network, allowing your Laravel application to successfully establish a connection. This principle of managing environment variables based on container service names is a cornerstone of robust application deployment, aligning perfectly with the principles emphasized by the Laravel team at laravelcompany.com.

Conclusion

The SQLSTATE[HY000] [2002] error in a Laradock environment is rarely a bug in the database itself; it's almost always a failure in networking configuration between containers. By understanding that Docker networks require service names (mysql) instead of host addresses (127.0.0.1), you can eliminate this frustrating connection refusal. Always verify your DB_HOST setting against your underlying Docker orchestration rules to ensure