Fixing the mysql.sock not found error on MAMP

Cakephp logo

I’ve recently been working on a CakePHP project on my Mac, I’ve noticed that when
I tried Baking the project, This error kept popping up :

Warning Error: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in
[/Users/xxxxx/Projects/public_html/xxxxxx/lib/Cake/Model/Datasource/Database/Mysql.php, line 157]

After a lookup, seems lots of people seem to come through this when working on MAMP, to fix that, start your terminal and create a link to “mysql.sock” manually

sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

Now go back and and continueΒ bake πŸ™‚

47 comments

  1. JDR   β€’  

    Thanks a lot!

    I was trying to run many php files from the command line and didn’t know the reason why it was showing up some errors when sql statements were run. It took me days until I found out your simple but smart solution. πŸ™‚

    I assume that php is looking for a path that would be by default if mysql was installed individually and not as part of MAMP as it was my case. Linking a new folder with the active one used by MAMP is a great idea.

    • Mostafaberg   β€’     Author

      I’m glad it helped, it took me sometime to figure it out πŸ™‚

  2. Pingback: I’ve been having problems hooking CakePHP into MAMP… « « Jay Margalus Jay Margalus

  3. 3cents   β€’  

    thanks a lot !!!

  4. Octavian   β€’  

    Thanks a million man!

  5. Frank   β€’  

    Thanks Berg! much appreciated.

  6. Pingback: Scheduled Tasks in SilverStripe | Deadly Technology

  7. Bruno   β€’  

    oh man, thanks mate…. 1 command and you solved 2 days of arguments… πŸ™‚
    Thanks a lot

  8. Guins   β€’  

    Thanks a lot, first search on facebook, just perfect !

  9. Ilie Pandia   β€’  

    Hello,

    Instead of creating a link I think is better to configure the location of the socket in php.ini

    Open the php.ini you’re using with MAMP, look for [Pdo_mysql] and right under that you should find a line like this:

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

    Comment out that line (if not commented already) and then add the new location of the mysql.sock

    pdo_mysql.default_socket=/Applications/MAMP/tmp/mysql/mysql.sock

    Note: If do NOT use MAMP the most likely location on Mac for mysql.sock is /tmp/mysql.sock

  10. Ilie Pandia   β€’  

    I forgot to add that you need to restart you Apache server to pick up the change in php.ini otherwise it won’t work πŸ™‚

  11. Sam   β€’  

    I love you so much! I have been searching on how to fix this for a long bloody time!
    Much appreciated.

  12. Ibrahim Azhar Armar   β€’  

    I had problem using Doctrine CLI with Doctrine framework and gave me similar error message. thanks mate that command solved my problem

  13. Ibrahim Azhar Armar   β€’  

    I had problem using Doctrine CLI with Zend framework and gave me similar error message. thanks mate that command solved my problem

  14. Vick   β€’  

    You are a genius, thanks mate!

  15. Tamil   β€’  

    Thank You very much

  16. Fiqri   β€’  

    Thanks a lot , saved me loads of time πŸ™‚

  17. Gippy   β€’  

    Watch out … the Apple security update can wipe out the link

    • Berg   β€’     Author

      Oh thanks a lot for the tip !

  18. Paulo Rodrigues   β€’  

    Thanks a lot! πŸ™‚

  19. John Erck   β€’  

    Thanks.

  20. Ryan   β€’  

    Thanks for this. Got to love the simple fixes.

  21. Mo   β€’  

    WOW that was easy!

  22. Anonymous   β€’  

    Thanks a lot πŸ™‚ Helped so much !

  23. MidnightFistFight   β€’  

    Man — I just spent the past three hours of my life trying to find the solution to this problem. Fixed me right uP!

  24. Dodg3r   β€’  

    Thanks mate. Exactly what i was looking for πŸ™‚

  25. mohammed   β€’  

    Legend!!!!!

  26. Marc   β€’  

    Very nice, thanks!

  27. Neokio   β€’  

    Thank you FTW!!!

  28. hiroyuki kiyomizu   β€’  

    thanks a lot !!!
    Good job

  29. Rana   β€’  

    Thanks a lot. You saved my day πŸ˜€

  30. Sergei   β€’  

    Thanks man!!!

  31. Nolan   β€’  

    Thanks a ton! Been struggling with this for hours.

  32. makkus   β€’  

    perfect, thank you!

  33. Esdras   β€’  

    Muitissimo Obrigado!
    Resolveu meu problema.

  34. Mark Lilly   β€’  

    Epic, and terse. Time left to bake.
    thx,
    m

  35. Darshan Joshi   β€’  

    Darshans-MacBook-Air-2:~ Darshan1$ sudo mkdir /var/mysql
    Password:
    Sorry, try again.
    Password:
    Sorry, try again.
    Password:

    what to type in password ??

    mysql.sock not showing in folder πŸ™ please help

    • Berg   β€’     Author

      You type your actual account’s password, did you get this to work yet ?

  36. zee   β€’  

    it is 2017 and this just helped me loads!!

    • Berg   β€’     Author

      lol Thanks it’s strange how this is still relevant, glad to help nonetheless πŸ™‚

  37. Donovan   β€’  

    The file does not exists for MAMP but if I use the commands it says: File exists. Even if the hidden files are showed the file really does not exists. Do you maybe know how to fix this issue?

Leave a Reply

Your email address will not be published. Required fields are marked *