0

Ubuntu bash mysql 脚本看起来像这样

mysql -h localhost -u $MYSQLLOGIN -p$MYSQLROOTPASS  -t -e ' 
CREATE USER $USERNAME@'localhost' IDENTIFIED BY $MYSQLPASS;
create database $MYSQLDB;
grant usage on *.* to $USERNAME@localhost identified by $MYSQLPASS;
grant all privileges on $MYSQLDB.* to $USERNAME@localhost';

收到错误信息

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$MYSQLPASS' at line 1

实际上 $MYSQLPASS 是带有字符串内容的变量。当我测试时,它不是空的。我究竟做错了什么?

4

2 回答 2

1

在 Bash 脚本中,当字符串用 . 括起来时,变量不会被它们的值替换'。尝试"改用。

mysql -h localhost -u $MYSQLLOGIN -p$MYSQLROOTPASS  -t -e "
CREATE USER '$USERNAME'@'localhost' IDENTIFIED BY '$MYSQLPASS';
create database $MYSQLDB;
grant usage on *.* to '$USERNAME'@'localhost' identified by '$MYSQLPASS';
grant all privileges on $MYSQLDB.* to '$USERNAME'@'localhost';"
于 2013-10-13T10:34:59.617 回答
1

您需要将变量放在引号中以获取有效的 sql,并将字符串插入引号 ( ") 以替换变量。

于 2013-10-13T10:36:08.780 回答