mysql命令连接mysql服务为什么-P端口写错也能正常连?
感觉非常的迷幻
sh-4.4# mysql -uroot -p -P3307
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3801
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW VARIABLES LIKE 'skip_networking'; | |
---|---|
Variable_name | Value |
skip_networking | OFF |
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'port'; | |
---|---|
Variable_name | Value |
port | 41018 |
1 row in set (0.00 sec)
mysql>
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'port'; | |
---|---|
Variable_name | Value |
port | 41018 |
1 row in set (0.00 sec)
mysql> exit
Bye
sh-4.4# mysql -uroot -p -P999999
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3836
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ^C
mysql>
看到这个问题挺有意思,我也分别用 Docker 跑了下 MariaDB 和 MySQL 试了试。
结合 @唯一丶 兄弟的说法,还有这个讨论,推测应该是 mysql 命令在不主动使用
-h
参数时,默认值是localhost
,然后就会判定走SOCKET
的连接方式。而
SOCKET
连接方式,不会读取-P
/--port
提供的端口号,是直接通过的my.cnf
里配置的/var/run/mysqld/mysqld.sock
进行连接。MySQL
MariaDB
MariaDB 是 MySQL 的一个分支,按理说两者的现象应该是一致的,但是实际测试效果和 MySQL 不太一样。看起来像是默认使用
--protocol=TCP
的方式进行连接。