SQLSTATE[HY000] [2002] Connection refused within Laravel homestead

SQLSTATE[HY000] [2002] Connection refused within Laravel homestead

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

SQLSTATE[HY000] [2002] Connection refused within Laravel Homestead

Encountering SQLSTATE[HY000] [2002] Connection refused inside a Laravel project running on Homestead is a classic symptom of a mismatch between the application's expectations and the database server's actual network configuration. The error message indicates that the PHP process cannot establish a TCP connection to the MySQL port, yet the fact that you can connect via Sequel Pro suggests the database service itself is running and accepting connections.

When using Homestead 2.2.1 with Laravel 5.2, the environment relies on a specific virtual network topology. By default, Vagrant and Homestead forward specific ports from your host machine to the guest VM to avoid port conflicts. The most common scenario involves MySQL listening on port 3306 inside the VM but being accessible to your local host via port 33060.

If your .env file contains:

DB_HOST=127.0.0.1
DB_PORT=3306

Laravel attempts to connect directly to the IP address of the VM itself (which is often the host's loopback interface when using shared folders and port forwarding). If your MySQL configuration does not explicitly allow connections from that specific external IP or if the firewall within the VM blocks the direct incoming request, you will hit a "Connection refused" error.

The fact that Sequel Pro works with Host: 127.0.0.1 and Port: 33060 is the critical clue. This implies that your database server is configured to listen on port 3306 internally, but the Homestead virtual network forwards external connections arriving at 33060 to internal 3306. However, if you configure Laravel to connect to 127.0.0.1 without specifying the forwarded port, or if the database user is restricted by host permissions, the connection fails.

To resolve this, you must ensure the database credentials in your .env match the specific access rules defined in your MySQL configuration (my.cnf or my.ini). If you are using the Homestead default settings, the database is often created with a user named homestead that has access from localhost. When connecting via Sequel Pro, if you explicitly use 127.0.0.1, it might be resolving to the VM's internal loopback. However, Laravel running inside the same VM (via Homestead) should technically connect to 127.0.0.1 on port 3306.

If you are running the database commands from the host machine while pointing at the VM, you must change your connection string to use the forwarded port:

DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=tcv
DB_USERNAME=homestead
DB_PASSWORD=secret

Alternatively, if you are running everything strictly inside the VM (as suggested by "within homestead"), the port should remain 3306. The discrepancy between your terminal and Sequel Pro usually stems from how the host resolves the IP versus how the guest resolves it. Ensure your MySQL user privileges allow connections from the specific IP Laravel is trying to reach. You can verify this by running:

SELECT User, Host FROM mysql.user;

If you see homestead listed only with localhost, but Laravel is connecting via a different interface or port forwarding logic, access will be denied.

When moving the project to MAMP as mentioned in your edit, remember that MAMP manages its own virtual hosts and database ports (often defaulting to 8889 for MySQL). If you simply copy the .env file from Homestead (port 3306) to MAMP without adjusting the DB_PORT, you will encounter the exact same connection refused error. The solution is always to align the DB_HOST and DB_PORT in your environment file with the actual listening address of the database server currently running on your machine, regardless of whether that server is a VM or local software.

Our network