Since I spent quite some time trying to solve this and always came back to this page when looking for this error, I'll leave my solution here hoping that somebody saves the time I've lost. Although in my case I am using mariadb rather than MySql, you might still be able to adapt this solution to your needs.
My problem
is the same, but my setup is a bit different (mariadb instead of mysql):
Installed mariadb with homebrew
$ brew install mariadb
Started the daemon
$ brew services start mariadb
Tried to connect and got the above mentioned error
$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
My solution
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 is running (almost as described here):
$ netstat -ln | grep mariadb
.... /usr/local/mariadb/data/mariadb.sock
(you might want to grep mysql
instead of mariadb)
Add the socket file you found to ~/.my.cnf
(create the file if necessary)(assuming ~/.my.cnf
was listed when running the mysql --verbose ...
-command from above):
[client]
socket = /usr/local/mariadb/data/mariadb.sock
Restart your mariadb:
$ brew services restart mariadb
After this I could run mysql and got:
$ mysql -uroot
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
So I run the command with superuser privileges instead and after entering my password I got:
$ sudo mysql -uroot
MariaDB [(none)]>
Notes:
I'm not quite sure about the groups where you have to add the socket, first I had it [client-server] but then I figured [client] should be enough. So I changed it and it still works.
When running 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 the /usr/local/mariadb/data/mariadb.sock
to actually be /tmp/mysql.sock
so I can use the default settings instead of having to edit my .my.cnf
(but I'm too tired now to figure that out...)
At some point I also did things mentioned in other answers before coming up with this.
mysqld
to check things and ensure MySQL shut down properly last time. If it had a 'dirty' shutdown (e.g. if a laptop battery forces a system shutdown) this should clean it up. Then you can start MySQL server again:mysql.server start
. – Abject