配置了 3 个 root 账号,来源分别为 ::1,127.0.0.1’,localhost。
在账号密码都不输入的情况下,Windows 默认会用 ODBC@localhost 这个用户连接。
在加上 -uroot 后,会优先使用 root@’::1′ 这个账号来连接,用户仍然使用的是 root@localhost。
把 root@’::1′ 账号删除后,会使用 root@localhost 的账号连接。
把 root@localhost 账号也删除后,默认不会使用 root@127.0.0.1 的账号连接,除非指定了 -h127.0.0.1。
在 Windows 中,无论是 ::1、localhost 还是 127.0.0.1,哪怕是使用 -S 进行连接,也都是 TCP/IP 连接。
mysql> select user,host from mysql.user;+---------------+-------------+| user | host |+---------------+-------------+| root | 10.186.64.% || root | 127.0.0.1 || root | ::1 || mysql.session | localhost || mysql.sys | localhost || root | localhost |+---------------+-------------+6 rows in set (0.00 sec)
mysql> exitBye
C:\Users\Administrator>mysqlERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
C:\Users\Administrator>mysql -urootERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
C:\Users\Administrator>mysql -uroot -pEnter password: ********Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 7Server version: 5.7.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select current_user(),user();+----------------+----------------+| current_user() | user() |+----------------+----------------+| root@::1 | root@localhost |+----------------+----------------+1 row in set (0.00 sec)
mysql> drop user root@'::1';Query OK, 0 rows affected (0.06 sec)
mysql> exitBye
C:\Users\Administrator>mysql -uroot -pEnter password: ********Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 8Server version: 5.7.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select current_user(),user();+----------------+----------------+| current_user() | user() |+----------------+----------------+| root@localhost | root@localhost |+----------------+----------------+1 row in set (0.02 sec)
mysql> drop user root@localhost;Query OK, 0 rows affected (0.03 sec)
mysql> exitBye
C:\Users\Administrator>mysql -uroot -pEnter password: ********ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
C:\Users\Administrator>mysql -uroot -p -h127.0.0.1Enter password: ********Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 10Server version: 5.7.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select current_user(),user();+----------------+----------------+| current_user() | user() |+----------------+----------------+| root@127.0.0.1 | root@localhost |+----------------+----------------+1 row in set (0.00 sec)
mysql> \s--------------mysql Ver 14.14 Distrib 5.7.30, for Win64 (x86_64)
Connection id: 10Current database:Current user: root@localhostSSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256Using delimiter: ;Server version: 5.7.30-log MySQL Community Server (GPL)Protocol version: 10Connection: 127.0.0.1 via TCP/IPServer characterset: latin1Db characterset: latin1Client characterset: gbkConn. characterset: gbkTCP port: 3306Uptime: 6 hours 41 min 40 sec
Threads: 1 Questions: 26 Slow queries: 0 Opens: 123 Flush tables: 1 Open tables: 116 Queries per second avg: 0.001--------------
mysql> show variables like 'socket';+-----------------------------------------+-------+| Variable_name | Value |+-----------------------------------------+-------+| socket | MySQL |+-----------------------------------------+-------+3 rows in set, 1 warning (0.01 sec)
mysql> exitBye
C:\Users\Administrator>mysql -uroot -p -S MySQL
Enter password: ********Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 16Server version: 5.7.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \s--------------mysql Ver 14.14 Distrib 5.7.30, for Win64 (x86_64)
Connection id: 16Current database:Current user: root@localhostSSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256Using delimiter: ;Server version: 5.7.30-log MySQL Community Server (GPL)Protocol version: 10Connection: localhost via TCP/IPServer characterset: latin1Db characterset: latin1Client characterset: gbkConn. characterset: gbkTCP port: 3306Uptime: 7 hours 6 min 23 sec
Threads: 1 Questions: 63 Slow queries: 0 Opens: 137 Flush tables: 1 Open tables: 130 Queries per second avg: 0.002--------------