MySQL
Error 2002
Database Connection
Unix Socket
mysql_connect

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:

php
<?php
$link = mysql_connect("localhost", "user", "password");

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:

bash
mysqladmin ping
ps aux | grep mysqld

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:

bash
mysql -u root -p -e "SHOW VARIABLES LIKE 'socket';"

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:

php
1<?php
2$link = mysql_connect(
3    "localhost",
4    "user",
5    "password",
6    false,
7    "/var/run/mysqld/mysqld.sock"
8);

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:

php
<?php
$link = mysql_connect("127.0.0.1", "user", "password");

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:

php
1<?php
2$mysqli = mysqli_init();
3$mysqli->real_connect(
4    "localhost",
5    "user",
6    "password",
7    "database",
8    null,
9    "/var/run/mysqld/mysqld.sock"
10);

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 localhost and 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_connect instead of mysqli or PDO.

Summary

  • Error 2002 with trying to connect via unix:///tmp/mysql.sock usually 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.1 to force TCP if you want to bypass socket issues.
  • For modern PHP, switch from mysql_connect to mysqli or PDO.

Course illustration
Course illustration

All Rights Reserved.