0

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.

Leandro Maro
  • 325
  • 1
  • 12
  • 1
    It's like opening a Lock. The Lock opens with a Set of Keys (Username and Password). The question is, the Lock is on whose door. The door here is the hostname. Without Hostname, where are you trying to login? – Raky Jun 11 '21 at 14:07

1 Answers1

2

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.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828