Error Reference › SQL / Database
SQL / Database

Can't connect to MySQL (2002)

The client couldn't reach the MySQL server at all.

What it looks like

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
SQLSTATE[HY000] [2002] Connection refused

What does "Can't connect to MySQL (2002)" mean?

Returned by the MySQL client or driver (PDO, mysqli, Node drivers) before any login happens — the server could not be reached at all. The socket wording means it tried a local Unix socket; “Connection refused” means it tried TCP.

What causes "Can't connect to MySQL (2002)"?

  • MySQL isn't running.
  • Wrong host/port or socket path.
  • Firewall blocking 3306.

How do I confirm the cause?

  1. Check the server is running (systemctl status mysql, or your host’s control panel).
  2. Note the difference between localhost and 127.0.0.1: many clients treat localhost as a socket connection and 127.0.0.1 as TCP.
  3. For a remote database, check the port (3306) is open from your app server and that MySQL’s bind-address allows outside connections.
  4. Read the MySQL error log (often /var/log/mysql/error.log) — if the server keeps stopping, the reason, such as a full disk or a bad setting, is written there.

How do I fix "Can't connect to MySQL (2002)"?

  • Start the MySQL service.
  • Check host, port (3306), and socket in your config.
  • Confirm the port is reachable.

How do I stop it happening again?

  • Enable the database service to start on boot, and monitor it.
  • In containers, make the app wait for the database to be healthy before connecting.
Got a different error? Paste it into the Explain This Error tool → Identifies the family and the fix — runs locally, nothing uploaded.

Related errors