Skip to content

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_HOST or DB_PORT in .env is wrong.
  • In Docker, 127.0.0.1 points at the app container itself, not the database container.
  • The config is cached with old values.

How to fix it

  1. Start the database (XAMPP: start MySQL; Linux: sudo systemctl start mysql).
  2. Check DB_HOST and DB_PORT in .env. Locally use 127.0.0.1 and 3306; in Docker Compose use the service name, e.g. mysql.
  3. Clear the cached config: php artisan config:clear.
  4. 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

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.