Thursday 6 August 2009

Getting MySQL running with PHP on a Mac

I've just been trying to get PHP and MySQL running with Apache on a Mac (PHP 5.2.8 and MySQL 5.1.33 running OS X 10.5.7). Somehow I kept running into the following error when trying to get a PHP script to connect to MySQL:

mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'

Here's how I fixed it.

After a bit of research I tracked this down to a difference between PHP and MySQL on the Mac. PHP defaults to look at /var/tmp/mysql.sock for its connection, but MySQL on the Mac sets it up at /private/tmp/mysql.sock instead.

The solution is to edit your php.ini file (after making a backup copy first in case something goes wrong, and looking for the line for mysql.default_socket, which you should change so it looks like this:

; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /private/tmp/mysql.sock

See an article here for more details. There's also a handy test script here.

No comments:

Post a Comment