MySQL ERROR 2002 (HY000): Can't connect to local MySQL server through socket
MySQL ERROR 2002 (HY000) indicates the MySQL client cannot connect to the local server via the Unix socket file. This typically occurs when the MySQL service is not running, the socket file is missing or misconfigured, or file permissions prevent access.
Symptoms
When trying to connect to MySQL locally, you receive the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)This error prevents any MySQL client (mysql, phpMyAdmin, etc.) from connecting to the database server. The error may also appear in application logs, causing website or service downtime.
Root Causes
- MySQL service not running: The MySQL daemon (mysqld) has stopped or failed to start.
- Socket file missing or deleted: The Unix socket file (usually
/var/run/mysqld/mysqld.sockor/tmp/mysql.sock) does not exist. - Incorrect socket path in client configuration: The MySQL client is looking for the socket in the wrong location.
- File permissions: The MySQL user does not have permission to access the socket directory.
- Disk space full: The partition containing the socket directory is full.
- Corrupted MySQL installation or configuration: Damaged configuration files or missing dependencies.
Step-by-Step Fix
1. Check if MySQL Service is Running
Run the following command to check the MySQL service status:
systemctl status mysqlOr for older systems:
service mysql statusIf the service is not active, start it:
systemctl start mysql2. Verify the Socket File Exists
Check the default socket location:
ls -la /var/run/mysqld/mysqld.sockIf the file is missing, the MySQL service may not have created it. Restart the service:
systemctl restart mysql3. Check MySQL Error Log
View the MySQL error log to identify why the service failed to start:
tail -50 /var/log/mysql/error.logLook for errors like Can't start server: Bind on Unix socket or permission issues.
4. Verify MySQL Configuration
Open the MySQL configuration file:
nano /etc/mysql/my.cnfEnsure the socket directive is set correctly under the [mysqld] section:
[mysqld]
socket = /var/run/mysqld/mysqld.sockAlso check the [client] section for the same socket path:
[client]
socket = /var/run/mysqld/mysqld.sock5. Check Socket Directory Permissions
Ensure the MySQL user owns the socket directory:
chown mysql:mysql /var/run/mysqld
chmod 755 /var/run/mysqld6. Test Connection with Explicit Socket
Try connecting directly specifying the socket:
mysql -u root -p -S /var/run/mysqld/mysqld.sockIf this works, the socket path in your client configuration is incorrect.
7. Restart MySQL Service
After making changes, restart MySQL:
systemctl restart mysqlAlternative Fixes
- Use TCP/IP connection: Connect via localhost using TCP instead of socket:
mysql -u root -p -h 127.0.0.1 - Reinstall MySQL: If the socket file is persistently missing, reinstall MySQL:
apt-get remove --purge mysql-server mysql-clientthenapt-get install mysql-server mysql-client - Check disk space: Run
df -hto ensure the partition is not full. - Check AppArmor/SELinux: Disable or adjust security modules that may block socket creation.
Prevention
- Monitor MySQL service: Use monitoring tools (Nagios, Zabbix) to alert when MySQL is down.
- Regular backups: Keep configuration file backups.
- Update regularly: Keep MySQL updated to avoid bugs.
- Document socket path: Ensure all applications use the correct socket path.
- Automated health checks: Schedule cron jobs to verify MySQL connectivity.
Conclusion
MySQL ERROR 2002 (HY000) is usually resolved by restarting the MySQL service or fixing the socket path configuration. By following the steps above, you can quickly restore database connectivity.
Was this solution helpful?