MySQL connection not working: 2002 No such file or directory

MySQL connection not working: 2002 No such file or directory

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

MySQL connection not working: 2002 No such file or directory

You are staring at an error message that claims there is "No such file or directory," yet you are certain your database server is running and your credentials are correct. The culprit here isn't a missing configuration file in the traditional sense, but rather a mismatch between where the PHP process expects to find the MySQL socket and where the MySQL server has actually placed it.

On OS X Snow Leopard, particularly when using the built-in Apache installation alongside a manually installed MySQL client via the x86_64 dmg installer, this discrepancy is surprisingly common. The error 2002 specifically indicates that the PHP script cannot locate the Unix socket file required for local communication between the two services. While TCP/IP connections often work if you specify an IP address or hostname, most modern applications default to using the Unix domain socket for performance and security when connecting to localhost.

The standard path for this socket is typically /var/mysql.sock or /tmp/mysql.sock, but the actual location depends entirely on your MySQL installation configuration. The error occurs because the PHP runtime defaults to a hardcoded path (often /var/mysql/mysql.sock) that does not exist on your specific system setup. When you run the connection script and receive Error: 2002 - No such file or directory, PHP is looking for a socket in a location where it has never been created.

To resolve this, you must identify the exact path to the socket file and instruct PHP to use it. The most direct way to verify this is by checking the MySQL server's own configuration or inspecting the filesystem directly. If you have root access, you can often find the socket listed in the output of mysqladmin variables | grep socket. Alternatively, a simple shell command like ls -l /tmp/mysql.sock or checking /var/mysql/mysql.sock will reveal if the file exists and where it resides.

Once you identify the correct path—let's say it is indeed /tmp/mysql.sock as noted in your troubleshooting process—you need to update your PHP configuration. In your php.ini file, you must locate the directives governing socket connections. Look for the mysql.default_socket directive (if using legacy extensions) or, more commonly in modern setups, the pdo_mysql.default_socket directive if you are connecting via PDO.

You should replace any hardcoded paths with the absolute path to your active socket:

# In php.ini
pdo_mysql.default_socket = /tmp/mysql.sock

However, simply editing php.ini is not enough; Apache must be aware of this change. If you are running PHP as a module within Apache (mod_php), ensure that the configuration file you edited is loaded by your specific Apache virtual host or the global config. Sometimes, PHP runs from a different directory than where your main php.ini resides, leading to settings being ignored. You can verify which configuration file is actually active by running php -i | grep socket in your terminal.

There is a critical trade-off to consider regarding version compatibility. The code snippet in your source material uses the deprecated mysql_* functions. These were removed in PHP 5.5. If you are on a newer version of PHP, you cannot simply patch the socket path; you must migrate to PDO or the MySQLi extension. With PDO, the configuration approach described above is standard practice. With MySQLi, you can either set the socket in php.ini using mysqli.default_socket or pass it dynamically during instantiation:

$conn = new mysqli('localhost', 'USER', 'PASSWORD', 'database');
// Or explicitly use the socket
$conn = new mysqli('unix:/tmp/mysql.sock', 'USER', 'PASSWORD', 'database');

If you are building a modern application stack, consistency in your connection logic is key. Whether you rely on environment variables, configuration files like .env, or direct code injection, ensure that the path resolution happens before the connection attempt. If you find yourself struggling with these low-level socket configurations repeatedly, consider leveraging a managed service or consulting resources from experts like those at https://laravelcompany.com for streamlined architecture patterns that abstract away these specific server configuration quirks.

Ultimately, the error message is misleading; it's not about permissions or missing files in the general sense, but a precise mismatch in file paths. By aligning your PHP expectations with the reality of your MySQL installation, you restore the local communication channel and eliminate the 2002 error.

Our network