How to fix ERROR 2002: Can't connect to local MySQL server through socket in MySQL

MySQLBEGINNERHIGHUpdated Dec 25, 2025· 142 views

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.

Quick fix

Fixes ERROR 2002: Can't connect to local MySQL server through socket

sudo systemctl status mysql
  1. Check if MySQL server is running
  2. Start the MySQL server if it is not running
  3. Verify the socket file exists at the expected location
  4. Fix socket file permissions if needed
mysql — error
sudo systemctl status mysql
ERROR 2002: Can't connect to local MySQL server through socket

What this error means

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.

Typical symptoms

  • Cannot connect to MySQL using command `mysql` without specifying a host
  • Error message shows socket file path (e.g., `/var/run/mysqld/mysqld.sock`)
  • Database client or application fails when connecting to localhost
  • Error persists even though credentials (username/password) are correct
  • Can connect using TCP/IP (127.0.0.1) but not via local socket
  • After MySQL restart or system reboot, connection fails
  • Multiple MySQL instances running causes one to own the socket

Common causes

  1. MySQL server is not running or failed to start
  2. Socket file does not exist at the expected location
  3. Client is looking for the socket in a different directory than where the server created it
  4. Incorrect permissions on the socket file or its parent directory
  5. User running the client does not have read/write access to the socket file
  6. Configuration mismatch between client and server (different socket paths in my.cnf)
  7. Multiple MySQL instances running with conflicting socket locations
  8. MySQL crashed or was killed without proper shutdown
  9. Socket file was deleted or cleaned up by system maintenance

How to fix "ERROR 2002: Can't connect to local MySQL server through socket"

1Check if MySQL server is running

First, verify that the MySQL server process is actually running. On Linux systems with systemd:

bash
sudo systemctl status mysql

Or on systems using service command:

bash
sudo service mysql status

On macOS with Homebrew:

bash
brew services list | grep mysql

You 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).

2Start the MySQL server if it is not running

To start the MySQL server, use the appropriate command for your system.

On Linux with systemd:

bash
sudo systemctl start mysql
# or
sudo systemctl start mysqld

On Linux with service command:

bash
sudo service mysql start

On macOS with Homebrew:

bash
brew services start mysql

Wait a few seconds for the server to fully start, then try connecting again:

bash
mysql -u root -p

If the server fails to start, check the MySQL error log (Step 7) for startup errors.

3Verify the socket file exists at the expected location

Check if the socket file exists. The default location varies by system and MySQL configuration:

bash
ls -la /var/run/mysqld/mysqld.sock

Or try the alternate common location:

bash
ls -la /tmp/mysql.sock

If 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:

bash
grep socket /etc/mysql/my.cnf
# or
grep socket /etc/my.cnf
# or on macOS:
grep socket /usr/local/etc/my.cnf
4Fix socket file permissions if needed

Verify 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:

bash
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:

bash
sudo chown mysql:mysql /var/run/mysqld/
sudo chmod 755 /var/run/mysqld/

If the socket file itself has wrong permissions (rare):

bash
sudo chown mysql:mysql /var/run/mysqld/mysqld.sock
sudo chmod 777 /var/run/mysqld/mysqld.sock

If 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:

bash
sudo usermod -a -G mysql your_username

You may need to log out and back in for group membership to take effect.

5Reconcile socket paths between client and server configuration

If you have multiple MySQL configuration files, the client and server may be using different socket paths. Check what the server is actually using:

bash
mysql_config_editor print --all

Or query the running server for the socket path (if you can connect via TCP/IP):

bash
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:

bash
cat ~/.my.cnf

Make sure the [client] section specifies the same socket path as [mysqld]:

ini
[mysqld]
socket=/var/run/mysqld/mysqld.sock

[client]
socket=/var/run/mysqld/mysqld.sock

If they differ, update the client configuration to match the server's socket path and restart MySQL.

6Use TCP/IP connection instead of socket (workaround)

If you cannot resolve the socket issue immediately, you can connect using TCP/IP instead by explicitly specifying the host as an IP address:

bash
mysql -h 127.0.0.1 -u root -p

Or for remote connections:

bash
mysql -h mysql.example.com -u root -p

This 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:

ini
[client]
host=127.0.0.1
port=3306
user=root
7Review MySQL error logs for startup issues

If the server fails to start or exits immediately, the error log will contain details. Check the MySQL error log:

bash
sudo tail -f /var/log/mysql/error.log

Or on some systems:

bash
sudo tail -f /var/log/mysqld.log

Or on macOS with Homebrew:

bash
tail -f /usr/local/var/mysql/*.err

Common 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.

Advanced notes

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.

Frequently asked questions

What does "ERROR 2002: Can't connect to local MySQL server through socket" mean?
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. sock). When MySQL is not running, the socket file does not exist.
How do I fix "ERROR 2002: Can't connect to local MySQL server through socket"?
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.
What causes "ERROR 2002: Can't connect to local MySQL server through socket"?
MySQL server is not running or failed to start Socket file does not exist at the expected location Client is looking for the socket in a different directory than where the server created it
Is the "ERROR 2002: Can't connect to local MySQL server through socket" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading