Error
Error Code: 2002

MySQL Error 2002: Local Socket Connection Failure

📦 MySQL
📋

Description

This error indicates that the MySQL client cannot establish a connection to the local MySQL server process using a Unix domain socket file. It commonly occurs when the server is not running, the socket file is misconfigured or missing, or due to permission issues with the socket.
💬

Error Message

Can't connect to local MySQL server through socket '%s' (%d)
🔍

Known Causes

4 known causes
⚠️
MySQL Server Not Running
The MySQL server process is not active on the local machine, preventing any client connections via socket.
⚠️
Incorrect Socket File Path
The MySQL client is configured to use a socket file path that does not match the path configured for the server.
⚠️
Missing or Corrupted Socket File
The Unix domain socket file, crucial for local connections, is either absent from its expected location or has become corrupted.
⚠️
Socket File Permissions
The operating system user attempting to connect lacks the necessary read/write permissions for the socket file or its containing directory.
🛠️

Solutions

5 solutions available

1. Check MySQL is Running easy

Verify MySQL server is started

1
Check MySQL status
sudo systemctl status mysql
# or
sudo service mysql status
2
Start MySQL if stopped
sudo systemctl start mysql
# or
sudo service mysql start
3
On Windows
net start MySQL80
# or check Services panel

2. Fix Socket File Location medium

Correct socket path in configuration

1
Find actual socket location
sudo find / -name "mysql.sock" 2>/dev/null
# or
mysqladmin variables | grep socket
2
Specify socket in connection
mysql -u root -p --socket=/var/run/mysqld/mysqld.sock
3
Create symlink if needed
sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
4
Or configure in my.cnf
[client]
socket=/var/run/mysqld/mysqld.sock

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

3. Fix Socket Permissions medium

Ensure socket file is accessible

1
Check socket permissions
ls -la /var/run/mysqld/mysqld.sock
2
Fix directory permissions
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo chmod 755 /var/run/mysqld
3
Restart MySQL
sudo systemctl restart mysql

4. Use TCP Instead of Socket easy

Connect via TCP/IP instead of socket

1
Use 127.0.0.1 instead of localhost
# Socket connection (default for localhost):
mysql -u root -p -h localhost

# TCP connection:
mysql -u root -p -h 127.0.0.1
2
In connection string
// Using 127.0.0.1 forces TCP:
mysql://user:pass@127.0.0.1:3306/database

5. Fix After Crash or Improper Shutdown medium

Clean up stale socket file

1
Remove stale socket
sudo rm /var/run/mysqld/mysqld.sock
sudo rm /var/run/mysqld/mysqld.sock.lock
2
Check for stuck processes
ps aux | grep mysql
# Kill if necessary
sudo kill -9 <pid>
3
Restart MySQL
sudo systemctl restart mysql
🔗

Related Errors

5 related errors