Accessing MySQL from Ubuntu Bash



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是字符串内容可变的。当我测试时,它不是空的。我做错了什么?

在 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';"

你需要把你的变量放在引号里以获得有效的sql,把你的字符串放在内插引号(")中,以替换变量。

相关内容

  • 没有找到相关文章

最新更新