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 [...]

  • #21 : Mykonos

    … [Trackback]

    [...] There you will find 77843 more Infos: agavegroup.com/?p=58 [...]

  • #22 : tablice reklamowe

    … [Trackback]

    [...] Informations on that Topic: agavegroup.com/?p=58 [...]

  • #23 : Clearing IL

    … [Trackback]

    [...] There you will find 39839 more Infos: agavegroup.com/?p=58 [...]

  • #24 : rinoplasty surgury utah

    … [Trackback]

    [...] Read More here: agavegroup.com/?p=58 [...]

  • #25 : la bourse pour les nuls

    … [Trackback]

    [...] Read More: agavegroup.com/?p=58 [...]

  • #26 : Fillmore

    … [Trackback]

    [...] Informations on that Topic: agavegroup.com/?p=58 [...]

  • #27 : madbid avis

    … [Trackback]

    [...] There you will find 40258 more Infos: agavegroup.com/?p=58 [...]

  • #28 : Travel Guide of Greece

    … [Trackback]

    [...] Informations on that Topic: agavegroup.com/?p=58 [...]

  • #29 : round rock assault lawyer

    … [Trackback]

    [...] Read More: agavegroup.com/?p=58 [...]

  • #30 : alex becker seo

    … [Trackback]

    [...] Find More Informations here: agavegroup.com/?p=58 [...]

  • #31 : ????????? ?? amy

    ????????? ?? amy

    ????????? ??marc jacobs ??????????? ?? ?????????????? ?? ??

  • #32 : metformin pcos

    … [Trackback]

    [...] There you will find 39784 more Infos: agavegroup.com/?p=58 [...]

  • #33 : quick loans bad credit

    … [Trackback]

    [...] Find More Informations here: agavegroup.com/?p=58 [...]

  • #34 : how to get rid of love handles

    … [Trackback]

    [...] Read More here: agavegroup.com/?p=58 [...]

  • #35 : criminal lawyer Philadelphia

    … [Trackback]

    [...] Read More: agavegroup.com/?p=58 [...]

  • #36 : ??? ?? ???

    ??? ?? ???

    ???????????????????????????????????????????????? ??????????????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????.. coach ?? ??

  • #37 : SEO firms

    … [Trackback]

    [...] There you will find 78481 more Infos: agavegroup.com/?p=58 [...]

  • #38 : adidas Porsche design 550

    adidas Porsche design 550

    On the same note, it is advisable to avoid leaving the online casino account logged in whether the people around are trustworthy or not. This can result to many problems. For example, a person can manipulate the transactions. The problem can go further…

  • #39 : stone island jackets

    stone island jackets

    unconscious prone position on the gurney. After a pleasant giggle, I simply provide the given out Sea using the originally prescribed medication, break an ammonium nitrate ampule under their nasal area, as well as inform the right now conscious killer…

  • #40 : stone island outlet

    stone island outlet

    These laws and regulations control all of our lives (and also the rest of the World). They run completely as well as with out prejudice — all the time.- Accrued Returns -Good deeds usually lead to some type of goodness returning to a person. In fact,…

  • #41 : Adidas Porsche Design Sport P5000 black

    Adidas Porsche Design Sport P5000 black

    the home.Winters within this upper part (nearly Georgia) of Florida had been cold.- One Thing after Another -I would be a low ranked recruited guy and newly married. We continuously struggled to make payments. Then the actual wife obtained let go from…

  • #42 : concept

    … [Trackback]

    [...] Read More here: agavegroup.com/?p=58 [...]

  • #43 : bring the fresh

    … [Trackback]

    [...] Read More: agavegroup.com/?p=58 [...]

  • #44 : Escorts

    … [Trackback]

    [...] Read More here: agavegroup.com/?p=58 [...]

  • #45 : carlsbad homes for sale

    … [Trackback]

    [...] There you will find 49498 more Infos: agavegroup.com/?p=58 [...]

  • #46 : ALEX BECKER

    … [Trackback]

    [...] Read More here: agavegroup.com/?p=58 [...]

  • #47 : crack,Hack,Cheat,game,tools,engine,generator,key

    … [Trackback]

    [...] Find More Informations here: agavegroup.com/?p=58 [...]

  • #48 : eine bekannteste Taktik um schnell Geld verdienen aller Zeiten

    … [Trackback]

    [...] Read More: agavegroup.com/?p=58 [...]

  • #49 : cfd

    … [Trackback]

    [...] There you will find 10990 more Infos: agavegroup.com/?p=58 [...]

  • #50 : Top Home Business Review

    … [Trackback]

    [...] There you will find 72760 more Infos: agavegroup.com/?p=58 [...]

  • #51 : Yeast Infection No More by Linda Allen

    … [Trackback]

    [...] Find More Informations here: agavegroup.com/?p=58 [...]

  • #52 : Wedding Photography Adelaide

    … [Trackback]

    [...] Informations on that Topic: agavegroup.com/?p=58 [...]

  • #53 : ab exercises video

    … [Trackback]

    [...] Read More: agavegroup.com/?p=58 [...]

  • #54 : dr charles fat loss factor

    … [Trackback]

    [...] Read More here: agavegroup.com/?p=58 [...]

  • #55 : just click the up coming document

    just click the up coming document

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #56 : the truth about cellulite and how to get rid of it

    the truth about cellulite and how to get rid of it

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #57 : Post Brothers

    Post Brothers

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #58 : curtis debord

    curtis debord

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #59 : coach factory outlets extra 20 off

    coach factory outlets extra 20 off

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #60 : How to stop masturbating

    How to stop masturbating

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #61 : photoshop codes

    photoshop codes

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #62 : Metropolitan Antiques 57th

    Metropolitan Antiques 57th

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #63 : Dr Bradley Kurgis

    Dr Bradley Kurgis

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #64 : Kurgis Dermatology

    Kurgis Dermatology

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #65 : Metropolitan Antiques & Gems

    Metropolitan Antiques & Gems

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #66 : W 57Th Metro Antiques

    W 57Th Metro Antiques

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #67 : Metro Fine Arts & Antiques

    Metro Fine Arts & Antiques

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #68 : nishan kohli

    nishan kohli

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #69 : how to cut weight

    how to cut weight

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #70 : 42nd Street Photo

    42nd Street Photo

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #71 : monitoare sh

    monitoare sh

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #72 : viagra

    viagra

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #73 : bali wedding photographer

    bali wedding photographer

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #74 : web hosting coupon

    web hosting coupon

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

  • #75 : jasa penerjemah tersumpah

    jasa penerjemah tersumpah

    Agave Group » Blog Archive » Running PHP and mySQL on OS X : missing php.ini and mySQL sockets

Leave a Reply

name (required)

email (will not be published) (required)

website

comment