forked from dgraziotin/osx-docker-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_mysql_users.sh
executable file
·61 lines (48 loc) · 1.91 KB
/
create_mysql_users.sh
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
#!/bin/bash
/usr/bin/mysqld_safe > /dev/null 2>&1 &
RET=1
while [[ RET -ne 0 ]]; do
echo "=> Waiting for confirmation of MySQL service startup"
sleep 5
mysql -uroot -e "status" > /dev/null 2>&1
RET=$?
done
PASS=${MYSQL_ADMIN_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${MYSQL_ADMIN_PASS} ] && echo "preset" || echo "random" )
echo "=> Creating MySQL admin user with ${_word} password"
mysql -uroot -e "CREATE USER 'admin'@'%' IDENTIFIED BY '$PASS'"
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION"
CREATE_MYSQL_USER=false
if [ -n "$CREATE_MYSQL_BASIC_USER_AND_DB" ] || \
[ -n "$MYSQL_USER_NAME" ] || \
[ -n "$MYSQL_USER_DB" ] || \
[ -n "$MYSQL_USER_PASS" ]; then
CREATE_MYSQL_USER=true
fi
if [ "$CREATE_MYSQL_USER" = true ]; then
_user=${MYSQL_USER_NAME:-user}
_userdb=${MYSQL_USER_DB:-db}
_userpass=${MYSQL_USER_PASS:-password}
mysql -uroot -e "CREATE USER '${_user}'@'%' IDENTIFIED BY '${_userpass}'"
mysql -uroot -e "GRANT USAGE ON *.* TO '${_user}'@'%' IDENTIFIED BY '${_userpass}'"
mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${_userdb}"
mysql -uroot -e "GRANT ALL PRIVILEGES ON ${_userdb}.* TO '${_user}'@'%'"
fi
echo "=> Done!"
echo "========================================================================"
echo "You can now connect to this MySQL Server with $PASS"
echo ""
echo " mysql -uadmin -p$PASS -h<host> -P<port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "MySQL user 'root' has no password but only allows local connections"
echo ""
if [ "$CREATE_MYSQL_USER" = true ]; then
echo "We also created"
echo "A database called '${_userdb}' and"
echo "a user called '${_user}' with password '${_userpass}'"
echo "'${_user}' has full access on '${_userdb}'"
fi
echo "enjoy!"
echo "========================================================================"
mysqladmin -uroot shutdown