Update: A second solution involves changing a hostname in your PHP file from
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
/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
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: