如何为 MySQL 数据库用户设置散列密码

How to set hashed password for MySQL database user

我的老板希望我为我们的开发人员编写一个脚本 运行,以便在他们的开发版本上创建一个新的数据库用户。他不希望我在代码中有实际的密码;只有哈希。我有散列密码,但是当我尝试使用该散列密码创建用户时;它只是再次散列散列密码。

CREATE USER 'test_user1'@'localhost' IDENTIFIED BY 'password'; 显示哈希密码是“*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19”

但是如果我尝试... CREATE USER 'test_user2'@'localhost' IDENTIFIED BY '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19';

"password" 实际上不是我 test_user2 的密码。

我也尝试了以下 UPDATE 但 "password" 仍然不适用于 test-user2:

UPDATE `mysql`.`user` SET `authentication_string` = '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19' WHERE (`Host` = 'localhost') and (`User` = 'test_user2');

既然我已经知道哈希值,我该如何防止它对我输入的值进行哈希处理?

在我的测试中,我还为两个用户提供了 运行 GRANT SELECT

我认为(这里可能是错误的,不确定)实际上哈希被哈希多少次并不重要,它仍然代表相同的密码。因此,您可以 运行 对您的输入和 MySQL 哈希的输出进行相同的哈希检查。

关键是要用IDENTIFIED WITH mysql_native_password AS 'hash'。例如,考虑以下内容:

CREATE USER 'jeffrey'@'localhost'
  IDENTIFIED WITH mysql_native_password AS 'hash-goes-here';

相关文档摘录:

IDENTIFIED WITH auth_plugin AS 'auth_string'

Sets the account authentication plugin to auth_plugin and stores the 'auth_string' value as is in the mysql.user account row. If the plugin requires a hashed string, the string is assumed to be already hashed in the format the plugin requires.