## Summary

Laravel shows `SQLSTATE[HY000] [2002] Connection refused` (or `No such file or directory`, or `php_network_getaddresses: getaddrinfo failed`) when PHP cannot open a connection to the database server at all. It is not a bad password and not a missing table: the connection never gets as far as logging in. It typically appears right after a deploy, a Docker or Sail change, a server restart or a cached config.

This guide gives you the fast isolation steps first. If you would rather hand it over, SazM fixes this error for an affordable, low-cost **$99 fixed price, delivered within 48 hours**, entirely in writing.

## Where the Error Lives

```text
storage/logs/laravel.log
[2026-11-24 08:12:41] production.ERROR: SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from `sessions` where `id` = ... limit 1)

SQLSTATE[HY000] [2002] No such file or directory
  -> PHP tried a Unix socket (DB_HOST=localhost) and the socket file does not exist at that path

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known
  -> DB_HOST is a Docker service name, but the app is running outside that Docker network

SQLSTATE[HY000] [2002] Connection timed out
  -> a firewall or security group drops the packets instead of refusing them
```

The four wordings point to four different causes, so read the exact text before changing anything.

## Quick Fix Breakdown (DIY)

- **Check what Laravel actually reads.** A cached config ignores `.env` edits:

```bash
php artisan config:clear
php artisan tinker --execute="dump(config('database.connections.mysql.host'), config('database.connections.mysql.port'));"
```

  In production, `php artisan config:cache` freezes the values; run it again only after `.env` is correct. Also note that `env()` outside config files returns `null` once config is cached.
- **Make sure the database server is running and listening.**

```bash
sudo systemctl status mysql        # or mariadb
sudo ss -ltnp | grep 3306
mysqladmin -h 127.0.0.1 -P 3306 -u <user> -p status
```

  If nothing listens on 3306, start the service and read `/var/log/mysql/error.log`. A MySQL process killed by the out-of-memory killer after a deploy is a common cause.
- **Match `DB_HOST` to where the app runs.**

```dotenv
# app and MySQL on the same server
DB_HOST=127.0.0.1
DB_PORT=3306

# Laravel Sail / docker compose, app inside the container
DB_HOST=mysql          # the service name from docker-compose.yml

# app on your host, MySQL in a Sail container
DB_HOST=127.0.0.1
DB_PORT=3306           # or the FORWARD_DB_PORT you set
```

  `localhost` makes the MySQL driver use a Unix socket; `127.0.0.1` forces TCP. If you get "No such file or directory", switch to `127.0.0.1` or set `unix_socket` in `config/database.php` to the path shown by `mysql_config --socket`.
- **Check bind-address for a remote database.** If MySQL is on another machine, `bind-address = 127.0.0.1` in `/etc/mysql/mysql.conf.d/mysqld.cnf` refuses outside connections. The user must also be allowed from the app's host (`'user'@'app-ip'`), and the firewall must allow 3306 from that IP only.
- **Test from the app server with a plain client**, bypassing Laravel entirely:

```bash
php -r 'new PDO("mysql:host=127.0.0.1;port=3306", "user", "pass"); echo "ok\n";'
```

  If this fails too, Laravel is not the problem.
- **Restart long-running workers.** Queue workers and Octane keep the old config in memory:

```bash
php artisan queue:restart
```

  A connection that dies mid-job shows as `MySQL server has gone away`, which is a related but different error.
- **Managed databases:** many require TLS and an allow-listed IP. Check the provider's trusted-sources list and add the `MYSQL_ATTR_SSL_CA` option if the provider requires it.

If none of these isolates it within an hour, stop. Every extra hour of guessing is downtime.

## Hand It Over: $99 Fixed, Within 48 Hours

Most connection errors are quick once someone reads the exact message, checks the server, the `.env`, the cached config and the container or firewall layer together. They take long when the host, the deploy tool and the developer each assume the problem is elsewhere.

What you get for **$99**:

- **A fixed price agreed before work starts.** One error, one flat fee, no hourly meter.
- **Delivery within 48 hours** of access, with changes tested on staging or a copy before they touch production.
- **Fully written, fully async.** You send the URL, the exact error and what changed recently. You get questions, progress and proof in writing. There are no calls, meetings or project managers in between, so your time zone does not matter.
- **Written proof it works:** the root cause in plain language, what changed, before and after evidence, and a rollback note.
- **Money-back guarantee** if the agreed root cause cannot be resolved.
- **Safe access only:** a separate limited account, staging first, rotated credentials afterwards. Never send passwords through the inquiry form.

Behind it: 20+ years in production (since Oct 2006) and 100+ projects in the portfolio.

**[Get this error fixed for $99 →](/bug-fix-services)** · [Queue an urgent fix](/checkout/emergency) · [See past projects](/projects)

For wider application work, see [PHP and Laravel development](/php-development-services). If the app loads but a form fails with 419, see [Laravel 419 Page Expired fix](/articles/laravel-419-page-expired-fix). If the page loads without styles, see [Laravel Vite manifest not found](/articles/laravel-vite-manifest-not-found-fix). For a general written process, see [how to get a production bug fixed in 48 hours](/articles/fix-production-bug-in-48-hours-async).

## Frequently Asked Questions

### What does SQLSTATE[HY000] [2002] Connection refused mean in Laravel?
PHP could not open a network or socket connection to the database server. Either the server is not running or not listening on that host and port, `DB_HOST` points to the wrong place, cached config holds old values, or a firewall blocks the connection.

### Why does it say "No such file or directory" instead?
With `DB_HOST=localhost`, MySQL's PHP driver uses a Unix socket. If the socket file is not where PHP expects it, you get this message. Use `DB_HOST=127.0.0.1` to force TCP, or set the correct `unix_socket` path.

### Why does it work locally but fail after deploy?
The `.env` on the server often differs from local, Docker service names do not resolve outside Docker, or `config:cache` froze old values. Clear the config cache, then verify host, port and a plain PDO connection from the server.

### How much does it cost to fix a Laravel database connection error?
SazM fixes one issue for a fixed $99, delivered within 48 hours, with a money-back guarantee if the agreed root cause cannot be resolved. Typical agency hourly rates are higher and open-ended, while very low marketplace gig prices rarely include root-cause diagnosis, a staging test or a guarantee.

### Do I need a call to explain the problem?
No. Send the URL, the error text and how you deploy. A written report is more precise than a call.

If CodeIgniter routes return 404 instead, see the [CodeIgniter 404 fix](/codeigniter-404-not-found-fix).
