Created
November 23, 2013 17:58
-
-
Save martinusadyh/7617845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Variable declaration | |
MYSQL_ROOT_USER=root | |
MYSQL_ROOT_PASSWD=admin | |
HOME_DIR=/export/home | |
TMP_FILE=/tmp/LIST_OF_USER.txt | |
SQL_FILE=/tmp/SQL_FILE.sql | |
# Function declaration | |
getListOfUser() { | |
cut -d " " -f2 $TMP_FILE | sort | cut -d " " -f1 | |
} | |
# List available user in home dir | |
ls $HOME_DIR > $TMP_FILE | |
for user in $(getListOfUser) ; do | |
echo "Creating database and db user base on " $user | |
# Creating database with $user | |
echo "create database $user;" >> $SQL_FILE | |
# Create user first | |
echo "CREATE USER '$user'@'localhost' IDENTIFIED BY '$user';" >> $SQL_FILE | |
# Give permission to this $user | |
echo "GRANT SELECT, INSERT, UPDATE, DELETE ON $user.* TO '$user'@'localhost';" >> $SQL_FILE | |
done | |
# Connecting to MySQL then insert to database | |
mysql -u $MYSQL_ROOT_USER -p$MYSQL_ROOT_PASSWD < $SQL_FILE | |
echo "Deleting temporary file" | |
rm -rf $TMP_FILE | |
rm -rf $SQL_FILE | |
echo "Creating MySQL User based on user home done. " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment