How to work-around connection problems with PHP and MYSQL on OSX.6 (Snow Leopard)

I don’t know why I’m having this problem, but I noticed that a few months ago rebooting my computer meant that for some reason I couldn’t get my php sites to run on my local server. I’d poke around and try a few guesses but nothing made sense. Eventually I learned that this is probably due to a security update that requires PHP look in a new place for your connection socket for MYSQL.

Everyone says you simply need to open the correct php.ini file and make a simple change. So first off you will want to find your php.ini file: I used phpInfo() to find the location, which is the common one most OSX servers will see ( /etc/php.ini )

Open this and change this line:

mysql.default_socket = /var/mysql/mysql.sock

To this:

mysql.default_socket = /tmp/mysql.sock

And then save the file and restart apache:

$ sudo apachectl restart

This supposedly fixes the problem for most people. I was not so fortunate. Everything was still broken. What gives?

A quick look at the old phpInfo() output revealed that for some reason PHP was still looking for the old location for the connection socket. Strange, I thought to myself…but who needs a socket? I’ll just fill in login credentials and be done with it. So I added a host, user, pass, for mysql. I restarted apache and bam! – same problem, nothing was working. AT this point I thought: Ok, maybe I’m editing the wrong php.ini file?

I looked at the phpInfo() output again, and it seemed like maybe I was in the wrong file since none of my updates were working. Also I noticed a slightly contradictory output where php seemed to be telling me the ini file was in two locations…ini file location was listed as “/etc/” but the file loaded was “/private/etc/php.ini”. I opened the /private/etc/php.ini file and discovered the reason for this: These are the same file. All the changes I had made were here as well. I’d declared a host, a new socket location, and made many other minor changes that should be appearing. I checked the output again for phpInfo() – Though I’d declared a host, a new socket location, and changed all kinds of settings, the default values still appeared in this area.

So I changed some unrelated settings as a test and restarted apache: Behold…they were changed, but php had ignored ONLY the MYSQL-related settings. What gives?

I verified once more that I was in the correct file and changed a few more settings before and after the mysql section. Those sections all took, but PHP’s mysql connection settings in this php.ini file stubbornly refused to update.

After messing with this for a few hours, I STILL don’t know why I can’t update my php.ini file’s MYSQL settings…but I did find a quick work-around: Just make a symlink between the two locations if you are in a hurry.

$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

That will create a shortcut to the real socket in the place that PHP is looking.

After making that symlink, everything was gold.

Good luck and happy coding!


Leave a Reply

Translate »