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
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.
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:
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.