Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

February 20th, 2006
previous     next

I recently upgraded my mac to OS 10.4. Overall I’ve been pleased. Recently I decided to fire up PHP and mySQL and ran into some trouble.

Let me start at the beginning… I’ll cover the problem in a minute.

Getting php running on OSX couldn’t be easier. It is basically two steps:

  1. Go to system preferences, then click on Sharing. Click on ‘Personal Web Sharing’, then click the ‘Start’ Button. Voila, you’ve got Apache up and running. Go to a browser and type ‘localhost’. You should see a welcome page of sorts. The home directory for your new website is located at : Macintosh HD/Library/WebServer/Documents. So far so good
  2. Now, you probably want to enable PHP. You have several options. You can install it yourself, or you can use the version that shipped with OS X. To install the shipped version, do the following:
    1. Open apache’s configuration file, located at: Macintosh HD/private/etc/httpd/httpd.conf (note: this is a hidden file, so you will need to display hidden files first)
    2. In that httpd.conf file, search for this line: LoadModule php4_module , and uncomment it (remove the # sign in front). Done, save that file and close it.

    That’s it. Create a file in your home directory called index.php. In that file write<?php
    echo phpInfo();
    ?>

    Now go back to your browser, and you should see a screen full of purple php data. Easy.

  3. To get mySQL running, go here, download the package and run it. It’s a great package that installs mySQL in about 3 minutes.

So far so good!

Now the problem (and solution)

Everything was moving along nicely - until I tried to connect to my database from a PHP page. To make a (really) long story short, a bunch of research revealed that since OS 10.4, the PHP install tries to connect to mySQL via the socket: /var/mysql/mysql.sock, but mySQL uses /tmp/mysql.sock. So they won’t talk to one another. The trick is to change your php.ini to point to /tmp/mysql.sock.

OS X doesn’t have a php.ini initially. But it does have a php.ini.default located at Macintosh HD/private/etc. (Again, this is a hidden file.) Duplicate that file and rename your copy php.ini. Now search for the line mysql.default_socket. Update that line to say mysql.default_socket = /tmp/mysql.sock.

Now restart the web server (system preferences, sharing, turn personal web sharing off, then on).

That’s it. php sees mysql and all is well with the world.

Comments? Let's hear 'em:

  • #1 : Shrikey

    Why not use MAMP (http://www.mamp.info/)? MAMP is by far more user-friendly in regards to turning the apache and mysql servers on and off. Your way requires obscure Terminal commands to reset or turn off the webserver, and shares your website with the whole world. MAMP is only accessible locally. You can also set the site root to any folder you want.

  • #2 : Whosawhatsis

    mysql.sock has a tendency to disappear after rebooting when it’s in /tmp. MAMP is certainly easier to set up, but I don’t like where it stores things or the fact that it doesn’t give you an easy way to start the daemons without starting the GUI.

    I fixed this problem by creating a /etc/my.cnf file containing the following:

    [mysqld]
    socket=/var/mysql/mysql.sock
    [client]
    socket=/var/mysql/mysql.sock

    This will put the socket back where php and everything else expects to find it, and is more secure and permanent.

  • #3 : Patrick Ryan

    Whosawhatsis-
    I like the sound of this.. I’ll give it a go. thanks.

  • #4 : Sam Stevens

    Any chance that you got PEAR to work with your setup? I haven’t managed to get it working on my iBook: http://www.stevensmedia.com/blog/?p=57

  • #5 : Soumis

    Any chance of telling us how to install GD?

  • #6 : Patrick Ryan

    Soumis:

    I haven’t done it myself (yet) but I bookmarked this page for when I do.

    Give it a go. I’d be interested to hear…

  • #7 : marshall

    Thanks for posting this tip. Please forgive my ignorance… I had a friend help me set all this up about a month ago and he ran into the socket issue in osx 10.4.5 –and he eventually got it going. I recently had to erase&install (after a new hd compliments of applecare!) and want to get things up again without hassling the friend. I have 10.4.6 now, got php (5.1.2) running, and got apache (1.3.33) running.

    I installed the ’startup’ pkg and don’t see it in the system prefs like before, but figured I just needed to mess with the socket-thing. ….I tried following your advice, but can’t save to the ‘etc’ folder –so I’m kind of stuck. BTW, to “duplicate” the php.ini file I was copying it to another folder then opening it, editing it, and was trying to ’save as’ back to the etc folder when I got the no-can-do message.

    Hopefully, you found this amusing and can offer some advice. Thanks. -m

  • #8 : marshall

    ..oh, and I have installed mysql 5.0.19
    along with the startupitem pkg

    Thanks again,
    -m

  • #9 : Richard

    I have been at the same problem for the past two weeks. I tried many suggestions but not luck for me.

    I still have the issue: Can’t connect to local MySql server throught socket /tmp/mysql.sock (2)(code 2002)

    I am going crazy with this.

    Help please

    Thanks

    Richard

  • #10 : Krishenn

    Thank you SO much for posting this - I bought my first Mac (OS X Tiger) and I’ve been playing around with setting up Apache and mySQL and PHP - I ran into the socket issue with PHP and your article just resolved it for me. I was having the DARNEDEST time trying to find the php.ini file!! :-)

    Thanks again for sharing your knowledge.

  • #11 : Collin Allen

    This tip was exactly what I needed. Thanks!

  • #12 : Ben Hollingsworth

    Thanks several millions, spot-on, concise and the ONLY place I found the answer! Gold Star Chap!!

  • #13 : avator

    Fatal error: Call to undefined function mysql_connect() in /Users/teknonation/Sites/Site/test2.php on line 2

    i still get this message when i follow your steps, mysql and php are both working fine, but not together when i try to load mysql in php code, when i check the php info, i also can’t find anything about that mysql is installed for php. Somebody can help me with this?

    Thx

  • #14 : Steve

    hmmm, followed the steps; php info shows that its picking up php.ini, but under the mysql section it persists in wanting /var/mysql/mysql.sock

    I’m 10.4.9 and stuck, any ideas?

    thx, Steve

  • #15 : Chris Sainsbury

    Fantastic - right on the money. I’d never have figured this out! Thanks a lot.

  • #16 : Chris Sainsbury’s Blog » Blog Archive » Developing with php and mysql on mac os x 10.4

    [...] Thanks to this blog post, I found out how to do this. By default OS X points PHP to the wrong MySQL socket. We’ll need to make changes to the PHP config. You have to first make a copy of the default file and then change the new file’s permissions. sudo cp /private/etc/php.ini.default /private/etc/php.ini [...]

  • #17 : Nicholas Marshall

    I found this same error after upgrading to OS10.4.9 from an earlier version of 10.4.
    This article was very helpful after days of receiving mysql.sock errors but I found another article that got me up and running again.
    http://jadecell.com/linmacblog/
    At the bottom of the page is this comment:

    How to fix Warning: mysql_connect(): Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’
    Apparently, OS X Tiger doesn’t allow for PHP MySQL connection out of the box. You get this: Warning: mysql_connect(): Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’
    /etc/php.ini(.default) looks for mysql.sock in the wrong place… two options are to make a symbolic link from the right place to the socket…

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

    I ran those commands in terminal and voila, we’re off and running…

  • #18 : meri

    Thank you sooooo much!g

  • #19 : b

    in reply to #9 Richard,

    I had too installed MAMP and wanted to use the MySql Query Browser, but was getting the same warning you had

    “Can’t connect to local MySql server throught socket /tmp/mysql.sock (2)(code 2002)”

    the solution is to inform the MySql Query Browser or administrator that you want to use the sock located in the Mamp directory

    so check the “more options” or “advance tabs” when setting the connection in the MySql Query browser
    and make sure that the option
    “Connect Using Socket” is set to
    /Applications/MAMP/tmp/mysql/mysql.sock

    (change the dir MAMP to whatever you named the dir in your application folders)

    besides that thanks for the info provided in this article….VERY USEFULL

  • #20 : MK1 Design Blog - Articles, Discussion and Comment about Web Design » Blog Archive » How to set-up and create a MySQL Database driven PHP page Using Mac OS X Tiger 10.4.10 on an Intel Mac as a server - PART 01

    [...] If I am honest I have no idea why but for some reason it works and hopefully this will become apparent when you connect to MySQL from your first PHP page. If you want more info then go to http://www.agavegroup.com/?p=58#comment-13103 [...]

Leave a Reply

name (required)

email (will not be published) (required)

website

comment