Linux
MySQL 查詢不是從 bash 執行的,而是從 MySQL 客戶端工作的
使用免費 16。
MySQL 查詢從 bash 失敗,但可以從 MySQL 客戶端工作:
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}"
執行 bash 腳本後出現以下錯誤:
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' */'
如果我在 MySQL 客戶端(HeidiSQL)中執行列印的 MySQL 查詢,它工作正常:
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. */
請告知如何修復它。
解決方案
正如@yoonix 指出的那樣,是的,缺少 -e 選項。
mysql -u ${mysql_local_user} -p${mysql_local_pass} -h localhost - e "${query}"
現在效果很好。謝謝您的幫助!
我不是 100% 確定 collate utf8_hungarian_ci 部分,但我能夠通過省略反斜杠和刻度,將創建數據庫行更改為:
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;
並在此處添加 -se:
mysql -u ${mysql_local_user} -p${mysql_local_pass} -h localhost -se "${query}"