MySQL PDO for the default installation of PHP on Mac OSX

Quite a few times I have come across the problem of enabling the PDO drivers for MySQL on the Mac. I keep resolving this but every other time don’t remember how I did it. So I thought I’d write a about this so I can refer my own post when I need it 🙂

Initially the understanding that I had based on most of the posts and discussions around this topic is that the OSX does not contain the PDO drivers in it’s default PHP installation. Most solutions were talking about recompiling PHP with the PDO driver specifically for this purpose. But after much search I stumbled on this post which clearly mentions that the OSX Lion (10.7) ships with the PDO drivers by default. After playing around on Mountain Lion (10.8) I realised that this is the same case.

Initially when PHP is setup and working I was constantly getting the following PDO error

PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

This lead me believe that the problem was actually with the PDO. However later I realised that the problem was lot simpler than it seemed. The actual sock file was missing as the error says and this can be found in your /tmp folder in the Mac (at least for the 10.7 and 10.8 versions of OSX). To resolve the issue all you have to do is

  1. Locate the php.ini file on your Mac (/etc/php.ini)
  2. Replace all instances of /var/mysql/mysql.sock to /tmp/mysql.sock
  3. Save the file and restart Apache.

4 thoughts on “MySQL PDO for the default installation of PHP on Mac OSX

Comments are closed.