Mac OSX 10.8 PHP, mysql.sock not found

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

7 comments on “Mac OSX 10.8 PHP, mysql.sock not found

  1. Vaskerville September 11, 2012 3:28 am

    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. Lardlad November 11, 2012 2:39 pm

    “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. Hidde November 28, 2012 11:49 am

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

    pdo_mysql.default_socket=/tmp/mysql.sock

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

    This way you don’t need to make symlinks

  4. Mike February 26, 2013 3:20 pm

    Agreed, the -d is a typo. Only -s worked for me.

    Also I restarted MySQL from System Preferences.

  5. Jeffrey Horn February 26, 2013 8:10 pm

    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.

  6. BigL March 5, 2014 6:44 am

    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.

  7. admin March 5, 2014 7:25 am

    Updated the typo. Well spotted.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>