Knowledge

SQLSTATE[HY000] [2002] Connection refused

#Databases

This error means your application could not even reach the MySQL server. Unlike access denied, the credentials were never checked, the host or port is wrong or the database is not running.

Published by Mark van Eijk on June 23, 2026 · 1 minute read

  1. About the error
  2. Why do I see this error
  3. Solution

About the error

You'll see one of these two variants:

SQLSTATE[HY000] [2002] Connection refused
SQLSTATE[HY000] [2002] No such file or directory

This is different from a 1045 Access denied error. There the server rejected your login. Here the server was never reached at all, so it never got as far as checking a username or password.

Why do I see this error

  • MySQL isn't running.
  • DB_HOST or DB_PORT points to the wrong place.
  • The classic mix-up: Connection refused over TCP versus No such file or directory over a socket.

The two messages are a useful hint:

  • Connection refused means the app tried to reach MySQL over TCP (a host and port) and nothing was listening there.
  • No such file or directory means the app tried to use a Unix socket and the socket file wasn't where it expected.

Solution

First, make sure MySQL is actually running:

systemctl status mysql
systemctl start mysql

Then check your host and port. Using 127.0.0.1 forces a TCP connection, while localhost makes MySQL use a Unix socket, which is a frequent source of confusion:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306

Confirm MySQL is listening on that port:

ss -tlnp | grep 3306

Test the connection independently of your app:

mysql -u forge -p -h 127.0.0.1 -P 3306

If you're running MySQL in Docker, 127.0.0.1 from inside another container points at the container itself, not the database. Use the service name from your compose file as the host instead:

DB_HOST=mysql

After changing anything in .env, clear the cached config:

php artisan config:clear

Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

Related articles

Stream MySQL backup directly to S3 bucket

This error means your application could not even reach the MySQL server. Unlike access denied, the credentials were never checked, the host or port is wrong or the database is not running.

Read more →

Export MySQL database using command line

This error means your application could not even reach the MySQL server. Unlike access denied, the credentials were never checked, the host or port is wrong or the database is not running.

Read more →