Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql - Script to create user and database with hashed password

Tags:

mysql

Following the instructions at this link, I'm trying to use the following syntax to create an user with a password

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;

However, the penultimate instruction gives this error

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.

So, how can I solve? I don't want to manually insert the result of the SELECT @password (as explained here or here for example), but I'd like to use those result to create automatically a new user.

like image 928
tigerjack Avatar asked Dec 02 '14 18:12

tigerjack


People also ask

How do I create a database from a MySQL script?

Open the MySQL Workbench as an administrator (Right-click, Run as Admin). Click on File>Create Schema to create the database schema. Enter a name for the schema and click Apply. In the Apply SQL Script to Database window, click Apply to run the SQL command that creates the schema.

How does MySQL hash passwords?

The password hash stored by MySQL Server is generated by hashing the plain-text password twice using SHA1. When the client transmits the password to the server, it uses three pieces of information: The SHA1 hash of the plain text password. The SHA1 hash of the the SHA1 hash of the plain text password.


1 Answers

You dont need to create the hash, the passwords are already stored as hash.. This is how a new user is created

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

In your code: Change

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

to

GRANT ALL PRIVILEGES ON forum.* TO 'forum_admin'@'localhost' IDENTIFIED BY 'pass_string';
like image 179
Danyal Sandeelo Avatar answered Sep 30 '22 06:09

Danyal Sandeelo