MySql - 使用哈希密码创建用户和数据库的脚本

时间:2014-12-02 18:54:51

标签: mysql

按照this link上的说明操作,我正在尝试使用以下语法创建具有密码的用户

DROP DATABASE IF EXISTS forum;
CREATE DATABASE forum;
DELETE FROM mysql.user WHERE Host='localhost' AND User='forum_admin';
#In this way, the password will not be logged in mysql history
SELECT @password:=PASSWORD('forum_admin');
GRANT ALL PRIVILEGES ON forum.* TO 'forum_admin'@'localhost' IDENTIFIED BY PASSWORD '@password';
FLUSH PRIVILEGES;

然而,倒数第二条指令会出现此错误

ERROR 1827 (HY000): The password hash doesn't have the expected format. Check if the correct password algorithm is being used with the PASSWORD() function.

那么,我该如何解决?我不想手动插入SELECT @password的结果(例如herehere解释),但我想使用这些结果自动创建新用户

3 个答案:

答案 0 :(得分:7)

您不需要创建哈希,密码已经存储为哈希.. 这是创建新用户的方式

 create user forum_admin@'localhost'  identified by 'password' ;
 GRANT ALL PRIVILEGES ON forum.* TO 'forum_admin'@'localhost';
 flush privileges;

在你的代码中: 改变

GRANT ALL PRIVILEGES ON forum.* TO 'forum_admin'@'localhost' IDENTIFIED BY PASSWORD 'pass_string';

GRANT ALL PRIVILEGES ON forum.* TO 'forum_admin'@'localhost' IDENTIFIED BY 'pass_string';

创建它的另一种方法

答案 1 :(得分:3)

我也遇到了这个问题。可能是你应该得到你的密码哈希值

<div class="shape">
 <span>Hello</span> 
 <div> Test message </div>
</div>

然后执行grant命令

mysql> select password('1');
+-------------------------------------------+
| password('1')                             |
+-------------------------------------------+
| *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+-------------------------------------------+

一切都会好的。

以下是链接:http://shareolite.blogspot.com/2014/03/how-to-solve-mysql-error-1827-hy000.html

祝你好运!

答案 2 :(得分:0)

这对我有用。希望它会有所帮助。

SELECT @password:=PASSWORD('chosenPassword');
create User 'chosenUser'@'%' identified by '@password';
create database chosenSchema;
GRANT ALL PRIVILEGES ON chosenSchema.* TO 'chosenUser'@'%';