Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
You may find some help in a similar question here. serverfault.com/q/105612/984 –  Zoredache Jun 18 '12 at 22:59

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer
    
Thanks, I read up on bash scripting and wrote the code in the answer below. –  mortona42 Jun 19 '12 at 17:13
#!/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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.