Skip to content
FR
  1. LuzineDebug
  2. PHP & Laravel
  3. SQLSTATE[HY000] [2002] Connection refused (Conne…
PHP & Laravel Beginner ≈ 10 min to fix

Fix "SQLSTATE[HY000] [2002] Connection refused" in Laravel

Error message
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
In short

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.

Be the first to confirm this fix

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

  1. MySQL is not running: stopped service, stopped Docker container, or MySQL crashing on startup.
  2. Wrong host or port: port 3307 or 8889 (MAMP) instead of 3306, or an address that does not match where the database runs.
  3. Docker confusion: inside a container, 127.0.0.1 is the container itself, you need the service name (mysql). Conversely, outside Docker the service name does not resolve.
  4. Cached configuration: after php artisan config:cache, changes to .env are ignored until the cache is cleared.
  5. Unreachable remote server: MySQL only listens on 127.0.0.1 (bind-address) or a firewall blocks port 3306.

Step-by-step fix

  1. Check that MySQL is running

    Commands depend on your system and how MySQL is installed (MySQL or MariaDB).

    bash
    # 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
  2. Test the connection outside Laravel

    If the mysql client fails too, the problem is not Laravel. On Windows, Test-NetConnection 127.0.0.1 -Port 3306 checks that the port answers.

    bash
    mysql -h 127.0.0.1 -P 3306 -u laravel -p
    nc -zv 127.0.0.1 3306
  3. Fix .env

    Use 127.0.0.1 rather than localhost to force a TCP connection, and your server’s real port.

    ini
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=laravel
    DB_USERNAME=laravel
    DB_PASSWORD=secret
  4. Adapt DB_HOST to Docker or Sail

    If Laravel runs in a container, DB_HOST must be the MySQL service name from compose.yaml (mysql with Sail), and DB_PORT the internal port 3306. If Laravel runs on your machine and MySQL in Docker, use 127.0.0.1 and the published port (ports: "3306:3306"). With Sail, run commands through ./vendor/bin/sail artisan.

    ini
    # Laravel inside Docker / Sail
    DB_HOST=mysql
    DB_PORT=3306
  5. Clear the config cache and verify

    Laravel may still be reading an old configuration. Clear it, then check the connection.

    bash
    php artisan config:clear
    php artisan db:show
  6. 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.1 and DB_PORT=3307. If you really must expose MySQL, restrict access by firewall to allowed IPs only.

    bash
    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 healthcheck on MySQL and depends_on: condition: service_healthy so the app waits until the database is ready.
  • Keep .env.example up to date with the expected DB_HOST and DB_PORT values for each way of running the project.
  • Only run php artisan config:cache in production, and run it again after every .env change.

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

All PHP & Laravel errors →