How to Fix \'Can\'t connect to local MySQL server through socket \'var/run/mysqld/mysqld.sock\'?

When you encounter the error message "Can't connect to local MySQL server through socket 'var/run/mysqld/mysqld.sock' (2)", it means there is a problem connecting to your MySQL database server. This error typically occurs when the MySQL client cannot establish communication through the Unix socket file, which is the default connection method for local MySQL connections.

This error can occur for various reasons, including the MySQL server not running, incorrect socket file paths, permission issues, or misconfigured MySQL settings. Understanding the root cause is essential for applying the correct fix and restoring database connectivity.

Understanding the Error Message

A socket is a communication endpoint that allows processes to communicate with each other. In MySQL's context, the mysqld.sock file serves as a Unix domain socket for local communication between the MySQL server and client applications running on the same machine.

The error message indicates that the client application was unable to establish a connection with the MySQL server via its Unix socket, typically located at /var/run/mysqld/mysqld.sock. The error code (2) specifically means "No such file or directory," suggesting the socket file is missing or inaccessible.

Common Causes

  • MySQL server not running ? The most frequent cause is that the MySQL service has stopped or failed to start

  • Missing socket file ? The mysqld.sock file doesn't exist in the expected location

  • Permission issues ? Incorrect file or directory permissions prevent access to the socket

  • Configuration mismatch ? Client and server configurations specify different socket paths

Troubleshooting Steps

Step 1: Check MySQL Service Status

First, verify if the MySQL server is running using the following command ?

sudo systemctl status mysql

If the output shows the service is inactive or stopped, start it with ?

sudo systemctl start mysql

Step 2: Verify Socket File Existence

Check if the mysqld.sock file exists in the expected directory ?

ls -la /var/run/mysqld/

If the socket file is missing, restart the MySQL service ?

sudo systemctl restart mysql

Step 3: Fix File Permissions

Ensure proper permissions for the socket directory and file ?

sudo chmod 755 /var/run/mysqld/
sudo chown mysql:mysql /var/run/mysqld/
sudo chmod 666 /var/run/mysqld/mysqld.sock
sudo chown mysql:mysql /var/run/mysqld/mysqld.sock

Step 4: Update MySQL Configuration

If MySQL is looking for the socket in a different location, edit the configuration file /etc/mysql/my.cnf or /etc/my.cnf ?

sudo nano /etc/mysql/my.cnf

Ensure the socket path is correctly specified in both [client] and [mysqld] sections ?

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

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

After making changes, restart MySQL ?

sudo systemctl restart mysql

Advanced Troubleshooting

Check Network Connections

Verify that MySQL is listening on the correct port ?

sudo netstat -tlnp | grep :3306

Examine System Logs

Check MySQL error logs for detailed information about startup issues ?

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

Also check system logs for related errors ?

sudo journalctl -u mysql.service -f

Alternative Solutions

Method Description Use Case
TCP Connection Connect via 127.0.0.1:3306 instead of socket When socket issues persist
Create Socket Directory Manually create /var/run/mysqld/ directory When directory is missing
Reinstall MySQL Complete MySQL reinstallation When configuration is severely corrupted

To force TCP connection instead of socket ?

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

Conclusion

The MySQL socket connection error is typically resolved by ensuring the MySQL service is running and the socket file exists with proper permissions. Most cases can be fixed by restarting the MySQL service and verifying configuration settings. For persistent issues, checking system logs and using TCP connections provide alternative solutions.

Updated on: 2026-03-17T09:01:38+05:30

40K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements