MySQL Error 2002 occurs when the client cannot locate or access the Unix socket file used for local MySQL connections. This typically means the MySQL server is not running, the socket path is incorrect, or there are permission issues. Fix it by checking if the server is running, verifying socket file location and permissions, or using TCP/IP instead of socket connections.
ERROR 2002 is a connection error that occurs when the MySQL client attempts to establish a local connection via Unix socket but fails to find or communicate with the socket file. Unlike network errors, socket-based connections do not use TCP/IP—they use a special file on the filesystem (typically `/var/run/mysqld/mysqld.sock` or `/tmp/mysql.sock`). When MySQL is not running, the socket file does not exist. If permissions are wrong, the client user cannot access it. When client and server configurations disagree about the socket location, the client looks in the wrong place. This error only occurs for local connections; remote connections use TCP/IP instead.
First, verify that the MySQL server process is actually running. On Linux systems with systemd:
sudo systemctl status mysqlOr on systems using service command:
sudo service mysql statusOn macOS with Homebrew:
brew services list | grep mysqlYou should see output indicating the service is active/running. If it is not running or shows as inactive, the server needs to be started (see Step 2).
To start the MySQL server, use the appropriate command for your system.
On Linux with systemd:
sudo systemctl start mysql
# or
sudo systemctl start mysqldOn Linux with service command:
sudo service mysql startOn macOS with Homebrew:
brew services start mysqlWait a few seconds for the server to fully start, then try connecting again:
mysql -u root -pIf the server fails to start, check the MySQL error log (Step 7) for startup errors.
Check if the socket file exists. The default location varies by system and MySQL configuration:
ls -la /var/run/mysqld/mysqld.sockOr try the alternate common location:
ls -la /tmp/mysql.sockIf you get "No such file or directory," the socket file does not exist. This confirms the MySQL server is either not running or not creating the socket properly. Start the server and wait a few seconds (Step 2), then check again.
You can also find the socket location by checking the MySQL configuration file:
grep socket /etc/mysql/my.cnf
# or
grep socket /etc/my.cnf
# or on macOS:
grep socket /usr/local/etc/my.cnfVerify that the socket file and its parent directory have correct permissions. The socket directory should allow the MySQL user to write and the client user to access it:
ls -la /var/run/mysqld/The permissions should typically be:
- Directory: drwxr-xr-x (755) owned by mysql:mysql
- Socket file: srwxrwxrwx (777) owned by mysql:mysql
If permissions are wrong, fix them:
sudo chown mysql:mysql /var/run/mysqld/
sudo chmod 755 /var/run/mysqld/If the socket file itself has wrong permissions (rare):
sudo chown mysql:mysql /var/run/mysqld/mysqld.sock
sudo chmod 777 /var/run/mysqld/mysqld.sockIf you are running the client as root, you should have permission. If running as a regular user, ensure that user is in the mysql group:
sudo usermod -a -G mysql your_usernameYou may need to log out and back in for group membership to take effect.
If you have multiple MySQL configuration files, the client and server may be using different socket paths. Check what the server is actually using:
mysql_config_editor print --allOr query the running server for the socket path (if you can connect via TCP/IP):
mysql -h 127.0.0.1 -u root -p -e "SHOW VARIABLES LIKE 'socket'"Then check your client configuration in ~/.my.cnf or /etc/mysql/my.cnf:
cat ~/.my.cnfMake sure the [client] section specifies the same socket path as [mysqld]:
[mysqld]
socket=/var/run/mysqld/mysqld.sock
[client]
socket=/var/run/mysqld/mysqld.sockIf they differ, update the client configuration to match the server's socket path and restart MySQL.
If you cannot resolve the socket issue immediately, you can connect using TCP/IP instead by explicitly specifying the host as an IP address:
mysql -h 127.0.0.1 -u root -pOr for remote connections:
mysql -h mysql.example.com -u root -pThis bypasses the socket mechanism entirely and uses the network layer. This works if the MySQL server is running and accepting TCP/IP connections on port 3306 (the default). This is a temporary workaround; investigate the root cause using Steps 1-4 for a permanent fix.
You can also update your connection configuration to prefer TCP/IP. In ~/.my.cnf:
[client]
host=127.0.0.1
port=3306
user=rootIf the server fails to start or exits immediately, the error log will contain details. Check the MySQL error log:
sudo tail -f /var/log/mysql/error.logOr on some systems:
sudo tail -f /var/log/mysqld.logOr on macOS with Homebrew:
tail -f /usr/local/var/mysql/*.errCommon errors include:
- "Address already in use" – MySQL is already running or another service is using port 3306
- "Permission denied" – MySQL user does not have permission to access data directory
- "Corrupted myisam table" – Data files are corrupted; you may need to repair tables
- "InnoDB: Unable to lock" – MySQL data directory is locked by another instance
Search the error message online or consult the MySQL documentation for the specific error code you see.
Error 2002 is specific to Unix socket connections and only occurs on Linux/Unix systems. Windows MySQL installations do not use socket files; they use named pipes or TCP/IP instead. If your application is containerized (Docker), be aware that socket paths inside the container may differ from the host system. When using Docker, it is common to share the socket via a volume mount (e.g., -v /var/run/mysqld:/var/run/mysqld), but both the container and host must agree on the path. If MySQL is installed via package manager, the socket typically goes to /var/run/mysqld/mysqld.sock. If compiled from source or installed via container, it may default to /tmp/mysql.sock. Some distributions (like Ubuntu 20.04+) use a socket authentication plugin that allows socket connections without a password for the root user; this is secure and intentional. Finally, systemd may clean up /tmp on reboot, so if MySQL uses /tmp/mysql.sock, the socket may disappear after restart. Changing the socket location to /var/run/mysqld/mysqld.sock (which is preserved) is more reliable.
EE_WRITE (3): Error writing file
How to fix "EE_WRITE (3): Error writing file" in MySQL
CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters
How to fix "CR_PARAMS_NOT_BOUND (2031): No data supplied for parameters" in MySQL
CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed
How to fix "CR_DNS_SRV_LOOKUP_FAILED (2070): DNS SRV lookup failed" in MySQL
ERROR 1146: Table 'database.table' doesn't exist
How to fix "ERROR 1146: Table doesn't exist" in MySQL
ERROR 1040: Too many connections
How to fix "ERROR 1040: Too many connections" in MySQL