为什么 Wireshark 在不使用 -h 参数时无法捕获 mysql 登录数据包

why Wireshark can't capture mysql login packets when without using -h parameter

当我执行

$ mysql -u root -p

在终端命令行中,wireshark 无法捕获任何内容。 但是当我执行

$ mysql -u root -p -h 127.0.0.1

在这种情况下,wireshark 可以捕获数据包。见下文:

我想知道 -h 和没有它的区别是什么? 顺便说一下,如果我使用 -h localhost,wireshark 也无法捕获任何东西。

*nix 系统的默认设置是 MySQL 通过不使用网络的套接字文件进行连接。这就是 Wireshark 在这种情况下什么也捕获不到的原因。如果您使用 localhost 而不是 127.0.0.1

,您也应该什么也看不到

来自文档:

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.

捕获 MYSQL 协议

mysql -u userName -p<passWord> --host=localhost --port=3306 --protocol=TCP