forked from simonlesechos/lamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_mysql_admin_user.sh
executable file
·99 lines (71 loc) · 2.72 KB
/
create_mysql_admin_user.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/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
if [ -z "${MYSQL_PASS}" ]
then
PASS="admin"
else
PASS=${MYSQL_PASS}
fi
_word=$( [ ${MYSQL_PASS} ] && echo "preset" || echo "default" )
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"
echo "=> Done!"
echo "========================================================================"
echo "You can now connect to this MySQL Server using:"
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 'admin' has password '$PASS'"
echo "MySQL user 'root' has password 'root' but only allows local connections"
echo "========================================================================"
# Mysql persist with cron
echo "MYSQL_PERSIST_BY_CRON : "
if [ -z "${MYSQL_PERSIST_BY_CRON}" ]
then
echo "No env var MYSQL_PERSIST_BY_CRON in docker run command. Do nothing."
else
if [ "${MYSQL_PERSIST_BY_CRON}" = "yes" ]
then
echo "Env var MYSQL_PERSIST_BY_CRON value is yes. Launch a crontab to backup mysql dir."
# get pass used in docker run or default admin pass
if [ -z "${MYSQL_PASS}" ]
then
PASS="admin"
else
PASS=${MYSQL_PASS}
fi
# restore database if exits file
if [ ! -f /data/MYSQL_PERSIST_BY_CRON_all_dbs.sql ]; then
echo "No file /data/MYSQL_PERSIST_BY_CRON_all_dbs.sql. Use an empty mysql server."
else
echo "File /data/MYSQL_PERSIST_BY_CRON_all_dbs.sql exists. Use it to restore all databases."
mysql -u admin -p$PASS < /data/MYSQL_PERSIST_BY_CRON_all_dbs.sql
fi
# backup all database all minute by cron rask
echo "* * * * * mysqldump -u admin -p$PASS --all-databases > /data/MYSQL_PERSIST_BY_CRON_all_dbs.sql
" >> mycron
crontab mycron
rm mycron
cron # launch cron exec job
else
echo "Env var MYSQL_PERSIST_BY_CRON value must be with yes. Do nothing."
fi
fi
# phpmyadmin configuration
# Change the MySQL root password
mysqladmin -u root password root
# Create the phpmyadmin storage configuration database.
mysql -uroot -proot -e "CREATE DATABASE phpmyadmin; GRANT ALL PRIVILEGES ON phpmyadmin.* TO 'root'@'localhost' IDENTIFIED BY 'root'; FLUSH PRIVILEGES;"
# Import the configuration storage database.
gunzip < /usr/share/doc/phpmyadmin/examples/create_tables.sql.gz | mysql -u root -proot phpmyadmin
# Shutdown the server.
mysqladmin -u root -proot shutdown