MySQL MySQL中的“由'密码'识别”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31111847/
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
"IDENTIFIED BY 'password'" in MySQL
提问by user3461823
I often see in many MySQL tutorials that people use command IDENTIFIED BY 'password'
both during user creation and granting him privileges.
我经常在许多 MySQL 教程中看到人们IDENTIFIED BY 'password'
在创建用户和授予权限时都使用命令。
For example:
例如:
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database.* TO 'username'@'localhost' IDENTIFIED BY 'password';
I tried using GRANT
without IDENTIFIED BY
and it works.
Can somebody explain me why it is used twice? Could there be other password for specific privileges?
我尝试使用GRANT
withoutIDENTIFIED BY
并且它有效。
有人能解释一下为什么它被使用了两次吗?是否有其他特定权限的密码?
回答by SystemParadox
GRANT
is meant for adding privileges to users. Confusingly, it also has the ability to create users and change their passwords. This functionality is deprecated and should not be used.
GRANT
用于向用户添加权限。令人困惑的是,它还具有创建用户和更改密码的能力。此功能已弃用,不应使用。
If you use GRANT
with IDENTIFIED
you can change the user's password:
如果你使用GRANT
withIDENTIFIED
你可以改变用户的密码:
When IDENTIFIED is present and you have the global grant privilege (GRANT OPTION), any password specified becomes the new password for the account, even if the account exists and already has a password. Without IDENTIFIED, the account password remains unchanged.
As of MySQL 5.7.2, if the account already exists, IDENTIFIED WITH is prohibited because it is intended only for use when creating new accounts.
当 IDENTIFIED 存在并且您拥有全局授予权限 (GRANT OPTION) 时,指定的任何密码都将成为该帐户的新密码,即使该帐户存在并且已经拥有密码。如果没有 IDENTIFIED,帐户密码保持不变。
从 MySQL 5.7.2 开始,如果帐户已经存在,则禁止使用 IDENTIFIED WITH,因为它仅用于创建新帐户时使用。
Also, GRANT
may create the user if it does not exist:
此外,GRANT
如果用户不存在,可以创建用户:
If an account named in a GRANT statement does not exist, the action taken depends on the NO_AUTO_CREATE_USER SQL mode:
- If NO_AUTO_CREATE_USER is not enabled, GRANT creates the account. This is very insecure unless you specify a nonempty password using IDENTIFIED BY.
- If NO_AUTO_CREATE_USER is enabled, GRANT fails and does not create the account, unless you specify a nonempty password using IDENTIFIED BY or name an authentication plugin using IDENTIFIED WITH.
Use of GRANT to define account authentication characteristics is deprecated as of MySQL 5.7.6. Instead, establish or change authentication characteristics using CREATE USER or ALTER USER. This GRANT capability will be removed in a future MySQL release.
如果在 GRANT 语句中命名的帐户不存在,则采取的操作取决于 NO_AUTO_CREATE_USER SQL 模式:
- 如果未启用 NO_AUTO_CREATE_USER,则 GRANT 创建帐户。这是非常不安全的,除非您使用 IDENTIFIED BY 指定一个非空密码。
- 如果启用 NO_AUTO_CREATE_USER,则 GRANT 失败并且不会创建帐户,除非您使用 IDENTIFIED BY 指定非空密码或使用 IDENTIFIED WITH 命名身份验证插件。
从 MySQL 5.7.6 开始,不推荐使用 GRANT 来定义帐户身份验证特征。相反,使用 CREATE USER 或 ALTER USER 建立或更改身份验证特征。此 GRANT 功能将在未来的 MySQL 版本中删除。
See https://dev.mysql.com/doc/refman/5.7/en/grant.html
见https://dev.mysql.com/doc/refman/5.7/en/grant.html
In summary, use CREATE
to create a user, and use GRANT
to add privileges:
综上所述,用于CREATE
创建用户,GRANT
用于添加权限:
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database.* TO 'username'@'localhost';
回答by Zafar Malik
As grant self created user so, below line is enough for rights-
由于授予自己创建的用户,因此以下行足以获得权限-
GRANT ALL PRIVILEGES ON database.* TO 'username'@'localhost' IDENTIFIED BY 'password';
Note: Identify represents to your password what password you want to save for credential verification.
注意:标识代表您要保存的密码以进行凭据验证。
回答by Robbie Averill
It's just an added security measure. You might have different passwords for the same user on different servers, for example in a shared host environment. If it's your own server and you and your colleagues are the only ones who use it then you don't need to identify the users you grant privileges to.
这只是一个额外的安全措施。您可能在不同的服务器上为同一用户使用不同的密码,例如在共享主机环境中。如果它是您自己的服务器,并且您和您的同事是唯一使用它的人,那么您不需要识别您授予权限的用户。
If you identify users then only the password you specify can be used with that user to perform those privileges.
如果您识别用户,则该用户只能使用您指定的密码来执行这些权限。
回答by Adel
Code below gives full access to all databases. Other commands mentioned in answers are work if you have the specific databases in the mysql server. Code below is works regardless of the state in which the server is in. Just login to the linux server -> type mysql and enter:
下面的代码可以完全访问所有数据库。如果您在 mysql 服务器中有特定的数据库,则答案中提到的其他命令也可以使用。无论服务器处于何种状态,下面的代码都有效。只需登录到 linux 服务器 -> 键入 mysql 并输入:
GRANT ALL privileges ON *.* TO 'user'@'host' identified BY 'password';