Fix this error
SQLSTATE[HY000] [2002] Connection refused
Dasun Kalupahana 160 views
The error
Error
SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from `users` ...)
Why it happens
PHP reached the database host, but nothing accepted the connection on that port. Usually:
- MySQL / MariaDB is not running.
DB_HOSTorDB_PORTin.envis wrong.- In Docker,
127.0.0.1points at the app container itself, not the database container. - The config is cached with old values.
How to fix it
- Start the database (XAMPP: start MySQL; Linux:
sudo systemctl start mysql). - Check
DB_HOSTandDB_PORTin.env. Locally use127.0.0.1and3306; in Docker Compose use the service name, e.g.mysql. - Clear the cached config:
php artisan config:clear. - Test the connection with
php artisan migrate:status.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 # Docker Compose: the service name, e.g. mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Still stuck? Send us the full error and what you tried. We add the answers here.
Learn more on the blog
MySQL
PostgreSQL vs MySQL Performance: Real Benchmarks Compared
Last updated: 7 November 2026. See the update log at the end."Which...
8 min read
PHP
How to Detect and Fix PHP Memory Leaks
Last updated: 5 November 2026. See the update log at the end.In...
9 min read
Docker
Docker Build Too Slow? 10 Ways to Speed It Up
A slow Docker build usually has one of a few causes: you...
8 min read