Missing mysql.sock on Mac OS X
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 php.ini, 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
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!