MW

How-To · Database

MySQL Error 2002 (HY000) 'Can't connect to localhost (10061)' — 7 Fixes (2026)

Error 2002 means your client can't reach the MySQL server — service is down, port is wrong, socket file is missing, or firewall is blocking. Here are the 7 things to check, in order.

Jan 24, 2025 5 min read Any platform intermediate
Advertisement

MySQL Error 2002 (HY000) means your client can’t reach the MySQL server. The error appears in three slightly different forms depending on what you connected with:

Terminal — what error 2002 looks like
$ mysql -u root -p
Enter password: ******** ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
$ mysql -h localhost -u root -p
Enter password: ******** ERROR 2002 (HY000): Can't connect to MySQL server on 'localhost' (10061)

The error code breakdown: 2002 = client-side connection error; HY000 = generic SQL state. (10061) on Windows means “connection actively refused” — the OS got there but nothing answered. (2) on Linux means “no such file” — the socket file is missing.

This guide walks 7 root causes in order of likelihood.

1. MySQL service isn’t running

This is the cause 90% of the time. Check + start:

Command Prompt — Windows service check
C:\> net start MySQL80
The MySQL80 service is starting. The MySQL80 service was started successfully.
C:\> sc query MySQL80
SERVICE_NAME: MySQL80 TYPE: 16 WIN32_OWN_PROCESS STATE: 4 RUNNING
Terminal — macOS via Homebrew
$ brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
$ brew services list
Name Status User File mysql started mahesh ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Terminal — Linux via systemd
$ sudo systemctl start mysql
$ sudo systemctl status mysql
* mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2026-05-13 09:42:18 UTC; 3s ago

2. Wrong port

MySQL defaults to 3306. If you (or your installer) changed it, your client needs to know:

Terminal — discover the actual port
$ sudo netstat -tlnp | grep mysql
tcp 0 0 127.0.0.1:3307 0.0.0.0:* LISTEN 1234/mysqld
$ mysql -h localhost -P 3307 -u root -p
Enter password: ******** Welcome to the MySQL monitor.

On Windows: netstat -ano | findstr 3306 or check C:\ProgramData\MySQL\MySQL Server 8.0\my.ini for the port setting.

3. Socket file missing (Linux/Mac only)

On Linux + macOS, MySQL clients use a Unix socket by default for localhost connections (faster than TCP). If MySQL is running but the socket file is missing or in the wrong place, you get error 2002.

Terminal — find the socket
$ mysql_config --socket
/var/run/mysqld/mysqld.sock
$ ls -la /var/run/mysqld/mysqld.sock
srwxrwxrwx 1 mysql mysql 0 May 13 09:42 /var/run/mysqld/mysqld.sock

If the file is missing, MySQL didn’t fully start (check the service status from step 1). If it’s in a non-default location, point your client there:

mysql -u root -p --socket=/tmp/mysql.sock

Or force TCP to sidestep sockets entirely:

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

4. Firewall blocking the port

Mostly relevant if connecting from another machine, or if you’re on a corporate / antivirus-restricted Windows machine.

Command Prompt — allow MySQL through Windows Firewall
C:\> netsh advfirewall firewall add rule name="MySQL" dir=in action=allow protocol=TCP localport=3306
Ok.

On Linux:

sudo ufw allow 3306/tcp

5. bind-address restricting connections

In my.cnf / my.ini, the bind-address directive limits which interfaces MySQL listens on. Common gotcha: setting it to 127.0.0.1 blocks every connection except from the same machine.

Terminal — check bind-address
$ grep bind-address /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 127.0.0.1

To accept connections from other machines, change to 0.0.0.0 (any interface) or your specific network IP, then restart MySQL.

6. Wrong credentials cached

You can also get error 2002 in some edge cases where the client uses a cached connection profile pointing at the wrong host. Easy diagnostic: explicitly pass everything on the command line:

Terminal — bypass any cached profile
$ mysql -h 127.0.0.1 -P 3306 -u root -p --protocol=TCP
Enter password: ******** Welcome to the MySQL monitor.

If this works but mysql -u root -p alone fails, your local ~/.my.cnf or [client] section in /etc/mysql/my.cnf has a stale setting. Check and update.

7. MySQL didn’t fully start

Check the error log for crash reasons:

Terminal — read MySQL error log
$ sudo tail -30 /var/log/mysql/error.log
2026-05-13T09:42:18.123456Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33-0ubuntu0.22.04.2) starting as process 12345 2026-05-13T09:42:18.234567Z 0 [ERROR] [MY-013183] [InnoDB] Assertion failure...

On Windows: check C:\ProgramData\MySQL\MySQL Server 8.0\Data\<hostname>.err.

Common causes the log will reveal: disk full, corrupted InnoDB tables, port already in use by another process, missing data directory.

Quick reference

CauseFast diagnosticFix
Service not runningsc query MySQL80 (Win), brew services list (Mac), systemctl status mysql (Linux)Start it
Wrong portnetstat -tlnp | grep mysqlPass -P <port>
Socket file missingls /var/run/mysqld/mysqld.sockUse -h 127.0.0.1 to force TCP
Firewall blockingTry connecting from same machine firstAllow port 3306
bind-address limitgrep bind-address my.cnfChange to 0.0.0.0 if needed
Cached profilemysql -h 127.0.0.1 --protocol=TCP works but mysql doesn’tClean ~/.my.cnf
Service crashedtail /var/log/mysql/error.logResolve whatever the log says

Conclusion

Error 2002 always means “client can’t reach server” — but the reason varies. Walk the 7 causes in order:

  1. Service running? (90% of cases)
  2. Right port?
  3. Socket file present? (Linux/Mac)
  4. Firewall open?
  5. bind-address not blocking?
  6. No cached bad profile?
  7. What does the error log say?

The first 3 cover almost every case in dev environments. The last 4 matter more for shared/server setups.

MySQL Database Troubleshooting Connection Error
Advertisement

Get weekly notes in your inbox

Practical tips, tutorials and resources. No spam.