Stefan Fischerländer’s Blog One Blog Is Not Enough

Mac OS X: Trouble with MAMP MySQL

MAMP is for Mac what XAMPP is for windows: A very easy-to-use, but Mac-like web development solution. Everything worked out-of-the box as it should - until I tried to install Wordpress on my MacBook. I got a database connection error very early during the Wordpress installation process.

So I tried to talk to the mysql server with the mysql command line tool and got this error message:
macbook:~ sf$ /Applications/MAMP/Library/bin/mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

This was strange as the mysql server was indeed running; I could verify this by an ps command. Futhermore phpMyAdmin, as provided by MAMP, worked flawlessly. Doing some Google research told me that the problem was connected with the location of mysql.sock.

Running ps -awwx | grep mysql showed, that mysqld had been started by MAMP with the option --socket=/Applications/MAMP/tmp/mysql/mysql.sock, whereas the mysql tool and Wordpress assume the socket file here: /tmp/mysql.sock.

With this information the solution is easy:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
This creates a symbolic link from the location expected by mysql and wordpress to the location where the socket file resides. Now I can access my mysql server with the mysql command line tool and my local Wordpress installation can also talk to the database.

50 Responses to “Mac OS X: Trouble with MAMP MySQL”

  1. » Blog Archive » Mac OS X: Trouble with MAMP MySQL says

    […] Mac OS X: Trouble with MAMP MySQLMAMP is for Mac what XAMPP is for windows: A very easy-to-use, but Mac-like web development solution. Everything worked out-of-the box as it should - until I tried to install Wordpress on my MacBook. I got a database connection error very early during… […]

  2. I thought using a Mac was supposed to be simple? | The Social Publishing Blog says

    […] socks, sockets and sudo’s - something i really don’t care or want to learn. Thankfully, the fix was simple, but virtually impossible to guess or work out the solution for oneself. I thought using […]

  3. Matt says

    thanks for the help Stefan - this is really helpful!

  4. Matt says

    Can anyone tell me how the above fix can be remembered after each re-boot? at the moment i have to re-run the sudo command every time i start up the OS. Any help, or pointers greatly appreciated!

  5. Andrea says

    Hi, I’m very interested about this topic.
    I’m a new “Mac-User” and I need to use my laptop as a local server for php pages.
    I have installed MAMP 1.7: everything works fine using the following ports:
    Apache: 8888
    MySQL: 8889
    The problem is when I try to create a new connection to the database using Dreamweaver CS3: if I use these parameters

    Connection name: my_connection
    Server: localhost:8888
    Username: root
    Password: root
    Database: my_database

    I have the following error message: “MySQL Error#: 2003 - Can’t connect to MySQL server on localhost (10061)”

    All the parameters are correct: if I edit an test a php page like this:

    everything is ok.

    Why Dreamweaver can’t find MySQL connection?
    I hope someone can help me.
    Thank you in advance!

  6. Rob says

    Genius! Thank you for working this problem out, it had been bothering me for a long time now.

  7. Stefan says

    Andrea,
    you wrote your MySQL-Server listens on port 8889, but your Dreamweaver configuration tries to access the database server on port 8888 - which is the Apache server and not the MySQL server. Try to use localhost:8889.

  8. Andrea says

    Thank you!!! Now it works!!!
    Andrea

  9. Todd says

    Thanks for the tip!

  10. Matt says

    I have tried the tip that you have provided, and it worked for a few of the accounts I manage on our Xserve, but not others. After creating the alias, it works for the root account, admin account, and one of the other user accounts, but all the others I have tested so far give me the old error I was receiving prior: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (13). Any suggestions?

  11. Matt says

    Nevermind, I figured it out =) It was simply a mixture of cap-sensitivity between a few users and their database names, and file-permission settings on the /Applications/MAMP/tmp directory.

  12. Michael says

    THANK YOU. :-)

  13. Andrew Maier says

    Helped me get to sleep tonight :)

  14. firecall says

    Thanks for that. Seems to work when trying to use PEAR DB::connect - when the MAMP path directly wouldnt seem to… but I may just be crazy. :)

  15. Mark Tourtellott says

    Thanks! This was an easy fix for me thanks to you.

  16. roosta says

    Hi!

    I tried this, but after running that line I assumed i could type:

    mysql -h hostname etc…

    but i get
    -bash: mysql: command not found

    Its probably amazingly simple, but im new to command line stuff on the Mac….

  17. roosta says

    oh, its oK!

    worked it out

    Anyway, great fix, thanks!

  18. JD says

    i tried this on OS 10.5 but it still gives me the command not found error when i try and do a command line mysql command. any ideas?

  19. Stefan says

    Command not found is another kind of error. Did you try to invoke the mysql command with the complete path, e.g. /Applications/MAMP/Library/bin/mysql? If you just want to type mysql to start the command line tool, then you have to put your /Applications/MAMP/Library/bin directory into your default path.

  20. Mike’s Blog » Blog Archive » MAMP + Aptana with radrails says

    […] for me Stefan Fischerländer already found out how to solve […]

  21. mike says

    Thanks for interesting post

  22. Nico says

    Thanks a lot ! I have been looking during two days on the net to find a solution to that very problem. I found your post… and it works !
    Thanks
    Nico

  23. Matthew says

    thanks for this - saved me hours of headache - particularly on data imports

  24. cliff says

    I’ve run into an error trying to start MySql:

    080826 09:38:04 mysqld started
    080826 9:38:04 [ERROR] /Applications/MAMP/Library/libexec /mysqld: unknown option ‘–skip-federated’

    080826 09:38:04 mysqld ended

    I don’t suppose anyone else has seen this?

    Thanks!

    Cliff

  25. Paweł Philipczyk says

    Easy solution:
    When you configure connection in CocoaMySQL you have field called “Socket”. Now when you launch your MAMP start page it tells you to use UNIX socket: /Applications/MAMP/tmp/mysql/mysql.sock.
    I pasted that socket into Socket field and it worked for me :)

  26. augusto says

    better
    /Applications/MAMP/Library/bin/mysql -uroot -p

    than

    /Applications/MAMP/Library/bin/mysql -u root -p

  27. DJIO says

    Just to say Thanks!!!
    =D

  28. Vijay says

    Thanks !
    It saved me a day !

  29. Carlos Santos says

    Thank you! Really helped me! :D

  30. Kenny says

    Hi. I am using a macintosh and I am having troubles with my MAMP. I have just recently downloaded it and the mySQL light won’t come on when I go to start the servers. Apache is the only light that comes on. Please somebody help me!

  31. Seattle Web Design says

    Brilliant! Thanks for the great tip.

  32. headspun says

    Suddenly after making changes in phpMyAdmin to some tables, I can no longer connect to MAMP start page. Now: Error: Could not connect to MySQL server! is what I see. I have tried to change MySql Port to 3306, back to 8889. Also, have attempted various terminal commands. Viewing my index.php (Applications/MAMP/bin/mamp) as i read in previous post has: $link = @mysql_connect(’:/Applications/MAMP/tmp/mysql/mysql.sock’, ‘root’, ‘root’);
    Any thoughts? Maybe I changed username/password from phpMyAdmin? please help

  33. headspun says

    okay,

    so i deleted and reinstalled MAMP with success in launching start page. hope this helps.

  34. matt says

    Thanks a lot. This article saved me.

  35. Jean-Marc says

    Cheers Dude,
    you saved me from a massive headache. I had the issue while debugging php/mysql stuff with Aptana and MAMP.
    The symbolic link saved it! I have the feeling that people trying to do the same with Zend Studio would love you too.
    May be you’d like to post an update to this one.
    By the way don’t listen to the loosers afraid by command line stuff, Mac OS is UNIX and it’s powerful ;)
    Take care

  36. binyuevil says

    Yes, the solution works! Thanks a lot!!!

  37. Tyler G says

    Holy Shit! You just ended a two day, no holds barred throwdown between me and MAMP MySQL. Now I can finally get some work done. Thank You!

  38. Jordan Brough says

    Thank you. Ran into the exact same problem with my MAMP installation. Now it works like a charm!

  39. Donald says

    Hi - I’m new to this, fyi ;) I opened terminal and executed:
    sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

    I assumed this was the way to fix the issue. But… it still doesn’t work. I’m running OS X 10.5.6. When I go to localhost:8888/wordpress/ I’m still getting “Error establishing a database connection”.

    Did I do this incorrectly?

  40. Diemie says

    Thanks, works like a charm!
    Just one thing:
    Like Matt said at the moment i have to re-run the sudo command every time i start up the OS.
    Is there a way how I could make this link permanent???
    Thanks!

  41. MySQL++ on Mac using MAMP « seandenigris.com says

    […] in the default location, not wherever MAMP’s mySQL server is.  One solution, which I found here, is to put a symbolic link in the place where the library looks, which points to the real file in […]

  42. Waldemar Axdorph says

    Wow, thanks man! Solved my problem with Sequel Pro!

  43. martin says

    it seems i have to do this every time i fire up my mac. Is there a way to script this and have that script run on startup?
    thanks
    M

  44. Scott says

    Guys, the mysql.sock file gets created when MAMP is started and is deleted when MAMP is stopped, that’s why it’s in /tmp/ - because it’s temporary. Creating an alias to this file works, but when the original is deleted, the alias is also removed.

    A more permanent solution is to edit Mamp’s configuration files:

    /Applications/MAMP/bin/startMysql.sh and
    /Applications/MAMP/bin/stopMysql.sh

    Just replace

    socket=/Applications/MAMP/tmp/mysql/mysql.sock

    with

    socket=/tmp/mysql.sock

  45. David says

    I have been trying to connect MySQL WorkBench (MWB) for Mac but not joy.

    My local host is ok and can connect to DB in shell

    1) tried the symlink solution but couldn’t connect with Error (61) that cannot connect

    2) changed paths in MAMPs menu (changing those files in MAMP folder does noting) in File > Edit templates > MySQL my.conf, then php5.ini and so on -> didn’t work (after restart)

    3) changed PATH/MAMP/bin/startMysql.sh and stopMysql.sh -> didn’t work when reloaded MAMP
    IT ONLY WORKED when I killed processes and run those files manully in shell then was able to connect to MWB.

    There used to be socket custom field in Mysql Browser that allowed to connect to socket in a different location. No such thing in MWB anymore. Why? don’t ask me - left feedback on MySQL forums asking for answers.

    Things might stop working if you just change paths as described above because php.ini and many other scripts have this path to mysql.sock set to your MAMP folder. Just run in your MAMP this command to see how many:

    egrep -alR –regexp=”/tmp/mysql/mysql.sock” *

  46. Viking says

    Thanks,this post just help me much.

  47. Michael Braden says

    I have MAMP running on a Mac Mini on my local network. I’ve set up an ODBC connection to the MySQL database using the MySQL ODBC 5.1 driver and it connects fine using the localhost/port 8889/Applications/MAMP/tmp/mysql/mysql.sock settings.

    However, when I try to set up an ODBC connection from my MacBook to the same mysql db on that machine, I get an error: “Request Returned with SQL Error” I’m guessing this has to do with how I’m referencing the port . . .

    Any suggestions?

  48. DJIO says

    Thanls a lot for the symbolic link tip.
    Simple, easy and effective.

    SeeYa!

    Djio

  49. Daniel says

    Thank you! Perfect! I’ve been stuck with this problem so many times!

  50. chrisitan says

    thanx for that one!

Leave a Reply