Warning mysql_connect 2002 No such file or directory trying to connect via unix///tmp/mysql.sock in
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This MySQL 2002 error usually means the client tried to connect through a Unix socket path that does not exist. In practice, that points to one of three problems: MySQL is not running, the socket file lives somewhere else, or the client is configured to use a socket path that does not match the server.
Why the Socket Path Matters
On Unix-like systems, connecting to MySQL with localhost often means "use a Unix socket", not "use TCP".
If your PHP code does this:
the client may try a socket such as /tmp/mysql.sock. If that file is missing, you get the error.
That is why the message is so specific: it is not saying "bad password". It is saying "I tried a socket file and could not find it".
First Check Whether MySQL Is Running
If the server is down, there is no socket file to connect to.
Typical checks include:
If MySQL is not running, fix that first. A missing socket is often just a symptom of a stopped server.
Then Find the Real Socket Location
The server may be using a different socket path than the client expects.
Common locations include:
- '
/tmp/mysql.sock' - '
/var/run/mysqld/mysqld.sock' - '
/var/lib/mysql/mysql.sock'
Check the server variables:
Or inspect the MySQL config files to see which socket path is configured.
Make the Client Match the Server
If the server uses a different path, configure PHP or the connection code to use that exact socket.
With old mysql_connect, you can pass the socket as the fifth argument:
You may also need to update the PHP MySQL socket setting in php.ini so the extension uses the correct path consistently.
Force TCP to Bypass the Socket
If you want to avoid Unix sockets entirely, connect to 127.0.0.1 instead of localhost:
That usually forces a TCP connection and avoids the socket-path issue altogether.
This is a practical diagnostic step too. If 127.0.0.1 works and localhost fails, the problem is almost certainly the Unix socket configuration rather than credentials.
Important Note About mysql_connect
The old mysql_connect API is long deprecated and removed from modern PHP. In current code, use mysqli or PDO instead.
For example with mysqli:
The troubleshooting logic is the same, even though the API is better.
Common Pitfalls
- Assuming the error is about authentication when it is really about the socket path.
- Using
localhostand forgetting that it often means socket connection on Unix systems. - Fixing the code path while the MySQL server is still not running.
- Updating MySQL config but not the PHP client socket setting, or the reverse.
- Continuing to build new code with
mysql_connectinstead ofmysqlior PDO.
Summary
- Error
2002withtrying to connect via unix:///tmp/mysql.sockusually means the expected socket file is missing. - Check whether MySQL is running before changing code.
- Verify the server's real socket path and make the client use the same one.
- Use
127.0.0.1to force TCP if you want to bypass socket issues. - For modern PHP, switch from
mysql_connecttomysqlior PDO.

