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 mysql.com 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.