同一个授权账户,可能会从不同主机/不同方式连接到MySQL Server端,这时 CURRENT_USER()
返回的是对应的授权账户,而 USER()
返回的就是包含该账户当前连接的客户端地址,从不同主机连接过来后得到的结果也不同。
1、新建用户
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.33 |
+-----------+
1 row in set (0.00 sec)
mysql> create user test@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to test@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2、验证
[root@mysql3 ~]# mysql -h192.168.100.83 -utest -p123456 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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> select user(),current_user();
+---------------------+----------------+
| user() | current_user() |
+---------------------+----------------+
| test@192.168.100.83 | test@% |
+---------------------+----------------+
1 row in set (0.00 sec)
mysql> select user,host from mysql.user where user='test';
+------+------+
| user | host |
+------+------+
| test | % |
+------+------+
1 row in set (0.00 sec)