January 7, 2009

PHP on OSX: Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'

When installing PHP and MySQL on OSX  you may get the error Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’.  Or you may also get “No such file or directory” when calling mysql_connect from a PHP page.  This occurs because PHP is looking for the file mysql.sock in it’s typical installation location of /var/mysql/mysql.sock. However the MySQL OSX installer actually puts the file in /tmp/mysql.sock. There are two easy ways to solve the problem.

Solution 1: Create a symbolic link

Open terminal and do the following:

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

You just created a symbolic link in the place where PHP expects the socket file to be located so it should be happy.

Solution 2: Edit php.ini

If you don’t like the idea of creating a symbolic link, you can also simply alter your php.ini file to point PHP to the real location of mysql.sock.

Locate /etc/php.ini. (If php.ini doesn’t exist on your system, copy /etc/php.ini.default to /etc/php.ini).  You will likely have to do this from the terminal unless you have Finder configured to show hidden files.  Open the file and update the setting mysql.default_socket so it looks like this:

mysql.default_socket = /tmp/mysql.sock

To commit the change you need to restart Apache.  You can do that in System Settings -> Sharing, then  uncheck, then recheck Web Sharing.

11 Comments on “PHP on OSX: Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'

Ben Lieb
August 9, 2009 at 5:20 pm

Thaaaaaaaaaaaaaaaaaaank You

November 2, 2009 at 8:19 pm

Thanks. You saved me a lot of time. I still find it odd that copying /etc/php.ini.default to /etc/php.ini and restarting Apache causes the connection error. I wonder if PHP is actually reading some file besides /etc/php.ini.default if it doesn’t find /etc/php.ini.

November 6, 2009 at 2:42 pm

hey Paul, if php doesn’t locate a php.ini file on the system then it will just use the defaults that were set at compile time. you can tell by creating a phpinfo page – it will not show any loaded php.ini file.

Benjamin Good
January 12, 2010 at 5:58 pm

Thank you so much! I had no idea where to start on this one, you + google = relief.

Joe Carey
January 27, 2010 at 10:09 am

Thank you, thank you, thank you … !

May 19, 2010 at 11:35 am

Thank you, I almost went to MAMP before this, but I like doing installs manually, you saved me, thanks.

isaiah Mobit
August 12, 2010 at 2:30 am

Thanks – Saved Me a lot of head ache !

September 13, 2010 at 12:02 pm

THANK YOU!!!! I spent too many days on trying to figure this out and searching endless blogs.

September 23, 2010 at 12:14 am

what Ben said …
Nothing but nothing else worked, drupal REFUSED, until I relented and did this.

May 4, 2011 at 8:03 am


If youre using Zend. You may also need:

ln -s /tmp/mysql.sock /usr/local/zend/mysql/tmp/mysql.sock

May 17, 2011 at 1:24 pm

This worked GREAT!! Thank you!


Leave a Reply

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

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>