SQLSTATE[HY000] [2002] Connection refused Laravel 5.8.35

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Solving SQLSTATE[HY000] [2002] Connection Refused in Laravel Development

As a senior developer working with PHP frameworks like Laravel, we frequently encounter frustrating errors that seem abstract but point to very concrete infrastructure problems. One of the most common and maddening issues developers face when setting up local environments—especially when running php artisan migrate—is the SQLSTATE[HY000] [2002] Connection refused error.

This post will dive deep into why this specific error occurs within a Laravel 5.8.35 context on macOS/MAMP setups, and provide a practical, step-by-step guide to resolving it.

Understanding the "Connection Refused" Error

The error message SQLSTATE[HY000] [2002] Connection refused is not an error generated by Laravel itself; rather, it is a low-level operating system or network error indicating that the client application (your PHP script attempting to connect via PDO) successfully sent a request to the specified IP address and port (e.g., 127.0.0.1:3306), but the target machine actively refused the connection attempt.

In simple terms, this means: The network path was found, but no service was listening on that specific port, or a firewall blocked the handshake.

When running migrations, Laravel needs to talk to the MySQL database to create tables. If the connection is refused, it means the PHP process cannot establish the necessary communication with the MySQL server.

Troubleshooting Steps for Local Environments (MAMP/macOS)

Since your configuration in your .env file (DB_HOST=127.0.0.1 or localhost, DB_PORT=3306) looks standard, the problem almost certainly lies outside of Laravel’s code and within your local server setup. Here is a systematic approach to diagnose and fix the issue:

1. Verify the MySQL Server Status (The Most Common Culprit)

The primary reason for this error is that the database service is not running.

  • Check MAMP Status: Open your MAMP application and ensure that the MySQL server module is actively running. If it's stopped, start it immediately.
  • System Service Check (If using native MySQL): If you are using a standalone MySQL installation rather than MAMP, verify that the service is active.

2. Validate Database Credentials and Configuration

Even if the connection is refused, ensuring the credentials match what the database expects is crucial. Double-check your .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1  # Or localhost
DB_PORT=3306
DB_DATABASE=auth
DB_USERNAME=etn
DB_PASSWORD=pass

Ensure that the MySQL user (etn) exists and has the necessary permissions to connect from 127.0.0.1 (or whatever host you specify). If you are using a local setup, ensure the user was created correctly during the initial database setup phase.

3. Inspect Firewall Settings

While less common on standard macOS setups for localhost connections, overly aggressive security settings or third-party security software can sometimes block internal communication ports like 3306. Briefly check your system's firewall settings to ensure no rules are explicitly blocking outgoing/incoming traffic on port 3306.

4. Test Connectivity Independently (The Ultimate Test)

To isolate the problem, bypass Laravel and test the connection directly using a native MySQL client (like the command line or MySQL Workbench).

Try connecting manually:

mysql -u etn -p -h 127.0.0.1 -P 3306

If this manual connection also fails with "Connection refused," you have definitively proven that the issue is between your PHP/Laravel environment and the MySQL server itself, not Laravel’s migration logic.

Best Practices for Robust Database Setup

When building applications on a framework like Laravel, establishing robust infrastructure is paramount. As we discuss in modern application architecture, ensuring reliable data access is key to scalability. For developers focusing on solid framework design, understanding how these dependencies interact is vital—something that aligns perfectly with the principles discussed at https://laravelcompany.com.

For future projects, always establish a clean separation between application settings and infrastructure setup. Avoid hardcoding database details wherever possible; reliance on environment variables, as you have done correctly in your .env file, is the correct approach for maintainable code.

Conclusion

The SQLSTATE[HY000] [2002] Connection refused error in a Laravel migration context is almost always an infrastructure issue related to the MySQL server not being accessible or running. By systematically checking the status of your database service, verifying credentials, and testing connectivity outside of the framework, you can quickly resolve this frustrating roadblock. Remember: when dealing with connections, always trace the failure back to the operating system level before blaming the application layer.