简体   繁体   中英

Can not connect docker mysql that's forwarding to 3306

When I'm trying to connect to a docker MySQL that's running and forwarding to my local TCP:3306 I get the following answer

mysql -u root -pPASSWORD
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

but when I do mysql -u root -pPASSWORD -h127.0.0.1 it connects wonderfully. Any clue?

[UPDATE]

Considering the comments and this post I create ~/.my.cnf with this content

[Mysql]
user=root
password=yourpass
host=127.0.0.1
port=3306

Giving these changes I could connect the localhost with the 127.0.0.1 address.

If you don't specify a host with -h (or a host directive in your.my.cnf), then MySQL defaults to connect to localhost. Connections to localhost use the UNIX domain socket interface, not TCP/IP. So it's not connecting to a TCP/IP port, and therefore does not forward to your docker container.

This distinction between localhost and 127.0.0.1 is a historical oddity of MySQL. Normally localhost and 127.0.0.1 are assumed to be equivalent. But MySQL treats the hostname "localhost" as special, using it to invoke the UNIX domain socket interface. This is a bit faster than using TCP/IP , but of course only works if the connection is on the local computer.

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