Software⏱️ 2 min read📅 2026-05-31

How to Fix: MySQL connection not working: 2002 No such file or directory

MySQL connection issue on OS X Snow Leopard with built-in Apache and x8 MySQL installation.

Quick Answer: The error message indicates that the MySQL server is not running or not accessible. Check if MySQL is installed correctly, start the MySQL service, and ensure it's listening on a specific port (default is 3306).

To resolve the 'No such file or directory' error when establishing a MySQL connection, it's essential to understand that this error occurs due to the incorrect path to the MySQL socket file. On OS X Snow Leopard using built-in Apache, the default location of the MySQL socket file is not specified.

⚠️ Common Causes

  • The MySQL socket file path may be incorrect or not specified.

🛠️ Step-by-Step Verified Fixes

Method 1: Specifying the MySQL socket file path manually

  1. Step 1: In your PHP script, specify the MySQL socket file path using the `mysql_connect` function with the third parameter. For example: `$conn = mysql_connect('localhost', 'USER', '/var//mysql/mysql.sock');

Method 2: Using the MySQL socket file path environment variable

  1. Step 1: Set the `MYSQL_SOCKET` environment variable to point to the correct location of the MySQL socket file. For example: export MYSQL_SOCKET=/var/mysql/mysql.sock

🎯 Final Words

By following these steps, you should be able to resolve the 'No such file or directory' error and establish a successful MySQL connection.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions