grant usage to users listed in a file

hi,

i'm trying to grant usage to multiple users whose ids are in a file. i thought that i could put the mysql code within a while loop, but that's not working for me:

while read user; do
   
  userid=$user
  passwd="changeme"
  query="grant usage on mysql.USERS.* to $userid identified by '$passwd'";

  mysql -u<root> -p<password>
  $query;
  flush privileges;
  quit;

done < userid_file

i wonder if anyone can provide some guidance.

thanks.

i thought about this some more and wonder if this is a smarter solution. it seems closer, but i'd still love feedback if there is a better way:

while read user; do
   
  userid=$user
  passwd="changeme"
  print "grant usage on <database>.* to '"$user"' identified by '"$passwd"';"  >> grant_file

done < userid_file


mysql -BCNnqs --disable-pager -u<root> -p<password> <<EOJ
source /PATH/to/grant_file
flush privileges;
quit;

EOJ

for the other newbies, i made some dumb mistakes. the biggest was that i shouldn't have had a ; after quit. i think this is better:

while read user; do
   
  userid=$user
  passwd="changeme"

  mysql -u<root> -p<password>
  grant usage on *.* to '$userid' identified by '$passwd';
  flush privileges;
  quit

done < userid_file