PHP and MySQL Socket Gotcha

Note: This has been fixed with a better solution than I came up with here.

Before I start on this one, let me make one thing clear :) See the title of this blog? MacNEWBIE… I have no idea if this is the best solution to this because it sure doesnt seem like it to me but it's working for me right now so I am sticking with it but please leave me a comment if you know a better or more satisfactory way to fix this issue. Ok, disclaimer out of the way!

So this nightmare started when I tried to get the pre-installed Apache and PHP working with MySQL (this article relates to both MySQL v4 and v5 as I have tried both in the process of trying to get this working.

First of all, the good!

  • Apache was really easy to switch on, open preferences and click one button. Done. Nice.
  • PHP was also really easy to turn on. Open httpd.conf, uncomment 2 lines. Done. Nice.
  • MySQL was really easy to install. Grab the Mac OS binaries from and install. Done. Nice.

Then the issues started, Apache, PHP and MySQL all worked independently but every time I tried to open a connection to the MySQL database through PHP, I got an error message:

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

The MySQL documentation says that MySQL runs on /tmp/mysql.sock as default and sure enough it does, it just seems that PHP is trying to look at it through /var/mysql/mysql.sock instead.

To cut a very very very long story short (that included me flattening my MacBook Pro and reinstalling Mac OS/X in case the problem was caused by something I did when I was compiling Ruby on Rails, which is another story) nothing I did could fix this issue, I installed 3rd party PHP utilities like phpMyAdmin, and that didnt work either. Anyway, we were trying to cut this short. I have found a workaround.

Instead of connecting like this:

$db = mysql_connect("localhost", "myuser", "secret");

Specify the full name of the socket on the server, like this:

$db = mysql_connect("localhost:/tmp/mysql.sock", "myuser", "secret");

There has to be a better way to get PHP to connect to MySQL on Mac OS/X, but I havent found it. At least this works for now, hope this helps some other poor newbie if/when they come across this because it does not seem to be mentioned anywhere on the interweb that I could see.

Technorati Tags:
, , , , , ,

20 responses to “PHP and MySQL Socket Gotcha

  1. How about trying mamp? Its a simple app that works like a web server, just without a lenghty config…

  2. Hi, nice to hear about all these things from a switcher’s point of view; I hope the experience will turn out to be a good one. I switched from Windows to Mac in 2001, and I haven’t looked back, I must say.
    About the PHP/MySQL-thing: I do not know where you downloaded the install files for both, but a good starting point for pre-compiled PHP and for any support concerning PHP and MySQL on OS X is ( Swiss-based site, but it is in English). Check out the ‘Software’ section! This is how I got both to work properly, and at the time I was a complete newbie, so for someone with a bit of background, it ‘ll be a piece of a cake, I’m sure.

  3. Your MySQL is not configured to listen to an “internet” socket. It is instead configured to listen on a UNIX file system socket. This might be a better way to do it if you are only going to allow connections from localhost. The UNIX file system socket method is a popular way to do these types of things. This isn’t something specific to a Mac.

  4. Just edit your php.ini. There is a line in there in the MySQL section for the default socket.

  5. hi, nice to see you switching to mac :)

    the easiest way is to install the Entropy release, like Bart said, but another sollution way be this:

    connect to your db, switch to users and select ‘root’ or whatever user you’re using. In the bottom left you’ll see 4 buttons, click the 3rd one to add localhost to the allowed hosts list, that *should* do the trick

  6. Create or edit /etc/my.cnf. Add the following:


    This will affect any applications using the MySQL client library on your Mac.

  7. I’ve done the Entropy method on three machines without a glitch.

  8. This post in MySQL forum will help you:,23138,74554

    During the 10.4.4 OS update, Apple have changed where PHP looks for the MySQL socket file.

    You can correct this my:
    $ sudo mkdir /var/mysql
    $ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

  9. Mathias beat me to it. Also remember that that you may have to use OLD_PASSWORD() to set passwords for users.

  10. Last time I set this up Mac OS X didn’t include the php.ini file. What you need to do is download the version of PHP that you have and grab the php.ini file to install in /private/etc/. You should then be able to set your default host and socket and port.

  11. OK newbie it time you give us all a little feedback… after all this is a two way street…

    How did the PHP & MySQL issue resolve itself and did any of the solutions offered up help? Which one?

    Have a good day…


  12. This bug has been in Tiger since 10.4.0!! Seems like somebody should have fixed it by now, and rolled out the update.

    You can either configure php.ini to look in /var/mysql/, or you can configure MySQL to put its socket file in /tmp. I’ve found that the former method works if you’re building a system from the ground up, but the latter works better if you want easy compatibility.

    And, I echo the sentiment – if you’re looking to install phpMySQL especially, make a note of the OLD_PASSWORDS post above.

  13. Creating the symlink worked like a charm, thanks everyone who suggested that.

    Nice elegant solution.

  14. Hi,

    I’ve had the same issues all over several Macs, running both Tiger and 10.4 Server. What worked best for me was XAMPP, an all-in-one Installation of Apache, PHP, MySQL and a whole load of other useful stuff if you are developing Internet Applications. Works like a breeze with a mac-like installer. Setup is easily done even by newbies in less than five minutes.
    What’s even better: you can switch the whole package on and off with one simple and well documented command in your Terminal.

    Download at

    and yes, it’s free.

  15. Pingback: My Journey to Macintosh » PHP and MySQL on Mac OS X Tiger - The Fix

  16. What was your Ruby experience? I’m going to install it, and if you have some experiences and solutions, I’d love to hear them

  17. Argh…none of this fixes my problem. Still comes up with the 2002 error