# SQLSTATE[HY000] [2002] Connection refused

PHP could not open a connection to MySQL at the host and port Laravel used. Start the server or correct DB_HOST and DB_PORT, then clear the config cache.

Message:

```text
SQLSTATE[HY000] [2002] Connection refused
```

Exception:

```text
Illuminate\Database\QueryException
```

## What it means

PHP tried to connect to MySQL and nothing accepted the connection at that address. No SQL ran: PDO threw a `PDOException` and Laravel wrapped it in `Illuminate\Database\QueryException`. Laravel appends the address it used, such as `(Connection: mysql, Host: 127.0.0.1, Port: 3399, ...)`, or `Socket: ...` when `DB_SOCKET` is set. Read that first: it shows the values the app really used, which are not always the ones in your `.env`.

## Why it happens

- MySQL is not running, or it listens on another port. A `DB_PORT` that points at a closed port gives exactly this message.
- `DB_HOST=localhost` does not mean `127.0.0.1`. The PHP manual says that on Unix the host name `localhost` connects through a socket file instead of TCP. If that file is not where PHP looks (the `pdo_mysql.default_socket` setting), you get a sibling of this error: `SQLSTATE[HY000] [2002] No such file or directory`.
- The host name does not resolve. Laravel Sail tells you to set `DB_HOST=mysql`, a name that only exists on the container network. Run the same app outside the containers and you get the third sibling: `SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: nodename nor servname provided, or not known`. The words after `failed:` come from the operating system, so they read differently on Linux.
- The config cache is stale. After `php artisan config:cache`, Laravel no longer reads `.env`. Correcting the port in `.env` changes nothing until you rebuild the cache: the message keeps showing the old port.

## How to fix it

Check what Laravel uses and whether it connects:

```bash
php artisan config:show database.connections.mysql
php artisan db:show
```

`config:show` prints the values from the config cache when there is one, so a wrong port there and a right one in `.env` points at a stale cache.

If MySQL is not running, start it. If the port is wrong, set the one your server listens on:

```ini
DB_HOST=127.0.0.1
DB_PORT=3306
```

For a `No such file or directory` error, either use `127.0.0.1` so PHP connects over TCP, or keep `localhost` and tell Laravel where the socket is. It is often `/tmp/mysql.sock` with Homebrew and `/var/run/mysqld/mysqld.sock` on Debian and Ubuntu. Ask MySQL for the path:

```sql
SHOW VARIABLES LIKE 'socket';
```

```ini
DB_HOST=localhost
DB_SOCKET=/tmp/mysql.sock
```

For a `getaddrinfo` error, use a host name that resolves where the code runs. Inside Sail's containers that is `mysql`. From your own machine, connect to the port Sail forwards: `DB_HOST=127.0.0.1` and `DB_PORT=3306`, or the value of `FORWARD_DB_PORT`.

After any change to `.env` on a server that caches its config, rebuild the cache:

```bash
php artisan config:cache
```

## Find it in production

With `APP_DEBUG=false` the visitor gets a 500 page titled "Server Error". With the default `SESSION_DRIVER=database`, every page that uses the `web` middleware fails this way, even a page that runs no query of its own, because Laravel reads the session from the `sessions` table first. The built-in `/up` health route kept answering 200 while the database was down, so a check on `/up` does not catch this, unless a listener on `Illuminate\Foundation\Events\DiagnosingHealth` runs `DB::select('select 1')`. Then `/up` answers 500 too.

Laravel reports the exception: each failed request writes a `SQLSTATE[HY000] [2002]` line at error level to `storage/logs/laravel.log`. Ranetrace's [uptime check](/features/uptime-monitoring) requests your site about every minute and alerts you once it has been down for two minutes.

Reproduced on Laravel 13.45, PHP 8.5, MySQL 8.0.27, 5 December 2026.

Read more: https://laravel.com/docs/database

## Related

### Terms

- [Configuration caching in Laravel](https://ranetrace.com/glossary/configuration-caching-in-laravel.md)
- [Uptime monitoring](https://ranetrace.com/glossary/uptime-monitoring.md)

### Other errors

- [SQLSTATE\[42S02\]: Base table or view not found: 1146 Table doesn't exist](https://ranetrace.com/error-guides/laravel-sqlstate-1146-table-not-found.md)
- [No application encryption key has been specified](https://ranetrace.com/error-guides/laravel-no-application-encryption-key.md)
- [SQLSTATE\[HY000\] \[1045\] Access denied for user](https://ranetrace.com/error-guides/laravel-sqlstate-1045-access-denied.md)

Read this guide in a browser: https://ranetrace.com/error-guides/laravel-sqlstate-2002-connection-refused
