Missing mysql.sock on Mac OS X

Update: A second solution involves changing a hostname in your PHP file from localhost to 127.0.0.1. More info.

Recently I was doing some local development on my Mac OS X Leopard machine. When running apache2 with php I was able to connect to the database just fine, and from the command line I was able to run mysql commands.

However, when running a php script from the command line I was getting an error:

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

After spending some time thinking it was the code, I discovered that the mysql.sock file was missing.

Others on the net found the file elsewhere, like /private/tmp/mysql.sock or /tmp/mysql.sock but I had absolutely no .sock file anywhere.

Turns out, it did exist, but was named differently. According to the phpinfo output, the file was located at

/usr/local/mysql/run/mysql_socket

So, the solution was to create a symlink in /var/mysql to the proper file.

sudo ln -s /usr/local/mysql/run/mysql_socket /var/mysql/mysql.sock

Possibly related posts:

  1. Install Apache, MySQL, and PHP on OS X (10.5)
  2. MySQL Backup Shell Script
  3. Tech-Tidbit: MySQL Weighting

9 Comments »

  1. In my case, the missing socket was supposed to be under /tmp/mysql.sock and it was there and at some point disappeared and mysql was not functioning anymore. I had to re-install the mysql server several times on MAC OS X and in case you need to know how to remove previous MySQL, here is the link: http://steveno.wordpress.com/2009/03/26/uninstall-mysql-on-mac-os-x/

    What I discovered was that I tried to configure my.conf to make mysql by default use utf8. I copied my-medium.cnf from support-files under /usr/local/mysql and placed it under /etc as /etc/my.cnf. Somehow on computer restart this configuration file was kicking in and /tmp/mysql.sock went missing!!!

    As soon as I removed /etc/my.cnf I was able to launch newly installed mysql:

    > mysql -u mysql

    And the mysql.sock file appeared in the right place: /tmp/mysql.sock

    I spent HOURS to just figure out this thing!!! Only my MAC is not a server edition, that’s why this is not working. In work we have a MAC server where we installed MySQL from binaries and I created /etc/my.cnf and that had no collision whatsoever. I believe that possibly one should install MySQL from binaries (not from .dmg image) and that would guarantee a socket and a configuration file co-existing together!

    Comment by Ann — July 31, 2010 @ 12:18 am

  2. In my case, the socket file should have been at /var/mysql/mysql.sock, as in the main post above, but the entire /var/mysql directory was missing. I had been using local PHP-based web forms to access mysql as recently as August 3, 2010, but probably hadn’t used them since then (so about 2 months). At some point the directory and socket got deleted and I have absolutely no idea how.

    I noticed that mysql ran fine from a command line and I could start and stop the server from the System Preferences panel so I knew the socket must be somewhere. After reading some random posts I noticed that some people’s sockets were supposed to be /tmp and sure enough that’s where mine was. I did a soft link as above (after creating the /var/mysql directory) and poof! everything works again.

    Now I wonder what magic pixie caused my original link to disappear…

    Comment by Trevor Smith — October 3, 2010 @ 1:14 pm

  3. I have the same problem. The socket file is just gone! I cannot imagine what causes this to happen. Any ides?

    Comment by Helena Åsgård — October 15, 2010 @ 4:43 am

  4. ensure it is running mysqld first, otherwise you will get that error.

    Comment by Rick — October 15, 2010 @ 8:10 am

  5. Same problem here. I had a custom my.cnf file in /etc, and apparently MySQL didn’t like it (I’m not sure why). Removing the my.cnf file resolved the issue. This is on a Mac Pro workstation I’m using as a development machine — it’s not a server.

    Comment by Peter Weil — October 26, 2010 @ 5:33 am

  6. After following the clues here, I found mine in tmp so made symlink and away it went

    Comment by Adam Fakes — November 16, 2010 @ 4:21 am

  7. Ann from July 31, 2010 totally solved my problem and I just wanted to publicly say thanks Ann! I removed the my.cnf file, launched mysql, et voila! success!

    Comment by vynsynt — August 1, 2011 @ 12:03 pm

  8. Guys – I think the missing mysql.sock file is a security measure.

    Try to shut down your server and see if it says something along the lines of:
    2011-10-21 14:37:10 – Stopping server…
    2011-10-21 14:37:14 – Stop server: Warning: World-writable config file ‘/usr/local/zend/mysql/data/my.cnf’ is ignored
    Warning: World-writable config file ‘/usr/local/zend/mysql/data/my.cnf’ is ignored

    This means that the configuration file where you store your settings is being ignored -> hence you cant expect to be making changes in that file and see those changes kick in once you start the server. So try this:
    1. make changes to that file.
    2. find it through finder and change back the permissions so that everyone can read but not write to it.

    … then some magic takes place and the mysql server says to itself -> hmmm… that file is restricted – I can read it but I also know that only the administrator can write to it… which means its a good file for me to use when I set my settings using what’s written in there. :)

    Comment by Grigor Grigorov — October 21, 2011 @ 4:53 am

  9. I’d appreciate if you can provide links to official documentation and solutions to the core issue then, and to support this.

    Comment by Michael Botsko — October 21, 2011 @ 8:20 am

RSS feed for comments on this post.

Leave a comment