配置了 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> exit
Bye
C:\Users\Administrator>mysql
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
C:\Users\Administrator>mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
C:\Users\Administrator>mysql -uroot -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server 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 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> 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> exit
Bye
C:\Users\Administrator>mysql -uroot -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server 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 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> 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> exit
Bye
C:\Users\Administrator>mysql -uroot -p
Enter password: ********
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
C:\Users\Administrator>mysql -uroot -p -h127.0.0.1
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server 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 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> 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: 10
Current database:
Current user: root@localhost
SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
Using delimiter: ;
Server version: 5.7.30-log MySQL Community Server (GPL)
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 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> exit
Bye
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 16
Server 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 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> \s
--------------
mysql Ver 14.14 Distrib 5.7.30, for Win64 (x86_64)
Connection id: 16
Current database:
Current user: root@localhost
SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
Using delimiter: ;
Server version: 5.7.30-log MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 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
--------------