Last Updated: February 25, 2016
·
122.1K
· timfernihough

SOLVED: Can't connect to local mySQL server through socket /tmp/mysql.sock

So you're booting up a new local environment for the first time or perhaps you've had some form of system update and you've lost your settings. You don't use OSX native and you prefer to use a MAMP stack. You've also gotten used to using the command line for everything - even most of your database querying. You've got MAMP booted up and mySQL running but then you try to connect to mySQL

mysql -u root -ppassword

BAM.

Can’t connect to local MySQL server through socket /tmp/mysql.sock

Uh, what? Why are you trying to use the mySQL that didn't come with my MAMP? Well turns out there are two ways of dealing with this. I did the less elegant solution first before I came across the root cause solution more recently.

The mySQL client by default attempts to connect through a local file called a socket instead of connecting to the loopback address (127.0.0.1) for localhost. The default location of this socket file, at least on OSX, is /tmp/mysql.sock.

QUICK, LESS ELEGANT SOLUTION
Create a symlink to fool the OS into finding the correct socket.

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

You will need to reboot your mySQL before this will take effect.

PROPER SOLUTION
You can make a change to the native MySQL configuration file (/etc/my.cnf) that will permanently set the socket file used by the MAMP mySQL client.

This article by Quinn McHenry shows the few lines of code to do this, though I caution you that the destination for MAMP users should be something along the lines of:

/Applications/MAMP/tmp/mysql/mysql.sock

and this will be explicitly defined in the startMysql.sh file in /Applications/MAMP/bin.

Hopefully this helps prevent someone from having a rough morning.

9 Responses
Add your response

The "ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock" saved me alot of time, thanks alot! :)

Even if it's the ugly solution, it was ok for a programming contest rush with a broken mamp install for quick local devlopment.

over 1 year ago ·

@gableroux

Glad it helped!

over 1 year ago ·

Thanks !

over 1 year ago ·

Unfortunately I tried it and it didn't work running 5.5.29

I still get the error #2022 when I go into PHPMyAdmin

over 1 year ago ·

Thank you it helped me alot..

over 1 year ago ·

Thanks a lot. This almost drive me crazy

over 1 year ago ·

Hello,

I found a solution,

add this to your my.cnf file. without "---".

[client]

socket=/var/lib/mysql/mysql.sock # change this to your sock file.

Enjoy.

over 1 year ago ·

thanks @peacengell! Appreciate the further knowledge share

over 1 year ago ·

Thank you very much for sharing the solution !

over 1 year ago ·