HTML and CSS Reference
C.3.3. Getting MySQL and PHP to play nice together
There's one frustrating issue with getting PHP and MySQL to play nice together in Mac
OS X: the location of mysql.sock. mysql.sock is a Unix socket file that allows bidirectional
communication between MySQL and any other local application. In the case of our sample
application, we want PHP and MySQL to talk to each other, but if you look at the PHP info
(using your test.php web page from before), you'll see that PHP is looking for mysql.sock
in /var/mysql, whereas the actual mysql.sock file is by default in /tmp/. What to do?
There are four ways to fix this. Read through the options and decide which is the sanest
approach for you.
• Create the /var/mysql directory ( sudo mkdir /var/mysql ) and create a sym-
bolic link from /tmp/mysql.sock to /var/mysql/mysql.sock (with: sudo ln -
s /tmp/mysql.sock /var/mysql/mysql.sock ). This method is easiest
but a bit of a hack.
• Edit the /etc/php.ini file (it may not exist, in which case just sudo cp /etc/
php.ini.default /etc/php.ini ) so that
pdo_mysql.default_socket=/tmp/mysql.sock (by default line
1065), mysql.default_socket = /tmp/mysql.sock (by default line
1219), and mysqli.default_socket = /tmp/mysql.sock (by default
line 1278). This method is the easiest real way.
• Edit (or create) /etc/my.cnf, adding the following lines: [mysqld] socket=/
var/mysql/mysql.sock [client] socket=/var/mysql/
mysql.sock . This will tell MySQL to create its socket where Mac OS X's de-
fault PHP is looking for it. Next, you also need to create the /var/mysql directory
and sudo chown mysql /var/mysql it, or MySQL won't start because it
won't be able to create the socket. (Various sample my.cnf files can be found in
/usr/local/mysql/support-files.) This method isn't too bad, but it could cause issues
with other MySQL clients that look for the socket in /tmp/mysql.sock.
• Recompile php for your version of MySQL. This method, although a pain, isn't a
terrible idea; it's actually the best fix although clearly neither fast nor easy.
Pick the way that works for you and do it. When you've finished, your computer will
be ready to serve up MySQL-driven, PHP-based web apps—including the sample app in