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

1

3 Answers 3

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
1
  • Thanks, I read up on bash scripting and wrote the code in the answer below.
    – mortona42
    Jun 19, 2012 at 17:13
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"
0

I have adapted this script for create multiple mysql users and databases of a text file.
In this case, users name and db name is the same.
Syntanx text file is similar:
id1010
id2020
id3030

#!/bin/bash
BTICK='`'
SQL=''
File="users.txt"
Lines=$(cat $File)
for Line in $Lines
do
echo "$Line"
        UNAME="$Line"
        UDB="$Line"
        UPASS="123456" 
        Q1="CREATE DATABASE IF NOT EXISTS $UDB;"
        Q2="CREATE USER $UNAME@'%' IDENTIFIED BY '$UPASS';"
        Q3="GRANT ALL ON ${BTICK}$UDB${BTICK}.* TO '$UNAME'@'%';"
        SQL="$SQL${Q1}${Q2}${Q3}"
done

SQL="$SQL FLUSH PRIVILEGES;"
mysql -u root -p -e "$SQL"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .