简体   繁体   中英

How to use bash to run mysql script with `ENCRYPT()`?

My bash script as below:

#!/bin/bash
db_root_user='root'
db_root_password='rootpassword'
mail_db='mail'
mysql --user="$db_root_user" --password="$db_root_password" << EOF
CREATE DATABASE $mail_db;
USE $mail_db;
CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE users (staff_id SMALLINT unsigned NOT NULL,email_addr varchar(100) NOT NULL, password varchar(106) NOT NULL, PRIMARY KEY (email_addr)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO domains (domain) VALUES ('myd1.com'),('myd2.com'),('myd3.com');
INSERT INTO users (staff_id,email_addr, password) VALUES ('1','admin@myd1.com',ENCRYPT('mypassword',CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))),('1','admin@myd2.com',ENCRYPT('mypassword',CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))),('1','admin@myd3.com',ENCRYPT('mypassword',CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))));
EOF

But I got error as below:
ERROR 1048 (23000) at line 6: Column 'password' cannot be null

Actually, the password column is ENCRYPT('mypassword',CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))

What's the problem?
Thanks in advance!

I tried

mysql -e "select ENCRYPT('mypassword',CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))"

and get occasionally *0

Solution: Escape the dollar signs:

mysql -e "select ENCRYPT('mypassword',CONCAT('\$6\$', SUBSTRING(SHA(RAND()), -16)))"

This works for here documents too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM