PDOException: SQLSTATE[HY000] [2002] Connection refused
PHP couldn’t reach the database server at the host/port in your DSN.
Seen on:
PHP
Meaning
“Connection refused” means nothing listens there; “No such file or directory” means the Unix socket wasn’t found (host=localhost). In Docker the DB is another container, not localhost.
Common causes
- Database server not running
- Wrong host/port in .env
- localhost (socket) vs 127.0.0.1 (TCP) mismatch
- Docker: using localhost instead of the service name
⚡ Quick fix
- Start the database service
- Try
host=127.0.0.1to force TCP - In Docker Compose use the service name (
DB_HOST=db) - Run
php artisan config:clearafter changing .env (Laravel)
Detailed fix by platform
PHP
- Test with a tiny script:
new PDO('mysql:host=127.0.0.1;dbname=app', 'user', 'pass');
How to diagnose
- Server — Is MySQL/PostgreSQL running?
- Address — Host and port in the DSN
- Network — Container/firewall in between?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026