我的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','[email protected]',ENCRYPT('mypassword',CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))),('1','[email protected]',ENCRYPT('mypassword',CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))),('1','[email protected]',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)))

有什么问题?
提前致谢!

最佳答案

我试过了

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


偶尔得到*0

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

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


这也适用于此处文档。

关于mysql - 如何使用bash与`ENCRYPT()`一起运行mysql脚本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55073307/

10-13 05:03