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