Fix "SQLSTATE[HY000] [2002] Connection refused" in Laravel
SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from `sessions` where `id` = 6bQ2wXyZ limit 1)
- SQLSTATE[HY000] [2002] No such file or directory
- SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known
- SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it
- SQLSTATE[HY000] [2002] Connection timed out
- PDOException: SQLSTATE[HY000] [2002] Connection refused
PHP cannot open a network connection to MySQL: nothing is listening at the given address and port. Check that the MySQL server is running, then that DB_HOST and DB_PORT in .env are correct (the service name under Docker), and run php artisan config:clear.
What this error means
Error 2002 is raised by the PDO MySQL client before any authentication: the connection could not even be opened. "Connection refused" means the target host answered but no program is listening on that port. "No such file or directory" means PDO tried a Unix socket (which happens with DB_HOST=localhost) that does not exist. "getaddrinfo failed" means the host name could not be resolved. In Laravel 11+, the SQL query and connection name are appended in parentheses. If the username or password were wrong, you would get error 1045, not 2002.
Common causes
- MySQL is not running: stopped service, stopped Docker container, or MySQL crashing on startup.
- Wrong host or port: port 3307 or 8889 (MAMP) instead of 3306, or an address that does not match where the database runs.
- Docker confusion: inside a container,
127.0.0.1is the container itself, you need the service name (mysql). Conversely, outside Docker the service name does not resolve. - Cached configuration: after
php artisan config:cache, changes to.envare ignored until the cache is cleared. - Unreachable remote server: MySQL only listens on
127.0.0.1(bind-address) or a firewall blocks port 3306.
Step-by-step fix
-
Check that MySQL is running
Commands depend on your system and how MySQL is installed (MySQL or MariaDB).
# macOS (Homebrew) brew services list brew services start mysql # Linux (systemd) sudo systemctl status mysql sudo systemctl start mysql # Windows (PowerShell) Get-Service *mysql* # Docker docker compose ps docker compose up -d mysql -
Test the connection outside Laravel
If the
mysqlclient fails too, the problem is not Laravel. On Windows,Test-NetConnection 127.0.0.1 -Port 3306checks that the port answers.mysql -h 127.0.0.1 -P 3306 -u laravel -p nc -zv 127.0.0.1 3306 -
Fix
.envUse
127.0.0.1rather thanlocalhostto force a TCP connection, and your server’s real port.DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=laravel DB_PASSWORD=secret -
Adapt
DB_HOSTto Docker or SailIf Laravel runs in a container,
DB_HOSTmust be the MySQL service name fromcompose.yaml(mysqlwith Sail), andDB_PORTthe internal port3306. If Laravel runs on your machine and MySQL in Docker, use127.0.0.1and the published port (ports: "3306:3306"). With Sail, run commands through./vendor/bin/sail artisan.# Laravel inside Docker / Sail DB_HOST=mysql DB_PORT=3306 -
Clear the config cache and verify
Laravel may still be reading an old configuration. Clear it, then check the connection.
php artisan config:clear php artisan db:show -
Remote server: use an SSH tunnel
Instead of opening port 3306 to the Internet, create an SSH tunnel then set
DB_HOST=127.0.0.1andDB_PORT=3307. If you really must expose MySQL, restrict access by firewall to allowed IPs only.ssh -N -L 3307:127.0.0.1:3306 [email protected]
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- With Docker Compose, add a
healthcheckon MySQL anddepends_on: condition: service_healthyso the app waits until the database is ready. - Keep
.env.exampleup to date with the expectedDB_HOSTandDB_PORTvalues for each way of running the project. - Only run
php artisan config:cachein production, and run it again after every.envchange.
Frequently asked questions
Why does localhost give "No such file or directory" while 127.0.0.1 works?
With localhost, the MySQL driver uses a Unix socket instead of TCP. If the socket is not where it expects, the connection fails. 127.0.0.1 forces TCP; otherwise set the socket path with DB_SOCKET.
Migrations work but the site shows error 2002, why?
The CLI and the web server do not see the same environment: cached config on the web side, a different container, or a different .env. Run php artisan config:clear and check that both run in the same place.
What is the difference between error 2002 and error 1045?
2002: the MySQL server cannot be reached. 1045: the server is reachable but rejects the username or password.
Useful tools for this error
More PHP & Laravel errors
- Fix "419 Page Expired" in Laravel (CSRF token mismatch)
- Fix "Allowed memory size of 134217728 bytes exhausted" in PHP
- Fix "Target class [X] does not exist" and "Class not found" in Laravel
- Fix "No application encryption key has been specified" in Laravel
- Fix "Your requirements could not be resolved to an installable set of packages" (Composer)
- Fix "laravel.log could not be opened in append mode: Permission denied"
- Fix "Vite manifest not found at: public/build/manifest.json" in Laravel