Skip to content
SazM Fixed-price website fixes
Find a service
Where can I help?

Use arrow keys to browse, Enter to open, and Escape to close.

11 destinations

Client Login Get a fix →

Bug Fixing · 5 min read

Laravel SQLSTATE[HY000] [2002] Connection Refused: Affordable $99 Fix

Fix Laravel SQLSTATE[HY000] [2002] Connection refused: wrong DB_HOST, cached config, stopped MySQL, Docker or Sail hosts, sockets and firewalls, with a $99 fixed-price option.

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

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:
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.
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.
# 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:
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:
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 → · Queue an urgent fix · See past projects

For wider application work, see PHP and Laravel development. If the app loads but a form fails with 419, see Laravel 419 Page Expired fix. If the page loads without styles, see Laravel Vite manifest not found. For a general written process, see how to get a production bug fixed in 48 hours.

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.

// SazM ENGINEERING DISPATCH · PRODUCTION REMEDIATION

Senior Engineering Without Agency Overhead

// TRACK A · FIXED-PRICE FIXES (48H SLA)

Single Urgent or Speed Fix

Immediate code-level fixes with zero discovery calls. Emergency root-cause patch within 48 hours for $99, or full Core Web Vitals & speed remediation with before/after audit for $199. 100% money back if the agreed root cause cannot be fixed.

// TRACK B · SENIOR RETAINER & BUILDS

Monthly Plan or Custom Scope

Direct senior-level capacity embedded in your stack. Dedicated monthly engineering ($999/mo, 1 active task at a time, 24–48h PR delivery, pause or cancel anytime), or fixed-scope architectural milestones delivered within a 24h written quote.

✓ 20+ years engineering ✓ 100+ projects in the portfolio ✓ Fixes within 48 hours ✓ 100% money back if not fixed
1 Active Task at a Time
Requests are completed one by one in your priority order. As soon as an active task is shipped, tested, and approved, work starts immediately on your next task within our 24–48h SLA.
48h Emergency SLA
Clock starts as soon as site access details are provided. Delivered with full backup taken first, isolated staging testing, and 100% money back if not fixed.
WordPress Care Plan Scope
Weekly core and plugin updates pre-tested in staging, daily offsite encrypted backups, 24/7 uptime monitoring, and proactive security patching.
Pay Nothing Upfront
Zero upfront payment or credit card required. A 20-Year Senior Engineer diagnoses your issue, reproduces it on isolated staging, and deploys the fix. You only pay once you verify the staging reproduction or production fix.