简体   繁体   中英

MySQL server socket '/tmp/mysql.sock' connection issue

I am a complete beginner, so please forgive me, but I would greatly appreciate a step-by-step walk through of how I can fix the following issue that is literal and detailed (eg, instructing me to " Verify that the /tmp/mysql.sock file exists " is not helpful, as it does not explain how to verify that the file exists). My operating system is Mac OS X Version 10.8.5. I'm trying to install MySQL through Homebrew, which is required by an online course that I cam taking.

To uninstall the MySQL that I had previously installed on my computer, I entered the following commands in my Terminal:

$ brew unlink mysql
$ brew uninstall mysql

To install MySQL using Homebrew, I then entered the following command into my Terminal:

$ brew install mysql

The install was successful, but resulted in the following ambiguous "caveat":

==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

I decided to ignore this caveat and entered the following command to start the mysql server:

$ mysql.server start

Great! Entering this command generated the following message in my Terminal:

Starting MySQL
 SUCCESS! 

I next attempted to connect to my mysql server by entering the following command:

$ mysql -uroot

When I entered the above command, my Terminal produced the following error message:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

How do I eliminate this error message and connect to my MySQL server? I have spent several hours troubleshooting this issue with no luck. As suggested in the top answer on stackoverflow here , to see if mysql is starting properly, I entered the following command:

$ /usr/local/bin/safe_mysqld

This resulted in a message stating that

No such file or directory

exists, which leads me to believe that this was not in fact a command but a directory path. So, again, my question is, how do I connect to mysql without getting the error message pasted above? Your suggestions are greatly appreciated and please let me know if I need to clarify anything in this question.

You can locate safe_mysqld executable by running:

which safe_mysqld
whereis safe_mysqld

The message you ignored, could be important. You should take a look at /etc/my.cnf file. If you cannot manage to fix it, you should probably remove it (or back it up somewhere, and then remove), and try reinstalling mysql.

Also you should try mysql -h 127.0.0.1 -u root - this way the client will try to connect through TCP/IP loopback, rather than socket file.

Another file you should take a look at is ~/.my.cnf.

In configuration files, try to find mysql.cock. It could be configured differently in /etc/my.cnf and in ~/.my.cnf.

I would advise you to read man pages for mysql and my.cnf, which contain the information you need to configure mysql properly on your system.

One more thing you should check, is the configured location for mysqld.sock file. The /tmp dir is uncommon location for it. It usually resides somewhere in /var/run/

If mysqld has started properly, mysqld opens unix domain socket file and you can figure out it from /proc/net/unix (outout is sanitized)

[xxxxxx@host fd]$ cat /proc/net/unix
Num       RefCount Protocol Flags    Type St Inode Path
fffffffffffffff0: 11111111 11111111 12311111 1231 01 1234508758 /path/to/socket/mysql.sock
....
fffffffffffffff0: 11111111 11111111 11111111 1232 01 1234513046
fffffffffffffff0: 11111111 11111111 11111111 1232 01 1234513025
fffffffffffffff0: 11111111 11111111 11111111 1232 01 1234568184
fffffffffffffff0: 11111111 11111111 11111111 1232 01 1234543175
fffffffffffffff0: 11111111 11111111 11111111 1232 01 1234582424
fffffffffffffff0: 11111111 11111111 11111111 1232 01 1234550606
fffffffffffffff0: 11111232 11111111 11111111 1232 01 12345

If there are many mysqld instances, output may has many socket files. in this case, if you know mysqld pid (you could get pid from ps -ef | grep mysqld )

[xxxxxx@hostname fd]$ ls -l /proc/[pid]/fd | grep socket
lrwx------ 1 user user 64 01월 30 03:18 12 -> socket:[1234508758]
lrwx------ 1 user user 64 01월 30 03:18 13 -> socket:[1211111111]
lrwx------ 1 user user 64 01월 30 03:18 42 -> socket:[3333333333]

if my case, mysqld has three sockets, one of them (1234508758) is for mysql.sock

UPDATE

Sorry, I assumed you are using *NIX (thanks @Michael), not sure Mac support /proc/net/unix . but it seems that Mac has lsof . I wonder if following command works for you.

$ /usr/sbin/lsof | grep mysqld | grep sock
mysqld     1111 username   ....  /path/to/mysql.sock

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM