SQLSTATE[HY000] [2002] Connection refused LARAVEL

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Deciphering SQLSTATE[HY000] [2002] Connection Refused in Laravel: A Deep Dive into Database Connectivity

As a senior developer, I’ve seen countless hours spent chasing cryptic errors, especially when dealing with database connectivity. The error SQLSTATE[HY000] [2002] Connection refused during php artisan migrate is one of the most frustrating because it points to a fundamental communication failure, often masking deeper configuration or system issues rather than just a simple typo in the .env file.

This post will break down why this error occurs specifically in a Laravel context, analyze the subtle differences between using 127.0.0.1 and localhost, and provide a systematic approach to resolving these stubborn database connection problems with MariaDB or MySQL.

Understanding the "Connection Refused" Error

The Connection refused error means that your Laravel application (running on the PHP/FPM server) successfully initiated a request to connect to the database host and port, but the target machine actively rejected the connection attempt. This is usually not an authentication error (like wrong username/password), but rather a network or service-level blockage.

When you see this during migrations, it confirms that Laravel cannot establish the TCP handshake with the MariaDB server running on the specified host.

The Hostname Conundrum: 127.0.0.1 vs. localhost

The confusion often arises when testing different hostname conventions. You noted that switching from DB_HOST=127.0.0.1 to DB_HOST=localhost changed the error message from "Connection refused" to "No such file or directory." This difference is critical for debugging:

  1. 127.0.0.1 (IPv4 Loopback): This explicitly forces a connection attempt over the IP address. It bypasses potential DNS resolution issues and directly targets the network interface, which is often reliable for local connections.
  2. localhost: This relies on the system's hostname resolution (DNS lookup) to resolve the name to the loopback IP address (127.0.0.1). If the system configuration or specific MySQL service setup has issues resolving hostnames, using localhost can trigger a different error path, like "No such file or directory," indicating a failure in the name resolution step itself, rather than a connection refusal at the network level.

In many development environments, sticking to the explicit IP address (127.0.0.1) is often the most robust starting point when troubleshooting local connectivity issues.

Systematic Troubleshooting Steps

Since clearing caches didn't resolve the issue, we need to look outside of the Laravel application itself and examine the database environment:

1. Verify Database Service Status

The most common cause of "Connection refused" is that the database server (MariaDB/MySQL) is not running or is not listening on the expected port (3306).

Action: Log into your server directly and check the service status.

sudo systemctl status mariadb  # Or mysql, depending on your distribution

Ensure the service is active and running without errors. If it's stopped, start it immediately: sudo systemctl start mariadb.

2. Check Firewall Configuration

If the database server is running but still refuses connections, a firewall (like ufw or cloud security groups) might be blocking port 3306 from your application server.

Action: Verify that port 3306 is open for incoming traffic from the application host.

3. Validate User Permissions

Even if the connection is refused, ensuring the database user (root in your case) has appropriate privileges to access the necessary schema tables (information_schema) is crucial for migration tools like Laravel to function correctly. Ensure the user is configured to connect from the host specified in your .env.

Conclusion

Resolving SQLSTATE[HY000] [2002] Connection refused requires moving beyond application configuration and diving into the operating system and database service layers. By systematically checking if the service is running, verifying firewall rules, and understanding the subtle differences between IP addresses and hostnames, you can pinpoint whether the problem lies with network connectivity or internal service configuration.

Always remember that robust Laravel development relies on a stable underlying infrastructure. For deeper insights into building scalable applications on this foundation, exploring resources from laravelcompany.com is highly recommended.