error 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' 2' -- Missing /var/run/mysqld/mysqld.sock
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with MySQL, you might encounter an error message like:
This message typically indicates a problem with connecting to the MySQL server via the Unix socket. Let's delve into what causes this error, how it can be resolved, and what considerations should be made regarding this issue.
Understanding the Error
Unix Socket in MySQL:
A Unix socket is a communication endpoint used when a client and server are running on the same machine. The MySQL socket file, commonly located at /var/run/mysqld/mysqld.sock, is used by MySQL clients to connect to the MySQL server. When this file is missing, clients can’t communicate with the server, resulting in the error message above.
Common Causes
- MySQL Server Not Running:
- The socket file is deleted each time the MySQL server stops. If the MySQL server isn’t running, there’s no socket file for clients to connect to.
- Incorrect Socket Path:
- Misconfiguration in client applications or MySQL client configurations (
my.cnf) that point to the wrong socket path.
- Permission Issues:
- The MySQL server must have sufficient permissions to create the socket file upon startup.
- Accidental Deletion:
- Manual deletion or a cleanup script may have removed the socket file.
How to Resolve
Checking MySQL Server Status
Before anything else, ensure that your MySQL server is running:
If it's not running, start the MySQL service:
Verifying the Socket File Path
Check your MySQL configuration files (/etc/mysql/my.cnf or /etc/my.cnf) for the correct socket path and ensure consistency across client configurations:
Adjusting Permissions
Ensure that the directory /var/run/mysqld/ exists and has appropriate permissions for MySQL:
Alternative Connection Solutions
If using the socket connection repeatedly causes issues, consider connecting through TCP/IP by specifying 127.0.0.1 or localhost:
Preventing Future Issues
- Auto-start MySQL: Enable MySQL to start at boot time to ensure it’s always running when the system is up:
- Monitoring and Alerts: Set up monitoring for the MySQL process and alerts for any service interruptions using a monitoring tool like Nagios or Prometheus.
Summary Table
| Potential Cause | Description | Resolution |
| MySQL Server Not Running | Server stopped, causing deletion of socket file. | Start MySQL service. |
| Incorrect Socket Path | Misconfigured socket path in application or my.cnf. | Verify and correct the socket path in configurations. |
| Permission Issues | Insufficient permissions for creating socket file. | Set proper permissions on /var/run/mysqld/ directory. |
| Accidental Deletion | Socket file manually deleted or removed by scripts. | Restart MySQL to recreate the socket file. |
| Connection Through TCP/IP as Alternative | Avoid reliance on socket files for connectivity. | Use 127.0.0.1 or localhost for client connections. |
Additional Considerations
SELinux and Firewall:
- If your system uses SELinux, ensure appropriate contexts and policies are set for MySQL to create socket files.
- Ensure the firewall doesn’t block MySQL communication on TCP/IP.
Further Reading
To explore more about MySQL connections, socket usage, and performance optimizations, consider visiting MySQL's official documentation or exploring community resources and forums dedicated to MySQL configurations and troubleshooting.
By systematically addressing the potential causes, you can effectively diagnose and resolve this common MySQL connectivity issue.
Related reading
- Error Code 1292 - Truncated incorrect DOUBLE value - Mysql
- Error Code 2013. Lost connection to MySQL server during query
- Error creating bean with name 'entityManagerFactory' defined in class path resource Invocation of init method failed
- Error Dropping Database Can''t rmdir ''.test'', errno 17
- Error, Can''t set headers after they are sent to the client
- Error from server Timeout the server was unable to return a response in the time allotted, but may still be processing the request get nodes
- Error certificate signed by unknown authority after switching GCP project
- Error connecting to kafka server via IDE in WSL2

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.