I found that after install MySQL and Apache/PHP – that the database connections didn’t work from PHP. They worked if I tried to connect using the MySQL client though. Must be a problem with the PHP side.

There is a sock file that PHP was trying to use, and it was in /var/mysql/mysql.sock complaining that the file was not there.

Warning Error: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in [/filename.php, line number]

Sure enough, the file wasn’t there, and infact the folder was not there either. So I made a folder and created a symlink to the one that was there, in /tmp/mysql.sock

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Restarted Apache and MySQL just to make sure. And hey presto its working.

sudo apachectl restart
sudo /usr/local/mysql/support-files/mysql.server restart

By admin

7 thought on “Mac OSX 10.8 PHP, mysql.sock not found”
  1. Thanks for posting this – it’s the clearest explanation of this that I could find. It did the trick.

    I also needed to edit the socket paths in php.ini with /tmp/mysql.sock – the default did not suffice.

  2. “sudo ln -d…” did not work for me. I had to use the -s option:

    sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

  3. I managed to fix this issue by updating the following php.ini setting:


    (it defaults to: /var/mysql/mysql.sock)

    This way you don’t need to make symlinks

  4. Odd. Editing my configuration file didn’t do anything (I made sure it was the correct file with `php -i | grep ‘Configuration File’`). However, adding the symlink did the trick. Thanks.

  5. I fixed my problem this way
    1. mkdir /var/mysql
    2. ln -s /tmp/mysql.lock /var/mysql/

    and it works.
    this way I don’t have to update php.ini file.

    looking at the post above they did help identify the problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.