PHP and MySQL on Mac OS X Tiger – The Fix

I mentioned that I had a solution to the PHP and MySQL socket issue in my previous post but I figured it was worth a separate entry because it reminded me of a really nice feature of Unix that I had almost forgotten about – symbolic links.

The original problem occurred because PHP (through Apache) was trying to connect to a file in a certain location (/var/mysql/mysql.sock) and it didn’t exist there, it existed in another place (/tmp/mysql.sock). The solution? Create a virtual link between 2 locations on disk.

ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Now that these 2 ‘files’ are linked, any information that flows through /tmp/mysql.sock will automatically appear as if it is flowing through /var/mysql/mysql.sock as well.

The great thing about this solution is that it focused not around changing either application to look in an alternative place but instead to use the power of the operarting system to trick them both into being happy by creating a virtual link between the two locations. The biggest benefit of this is that any application that was already installed that was depending on MySQL existing at /tmp/mysql.sock will still work without any modification.

I really do love that Mac OS X is based on BSD, it was a great decision.

Thanks to Geoff Ellis who posted the original solution and also to hsm who posted it in my comments.

Technorati Tags: , , , , ,

10 responses to “PHP and MySQL on Mac OS X Tiger – The Fix

  1. Hi Dave, I found your blog from TUAW too. Hope you’re enjoying your Mac experience every bit as much as I did. Back in October I wrote up an article with tips on switching to Mac. Maybe you’d like to check it out

  2. I also made the switch with a mac mini and wrote a tutorial on how to get Apache2.2.x, Mysql4.1.x, and php4 up and running as a local dev machine… take a look if you’re interested.

  3. Mike Butler

    Great blog. Have you tried using MAMP? It may be too simplistic for what you are doing but rolls up Apache 2.0, PHP and MySQL in one transportable package.

  4. or you could just change the my.cnf to tell it where you want the socket to exist…

  5. you should also give camino a try, if you wouldn’t be liking Safari y you 3rd or 2nd try. “”

  6. The sym link is the correct answer to this problem. Interestingly, it’s not only the Mac OS X solution. I’ve had to do something like this for mySql on Linux as well…

    Mac OS X is just Unix under the hood and it helps a LOT if you’re used to the Unix way of doing things.

  7. If you start really getting deep is OS X you might find that your thought that it’s BSD based can get you into some trouble and sonfusion. In actuality, the very core of the XNU Kernel is the Mach Kernel (a microkernel) with bits of BSD plopped in to handle the POSIX api, security & permissions, and other things of the sort.

    Basically what they did is take the great parrt of the microkernel and use BSD for what a microkernel handles inefficiently.

  8. Michael Johnston

    What are you using for an editor? If nobody has recommended textmate to you, I will…it has a really really powerful contextual syntax coloring / completion / macro functionality. The best I’ve ever seen. I’ll also second that iTerm is much better than Terminal

    I’m currently going the other way. Well I’m (definitely) not switching to windows, but I built a Windows box for some things I needed to do. I’m a complete newbie with Windows. Can you recommend a good terminal emulator? should I check out the beta of the new command shell that is coming in vista?

  9. You’re welcome! I had the same problems that day, that’s why the answer was so close to me :)

  10. In Ubuntu (Linux), I had this problem as well, except it was sort of the other way around. It expected it to be in /tmp/mysql.sock, yet it was in /var/run/mysqld/mysqld.sock. Perfect solution, thanks.