Question

Ubuntu bash mysql script looks like that

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

Getting error message

 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

In fact $MYSQLPASS is variable with string content. And it's not empty, when I test. What am I doing wrong?

Was it helpful?

Solution

In Bash scripts, variable are not replaced by their value when string is enclosed by '. Try using " instead.

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

OTHER TIPS

You need to put your variables in quotes to get valid sql, and your string in interpolating quotes (") to have the variables substituted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top