-
Notifications
You must be signed in to change notification settings - Fork 23
/
mysql-create-user
executable file
·81 lines (71 loc) · 2.18 KB
/
mysql-create-user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
#
# mysql-create-user : Create a new user and database on your local mysql server
#
# Version 0.1 (2010/09/01)
# (c) 2010 Mathieu Comandon
# Licensed under the terms of the GPL Version 3
#
set -e
usage()
{
cat << EOF
Usage: $0 --user=mysql_user --password=mysql_password
Create a new user and database on your local mysql server
OPTIONS:
-u, --user=user MySQL user
-p, --password=password MySQL password
-r, --root-password MySQL root password
-h, --help Prints this message
EOF
}
verbose=false
#Parse arguments
if [ "$#" -eq 0 ] ; then
usage
exit 2
fi
PARAMS=`getopt -n $0 -o u:p:r:h:v --long user:,password:,root-password:,help,verbose -- "$@"`
eval set -- "$PARAMS"
while true ; do
case "$1" in
-u|--user) mysql_user=$2; shift 2 ;;
-p|--password) mysql_password=$2 ; shift 2 ;;
-r|--root-password) root_password=$2 ; shift 2 ;;
-h|--help) usage ; exit 1 ;;
-v|--verbose) verbose=true ; shift ;;
--) shift ; break ;;
*) usage ; exit 2 ;;
esac
done
#Error checking
error_state=0;
if [ -z "$mysql_user" ] ; then
echo "You MUST specify MySQL user !"
error_state=1
fi
if [ -z "$mysql_password" ] ; then
echo "You MUST specify MySQL password !"
error_state=1
fi
if [ "$error_state" = 1 ] ; then
echo "There are errors in your arguments, exiting."
exit 2
fi
if [ $verbose == true ]; then
echo "Creating user \"$mysql_user\" with associated database"
echo "Password: \"$mysql_password\""
echo "Root password: \"$root_password\""
fi
cd /tmp
cat > create_user.sql << EOF
GRANT USAGE ON *.* TO '$mysql_user'@'localhost';
DROP USER '$mysql_user'@'localhost';
DROP DATABASE IF EXISTS $mysql_user ;
CREATE USER '$mysql_user'@'localhost' IDENTIFIED BY '$mysql_password';
GRANT USAGE ON * . * TO '$mysql_user'@'localhost' IDENTIFIED BY '$mysql_password' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
CREATE DATABASE IF NOT EXISTS $mysql_user character set utf8;
GRANT ALL PRIVILEGES ON $mysql_user . * TO '$mysql_user'@'localhost';
EOF
mysql -uroot -p"$root_password" < create_user.sql
rm create_user.sql