Since I spent quite a lot of time trying to solve this problem, and always came back to this page when I was looking for this error, I will leave my solution here, hoping that someone will save the time I lost. Although in my case I use mariadb, not MySql, you can still adapt this solution to your needs.
My problem
same thing, but my settings are slightly different (mariadb instead of mysql):
Set mariadb with homegrown
$ brew install mariadb
Launched a demon
$ brew services start mariadb
Tried to connect and got the above error
$ mysql -uroot ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
My decision
find out which my.cnf files are used by mysql (as suggested in this comment ):
$ mysql --verbose --help | grep my.cnf /usr/local/etc/my.cnf ~/.my.cnf order of preference, my.cnf, $MYSQL_TCP_PORT,
check where the Unix socket file works (almost as described here ):
$ netstat -ln | grep mariadb .... /usr/local/mariadb/data/mariadb.sock
(you may want to grep mysql instead of mariadb)
Add the found socket file to ~/.my.cnf (if necessary, create it) (provided that ~/.my.cnf was specified when mysql --verbose... started mysql --verbose... -command from above):
[client] socket = /usr/local/mariadb/data/mariadb.sock
Restart your mariadb:
$ brew services restart mariadb
After that, I could start mysql and got:
$ mysql -uroot ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Therefore, instead, I ran the command with superuser privileges and after entering the password I received:
$ sudo mysql -uroot MariaDB [(none)]>
Notes:
Iβm not quite sure about the groups in which you want to add the socket, at first I had it [client-server], but then I thought that [client] should be enough. I changed this and it still works.
When starting mariadb_config | grep socket mariadb_config | grep socket mariadb_config | grep socket mariadb_config | grep socket I get: --socket [/tmp/mysql.sock] which is a bit confusing since it seems that /usr/local/mariadb/data/mariadb.sock is the actual place (at least on my machine )
I wonder where I can configure /usr/local/mariadb/data/mariadb.sock so that it really is /tmp/mysql.sock so that I can use the default settings instead of editing my .my.cnf (but I'm too tired to understand this ...)
At some point, I also did the things mentioned in other answers before coming up with this.