I recently tried installing MySQL with homebrew (brew install mysql
) and when I try to run it I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
There is no /tmp/mysql.sock
nor a /var/lib/mysql.sock
.
I've searched and haven't found any mysql.sock
file.
How can I fix this?
You'll need to run
mysql_install_db
- easiest way is if you're in the install directory:Alternatively, you can feed
mysql_install_db
abasedir
parameter like the following:I got the same error and this is what helped me:
1) If you are able to see "mysql stopped" when you run below command;
2) and if you are able to start mysql with below command;
then, adding mysql to services will fix your problem. With this method, mysql will start as service when your operating system started. To do so you can run below command;
After that, you can restart your operating system and try connecting to mysql. I did the same and stop receiving below error;
I hope this helps.
Just to add to these answers, In my case I had no local mySQL server, it was running inside a docker container. So the socket file does not exist and will not be accessible for the "mysql" client.
The sock file gets created by mysqld and mysql uses this to communicate with it. However if your mySql server is not running local, it does not require the sock file.
By specifying a host name/ip the sock file is not required e.g.
After installing macos mojave, had to wipe mysql folder under
/usr/local/var/mysql
and then reinstall viabrew install mysql
otherwise permission related things would come up all over the place.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
Started the daemon
Tried to connect and got the above mentioned error
My solution
find out which
my.cnf
files are used bymysql
(as suggested in this comment):check where the Unix socket file is running (almost as described here):
(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 themysql --verbose ...
-command from above):Restart your mariadb:
After this I could run mysql and got:
So I run the command with superuser privileges instead and after entering my password I got:
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.