MySQL 用户 'User'@'%' 和 'User'@'localhost' 不一样吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11634084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:15:02  来源:igfitidea点击:

Are Users 'User'@'%' and 'User'@'localhost' not the same?

mysql

提问by EdgeCase

I created a user per the first command but cannot logon via localhost (linux). This link mysqldocindicates that I need to create a second user by the same name, but using the syntax in the second block of commands.

我根据第一个命令创建了一个用户,但无法通过 localhost (linux) 登录。此链接mysqldoc表示我需要使用相同的名称创建第二个用户,但使用第二个命令块中的语法。

mysql> CREATE USER 'myuser'@'%' IDENTIFIED BY '4myuser';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL ON my_upload.* TO 'myuser'@'%' IDENTIFIED BY '4myuser';
Query OK, 0 rows affected (0.00 sec)

So I tried that as below, and it indeed worked. But are these two separate users? If I change the pw for one, will the other one sync, or are they truly separate users?

所以我尝试如下,它确实有效。但这两个用户是独立的吗?如果我为一个更改密码,另一个会同步吗,或者他们真的是分开的用户吗?

mysql> CREATE USER 'myuser'@'localhost' IDENTIFIED BY '4myuser';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON my_upload.* TO  'myuser'@'localhost';
Query OK, 0 rows affected (0.00 sec)

采纳答案by Mike Brant

User@%would allow access from all locations. User@localhostwould only allow access from localhost. They are two different users with two different passwords (though you can set them to the same password, but if you update one password the other will not auto-update)

User@%将允许从所有位置访问。 User@localhost只允许从本地主机访问。他们是两个不同的用户,有两个不同的密码(虽然您可以将他们设置为相同的密码,但如果您更新一个密码,另一个将不会自动更新)

回答by Bjoern

Basically yes, those are two different users with (possibly) different permissions and (possibly) different passwords.

基本上是的,这是两个不同的用户,具有(可能)不同的权限和(可能)不同的密码。

  • myuser@% : User myuser, connecting from any host.
  • myuser@localhost : User myuser, connecting from localhost only.
  • myuser@% :用户 myuser,从任何主机连接。
  • myuser@localhost :用户 myuser,仅从 localhost 连接。

A good read is the MySQL manual about connection access, it demonstrates how it works.

一个很好的阅读是关于连接访问MySQL 手册,它演示了它是如何工作的。

回答by dman

I encountered the same situation as described - Adding an entry for user@%was not working.

我遇到了与描述相同的情况 - 添加条目user@%不起作用。

Yetadding an entry for the same user@localhostwouldstart to work again though. This did seem unintuitive, given our understanding of the %pattern!

然而,为相同的条目添加一个条目user@localhost再次开始工作。鉴于我们对%模式的理解,这确实看起来不直观!

An issue identified by Kent above was that: We had an entry row in the users table for host:localhostbut the userwas blank. This apparently resolved to a rule for %@localhostwhich was being used as a match before my user@%rule.

上面 Kent 发现的一个问题是:我们在 users 表中有一个条目行,host:localhost但该user条目为空。这显然解决%@localhost了在我的user@%规则之前被用作匹配的规则。

Short answer - check for blank or wildcard usernames in your usertable.

简短回答 - 检查user表格中的空白或通配符用户名。

... I have no idea how that blank got there or if it was intentional.

...我不知道那个空白是怎么到那里的,或者是故意的。

回答by Konerak

Even if they would mean the same, or if one would include the other, they ARE indeed separate users!

即使它们的意思相同,或者如果一个包含另一个,它们确实是不同的用户!

13.7.1.3. GRANT Syntax

13.7.1.3. GRANT 语法

MySQL and Standard SQL Versions of GRANT

The biggest differences between the MySQL and standard SQL versions of GRANT are:

  • MySQL associates privileges with the combination of a host name and user name and not with only a user name.

GRANT 的 MySQL 和标准 SQL 版本

GRANT 的 MySQL 和标准 SQL 版本之间的最大区别是:

  • MySQL 将权限与主机名和用户名的组合相关联,而不仅仅是与用户名相关联。