我目前正在使用 bash 腳本編寫遷移腳本。 當(dāng)我嘗試通過 bash 腳本中的變量打開數(shù)據(jù)庫時,數(shù)據(jù)庫名稱不正確。 我收到以下錯誤 “‘RROR 1102 (42000):數(shù)據(jù)庫名稱‘development’不正確”
mysql --batch --host=********** --user=**** --password=***** $dbName -e "${fileContents}"
當(dāng)我在 bash 腳本中執(zhí)行此操作時,數(shù)據(jù)庫存在
mysql --batch --host=********** --user=**** --password=***** development -e "${fileContents}"
變量fileContents是SQL中的遷移腳本。 變量 dbName 是數(shù)據(jù)庫的名稱。
我通過以下幾行從數(shù)據(jù)庫中的表中獲取數(shù)據(jù)庫名稱
databaseNames=() shopt -s lastpipe mysql --batch --raw --host=***** --user=**** --password=***** -e 'SELECT database_name FROM users.organisations' | while read dbName guid; do if [ $i -gt 0 ] then databaseNames+=($dbName) fi i=$(($i + 1)) done
數(shù)據(jù)庫數(shù)組中的名稱似乎是正確的,但我認為該數(shù)組把事情弄亂了。 我按如下方式循環(huán) true 數(shù)組。
for dbName in "${databaseNames[@]}" do
您的數(shù)組為空。您必須將 while 循環(huán)更改為
while read dbName guid; do databaseNames+=($dbName) done然后
for dbName in "${databaseNames[@]}" do echo $dbName mysql --batch --raw --host=$host --port=$port --user=$user --password=$password --database="$dbName" -e '${fileContents};') done