MySQL中USER()和CURRENT_USER()的区别

2023年 11月 19日 56.3k 0

同一个授权账户,可能会从不同主机/不同方式连接到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)

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论