暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

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

原创 huayumicheng 2023-11-19
280

同一个授权账户,可能会从不同主机/不同方式连接到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)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论