如何使用bash与`ENCRYPT()`一起运行mysql脚本?

时间:2019-03-09 02:07:04

标签: mysql bash

我的bash脚本如下:

#!/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

但是出现如下错误:
ERROR 1048 (23000) at line 6: Column 'password' cannot be null

实际上,password列是ENCRYPT('mypassword',CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))

出什么问题了?
预先感谢!

1 个答案:

答案 0 :(得分:0)

我尝试了

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

偶尔得到*0

解决方案:逃脱美元符号:

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

这也适用于此处文档。