Home > Net >  Mysql bash Incorrect database name when database exist?
Mysql bash Incorrect database name when database exist?

Time:02-24

I'm currently working on a migration script with a bash script. When I tried to open a database via a variable in my bash script the database name is incorrect. I got the following error "'RROR 1102 (42000): Incorrect database name 'development"

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

When I do the this in my bash script the database exists

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

The variable fileContents are the migration scripts in SQL. The variable dbName is the name of the database.

I get the database names from a table in my database via the following lines

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

The names in the database array seem to be right, but I think that the array messes things up. I loop true the array as follows.

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

CodePudding user response:

Your array is empty. You have to change your while loop to

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

Then

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