I was working with a friend to get him started with PHP development on OSX, and we were stumped by a ‘No such file or directory’ error message for a long time. It seemed to be coming from the following line:
$this->linkid = @mysql_pconnect($host, $user, $password) or die(mysql_error());
At first, we didn’t understand it, because it doesn’t look like PHP is trying to connect to a file there. However, we eventually discovered that the file that wasn’t found was the MySQL socket. Because he had used Ports to install MySQL instead of downloading the .dmg from mysql.com, he didn’t have the same default socket as most other people on the internet.
If you are also getting this error, follow the steps below to diagnose and fix it.
- Narrow down error to mysql_connect() or mysql_pconnect().
- Make a phpinfo() page. Look for ‘mysql.default_socket‘, ‘mysqli.default_socket‘, and ‘pdo_mysql.default_socket‘. Remember their value; this is where PHP is trying to connect to MySQL.
- Start mysql. Execute ‘
STATUS;‘ and look for the ‘UNIX socket‘ value. If doesn’t match the value from phpinfo(), that’s the culprit. - Open your php.ini file (which is also found on your phpinfo() page as ‘Loaded Configuration File‘) and change all the occurrences of the incorrect socket location to the correct socket location from MySQL.
- Restart Apache (on OSX:
sudo apachectl restart) and try reloading your page.


June 30, 2010 at 4:47 am
Nice one. I had this exact problem and you had the exact fix!
June 30, 2010 at 8:00 am
Awesome, I’m happy I could save someone the time it took us to figure it out!
August 31, 2010 at 11:08 am
Wow, thanks! This worked just like you said. There’s something weird though, I have phpmyadmin at localhost, and even before, and after I modified the php.ini file, it worked flawlessly. All other local sites wouldn’t. The offending site began working as I changed the file to the correct settings. So I just cannot pinpoint exactly what happened!! Any clue?? Could it be that phpmyadmin has its own mysql socket configuration? Thanks again!
August 31, 2010 at 1:14 pm
That is really strange. Unfortunately, I’ve never used phpmyadmin, so I really can’t help there. Having its own config file definitely sounds plausible, though!
December 8, 2010 at 12:05 pm
I came across this solution, but it took a while for me to navigate through all sorts of stuff just to implement it. Suffice it to say, that you are great at presenting this. I used to develop in Windows but am no longer in that world, thank god. I remember learning what autoexec.bat was(25 years ago), and I feel like I am in that world again with Unix(Mac). It’s fun to learn all over again. You solved a minor problem that would have taken me far longer. Now onto more learning.
September 18, 2011 at 4:38 pm
Hi, changing ‘localhost’ with 127.0.0.1 also does the trick
November 14, 2011 at 2:25 am
Worked for me! Thanks!
December 24, 2011 at 3:27 am
Great solution for many problems, but not all. Here’s a page that shows how to solve the problem if it’s due to the location of the socket file.
http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi
–
K
December 30, 2011 at 2:06 pm
http://www.yourethemannowdog.com/
Seriously, thanks for posting this!