首頁  >  問答  >  主體

Mysql bash 資料庫存在時資料庫名稱錯誤?

我目前正在使用 bash 腳本編寫遷移腳本。 當我嘗試透過 bash 腳本中的變數開啟資料庫時,資料庫名稱不正確。 我收到以下錯誤 「『RROR 1102 (42000):資料庫名稱『development』不正確」

mysql --batch --host=********** --user=**** --password=***** $dbName -e "${fileContents}"

當我在 bash 腳本中執行此操作時,資料庫存在

mysql --batch --host=********** --user=**** --password=***** development -e "${fileContents}"

變數fileContents是SQL中的遷移腳本。 變數 dbName 是資料庫的名稱。

我透過以下幾行從資料庫中的表中取得資料庫名稱

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

資料庫數組中的名稱似乎是正確的,但我認為該數組把事情弄亂了。 我如下循環 true 數組。

for dbName in "${databaseNames[@]}" do

P粉818125805P粉818125805264 天前388

全部回覆(1)我來回復

  • P粉714844743

    P粉7148447432024-01-30 10:57:21

    您的陣列為空。您必須將 while 循環變更為

    while read dbName guid; do
          databaseNames+=($dbName)
    done < <(mysql --batch --raw  --host=$host --port=$port --user=$user --password=$password  --database="yourdatabase" -e 'SELECT database_name FROM users.organisations;')

    然後

    for dbName in "${databaseNames[@]}"
    do
      echo $dbName
      mysql --batch --raw  --host=$host --port=$port --user=$user --password=$password  --database="$dbName" -e '${fileContents};')
    done

    回覆
    0
  • 取消回覆