4

Using mysql 5.5 on Ubuntu 12.04, what's the easiest way to create 30 new users with their own database?

I know how to use the create user command

mortona42
  • 143
  • 1
  • 3

2 Answers2

4

Script it in bash, probably. Take a list of users, and do something like the following (you will need to modify this heavily):

for line in $ (cat users.txt) do
   mysql < create database $line;
   mysql < create user $line@localhost identified by defaultpassword;
   mysql < grant all privileges on $line to $line@'localhost;
done
Hyppy
  • 15,458
  • 1
  • 37
  • 59
0
#!/bin/bash
UNAMEBASE="drupal"
# number of users to create
COUNT=40
i=1
BTICK='`'
SQL=''
echo "Database  User            Password" > ./drupal_training_users.txt
until [ $i -gt $COUNT ]; do
        UNAME="$UNAMEBASE$i"
        UDB="drupal_$i"
        UPASS=$(makepasswd --char=8)
        echo "$UDB      $UNAME  $UPASS" >> ./drupal_training_users.txt
        Q1="CREATE DATABASE IF NOT EXISTS $UDB;"
        Q2="GRANT ALL ON ${BTICK}$UDB${BTICK}.* TO '$UNAME'@'localhost' IDENTIFIED BY '$UPASS';"
        SQL="$SQL${Q1}${Q2}"
        let i=i+1
done

SQL="$SQL FLUSH PRIVILEGES;"
mysql -u root -p -e "$SQL"
mortona42
  • 143
  • 1
  • 3