Using UBUNTU 16.
MySQL query fails from bash, but works from MySQL client:
query=$(cat << EOF
CREATE DATABASE \`${mysql_local_database}\` /*!40100 COLLATE \'utf8_hungarian_ci\' */; use \`${mysql_local_database}\`; GRANT SELECT, EXECUTE, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, INDEX, INSERT, REFERENCES, UPDATE, LOCK TABLES ON \`${mysql_local_database}\`.* TO 'myuser'@'%'; FLUSH PRIVILEGES;
EOF
)
echo $query
mysql -u ${mysql_local_user} -p${mysql_local_pass} -h localhost "${query}"
I get the following error after executing the bash script:
CREATE DATABASE `mydb` /*!40100 COLLATE 'utf8_hungarian_ci' */; use `mydb`; GRANT SELECT, EXECUTE, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, INDEX, INSERT, REFERENCES, UPDATE, LOCK TABLES ON `mydb`.* TO 'user'@'%'; FLUSH PRIVILEGES;
ERROR 1102 (42000): Incorrect database name 'CREATE DATABASE `mydb` /*!40100 COLLATE 'utf8_hungarian_ci' */'
If I execute the printed MySQL query in MySQL client (HeidiSQL), it works fine:
CREATE DATABASE `mydb` /*!40100 COLLATE 'utf8_hungarian_ci' */; use `mydb`; GRANT SELECT, EXECUTE, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, INDEX, INSERT, REFERENCES, UPDATE, LOCK TABLES ON `mydb`.* TO 'myuser'@'%'; FLUSH PRIVILEGES;
/* Affected rows: 1 Found rows: 0 Warnings: 0 Duration for 4 queries: 0,000 sec. */
Please advise how to fix it.
SOLUTION
As @yoonix pointed, yes, the -e option was missing.
mysql -u ${mysql_local_user} -p${mysql_local_pass} -h localhost - e "${query}"
Now works well. Thanks for the help!
I'm not 100% sure about the collate utf8_hungarian_ci part but I was able to get it to create the database without errors by omitting backslash and ticks, changing the create database line to:
and also added -se here: