Update: If you're interested in using PHP and MySQL on Mac OS X, these days I recommend skipping this article and using MAMP, which I describe in my Mac OS X MAMP tutorial.
Yesterday I was looking at a PHP application named Gallery that I wanted to use on one of my other blogs, and in the process of testing it out on my Mac I ran into some problems related to PHP, MySQL, and Mac OS X. The problems I had were that PHP -- and one of the PHP modules -- were configured to look for the
mysql.sock socket/file on my Mac in different places.
This post tells the story of how I got PHP, MySQL, and Mac OS X to work with each other, and also chronicles the changes needed to make the PHP framework Kohana also work with MySQL on Mac OS X.
The first problem is that vanilla PHP is looking for the MySQL communication socket as
/tmp/mysql.sock, but on my Mac (Mac OS X 10.5) the socket is really located at
/private/tmp/mysql.sock. The way the Mac works, the
/tmp directory is actually a symbolic link to
/private/tmp, so I thought this might work, but it doesn't.
The solution here is to tell PHP where the socket file is located, and you do this by editing the
/etc/php.ini file. If you've already created this file for some other reason, you can simple edit it, but in my case I first had to create it. Fortunately there's already a file on Mac OS X you can copy from, so the solution is pretty easy. Here are the steps I followed to fix this problem:
Step 1: open up a Terminal window.
Step 2: copy the
sudo cp /etc/php.ini.default /etc/php.ini
Note that you have to use the
sudo command to perform this copy action, as the
/etc directory is owned by the superuser. (If you haven't used the
sudo command before, this command means "superuser do", or more accurately, "Issue the following command as if you were the superuser".)
Also note that when you issue this command you'll be prompted for the superuser password. Hopefully you already know that -- it's the same password you're prompted for when you install some applications on your system, like iWork or iLife.
Step 3: edit the
/etc/php.ini file. You need to find the variable named
mysql.default_socket, and set it to point to the location of the MySQL socket on Mac OS X (in my case, Mac OS X 10.5). Your edited line should look like this:
mysql.default_socket = /private/tmp/mysql.sock
After making these changes I restarted Apache, and hit my initial web page, and saw that my initial PHP problem was fixed, and I thought I was done.
(Note that on a Mac you can restart Apache by clicking the Apple menu item, then System Preferences..., then the Sharing folder, then turn off Web Sharing, and then turn it back on again. There are other ways to do this, but this is an easy one to remember.)
Well, I thought I was finished, but then I learned that the Kohana framework does something else to connect to MySQL, and specifically looks for the MySQL socket as
I couldn't figure out where Kohana gets this location from, so to solve this problem, I created a hard link from this location (where Kohana expected to find the socket) to the real socket, following these steps from within the Terminal:
cd /var sudo mkdir mysql cd mysql sudo ln /private/tmp/mysql.sock .
sudo may again prompt you for the superuser password.)
After I made these changes, I just clicked refresh on my browser, and Kohana also began working with MySQL.