PHP & MySQL on Leopard: mysql.sock Location
Posted on October 30, 2007 at 6:06 pm
One thing I noticed setting up PHP and MySQL on Leopard, is that the default PHP build looks for the MySQL socket in /var/mysql/mysql.sock which doesn't exist and isn't the location that MySQL would use if it did (by default anyway). By default the MySQL socket is: /tmp/mysql.sock.
There are four ways to fix this:
- Create the /var/mysql directory (
sudo mkdir /var/mysql) and then create a symbolic link from/tmp/mysql.sockto/var/mysql/mysql.sock(with:sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock). - Edit the
/etc/php.inifile (it may not exist in which case justsudo cp /etc/php.ini.default /etc/php.ini) so that themysql.default_socket = /tmp/mysql.sock(by default line 760) andmysqli.default_socket = /tmp/mysql.sock(by default line 795). - Edit
/etc/my.cnfadding the following lines:
[mysqld]
socket=/var/mysql/mysql.sock
[client]
socket=/var/mysql/mysql.sock
This will tell MySQL to create its socket where Leopard's default PHP is looking for it. Next, you also need to create the /var/mysql directory andsudo chown mysql /var/mysqlit or MySQL won't start since it wont be able to create the socket. - Recompile php for your version of MySQL (a PIA, but not a terrible idea at all).
Of these #1 is easiest, but a bit of a hack. #2 is the easiest real way. #3 isn't to bad, but could cause issues with other MySQL clients that look for the socket in /tmp/mysql.sock. #4 is ultimately the best fix, but is clearly neither very fast nor particularly easy
12 Comments »
RSS feed for comments on this post. TrackBack URL
Leave a comment

Migrating MySQL 5.0.45 to Mac OS X 10.5 Leopard
http://blog.tomatocheese.com/archives/2007/11/1/migrating_mysql_to_mac_os_x_leopard/
Comment by Joannou Ng — November 1, 2007 @ 11:42 pm
[...] beyondmac(dot)com [...]
Pingback by insites » Blog Archive » MySQL in Leopard — November 8, 2007 @ 10:29 am
Thank you for this, it was very easy to find on google and helped out quickly.
Comment by Nate — November 29, 2007 @ 3:38 pm
[...] a good explanation of your choices in how to fix it. I like solution #1 unless you have the time and energy to tackle [...]
Pingback by Null is Love » Blog Archive » Taming Leopard for PHP — December 19, 2007 @ 2:10 pm
This solved a headache I had for 2 days. You give not one but four solutions. Thanks. Leopard should have Mysql pre-installed just like PHP.
Comment by hanafi — June 26, 2008 @ 9:42 pm
Brilliant ! After hours of configuring mysql under Leopard with no success, your solution took me 5 minutes and it works with phpmyadmin !
Thank you very much ! I accomplished all the steps explained from #1 to #3.
I would like to add, that to make it easier to find the hidden files "php.ini" and hidden directories, i used the software ONYX and i asked it to make hidden files and directories visible.
Regards.
Comment by DAVID — September 12, 2008 @ 7:56 am
On Leopard (OS X 10.5), PHP defaults to look for the socket at /var/mysql/mysql.sock. While the OS X MySQL installation package (mysql.com) defaults to placing it at /tmp/mysql.sock. A solution that corrects the problem is to edit the /etc/php.ini file and change
mysql.default_socket =
to
mysql.default_socket = /tmp/mysql.sock
and for completeness also change
mysqli.default_socket =
to
mysqli.default_socket = /tmp/mysql.sock
Comment by Dalmazio — September 15, 2008 @ 3:03 am
I think the following is an even easier REAL way than #2:
Add the following line
php_value mysql.default_socket /tmp/mysql.sock
to the
/private/etc/apache2/other/php5.conf
file (in between the tags of course), then restart web sharing. In most instances php.ini does not exist, and #2 makes you create that file, which is a security risk in some installations. This does not add that risk.
Comment by jaffle — October 24, 2008 @ 4:50 am
I can't find any my.cnf file. I've tried to locate it with text wrangler, but no results. How can I edit it? Thank you.
Comment by Juan — February 17, 2009 @ 6:35 pm
[...] PHP & MySQL on Leopard: mysql.sock Location « beyondmac.com [...]
Pingback by How to Install WordPress on Mac OS X (Leopard) :: Software — July 8, 2009 @ 4:01 am
This solved in a couple of minutes what I spent ALL day trying to get resolved yesterday. Thanks!!!!
Comment by Chad — October 15, 2009 @ 9:57 am
[...] PHP & MySQL on Leopard: mysql.sock Location « beyondmac.com [...]
Pingback by How to Install WordPress on Mac OS X (Leopard) — March 5, 2010 @ 6:47 am