1
1
#!/usr/bin/with-contenv bash
2
+ # shellcheck shell=bash
2
3
3
4
# set start function that creates user and password, used later
4
- start_mysql(){
5
+ start_mysql() {
5
6
mysqld --datadir="${DATADIR}" --init-file="${tempSqlFile}" --user=abc &
6
7
pid="$!"
7
8
RET=1
8
9
while [[ RET -ne 0 ]]; do
9
- mysql -uroot -e "status" > /dev/null 2>&1
10
+ mysql -uroot -e "status" >/dev/null 2>&1
10
11
RET=$?
11
12
sleep 1
12
13
done
@@ -16,20 +17,20 @@ start_mysql(){
16
17
# BEGIN: No indentation due to heredocs
17
18
if [[ ! -d "${DATADIR}/mysql" ]]; then
18
19
19
- # load env file if it exists
20
- if [[ -f "/config/env" ]]; then
21
- source /config/env
22
- fi
20
+ # load env file if it exists
21
+ if [[ -f "/config/env" ]]; then
22
+ source /config/env
23
+ fi
23
24
24
- # set basic sql command
25
- tempSqlFile=$(mktemp)
26
- cat > "${tempSqlFile}" <<-EOSQL
25
+ # set basic sql command
26
+ tempSqlFile=$(mktemp)
27
+ cat >"${tempSqlFile}" <<-EOSQL
27
28
DELETE FROM mysql.user WHERE user <> 'mariadb.sys';
28
29
EOSQL
29
30
30
- # set what to display if no password set with variable MYSQL_ROOT_PASSWORD
31
- NOPASS_SET=$(mktemp)
32
- cat > "${NOPASS_SET}" <<-EOFPASS
31
+ # set what to display if no password set with variable MYSQL_ROOT_PASSWORD
32
+ NOPASS_SET=$(mktemp)
33
+ cat >"${NOPASS_SET}" <<-EOFPASS
33
34
#################################################################
34
35
# No root password or too short a password, min of 4 characters #
35
36
# No root password will be set, this is not a good thing #
@@ -38,84 +39,83 @@ cat > "${NOPASS_SET}" <<-EOFPASS
38
39
#################################################################
39
40
EOFPASS
40
41
41
- # test for empty password variable, if it's set to 0 or less than 4 characters
42
- if [[ -z "${MYSQL_ROOT_PASSWORD}" ]]; then
43
- TEST_LEN="0"
44
- else
45
- TEST_LEN=${#MYSQL_ROOT_PASSWORD}
46
- fi
47
-
48
- if [[ "${TEST_LEN}" -lt "4" ]]; then
49
- MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '' ;"
50
- else
51
- MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;"
52
- fi
53
-
54
- # Make sure all user and database settings are set and pass is more than 4 characters
55
- # At the end change to default database created with environment variables to run init and remote scripts there
56
- if [[ "${MYSQL_USER+x}" ]] && \
57
- [[ "${MYSQL_DATABASE+x}" ]] && \
58
- [[ "${MYSQL_PASSWORD+x}" ]] && \
59
- [[ "${#MYSQL_PASSWORD}" -gt "3" ]]; then
60
- read -r -d '' MYSQL_DB_SETUP << EOM
42
+ # test for empty password variable, if it's set to 0 or less than 4 characters
43
+ if [[ -z "${MYSQL_ROOT_PASSWORD}" ]]; then
44
+ TEST_LEN="0"
45
+ else
46
+ TEST_LEN=${#MYSQL_ROOT_PASSWORD}
47
+ fi
48
+
49
+ if [[ "${TEST_LEN}" -lt "4" ]]; then
50
+ MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '' ;"
51
+ else
52
+ MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;"
53
+ fi
54
+
55
+ # Make sure all user and database settings are set and pass is more than 4 characters
56
+ # At the end change to default database created with environment variables to run init and remote scripts there
57
+ if [[ "${MYSQL_USER+x}" ]] &&
58
+ [[ "${MYSQL_DATABASE+x}" ]] &&
59
+ [[ "${MYSQL_PASSWORD+x}" ]] &&
60
+ [[ "${#MYSQL_PASSWORD}" -gt "3" ]]; then
61
+ read -r -d '' MYSQL_DB_SETUP <<EOM
61
62
CREATE DATABASE \`${MYSQL_DATABASE}\`;
62
63
CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';
63
64
GRANT ALL PRIVILEGES ON \`${MYSQL_DATABASE}\`.* TO '${MYSQL_USER}'@'%';
64
65
USE \`${MYSQL_DATABASE}\`;
65
66
EOM
66
- fi
67
+ fi
67
68
68
- # add rest of sql commands based on password set or not
69
- cat >> "${tempSqlFile}" <<-EONEWSQL
69
+ # add rest of sql commands based on password set or not
70
+ cat >>"${tempSqlFile}" <<-EONEWSQL
70
71
$MYSQL_PASS
71
72
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
72
73
DROP DATABASE IF EXISTS test ;
73
74
$MYSQL_DB_SETUP
74
75
EONEWSQL
75
76
76
- echo "Setting Up Initial Databases"
77
-
78
- # add all sql from a user defined directory on first init
79
- if [[ -e "/config/initdb.d" ]] && [[ -n "$(/bin/ls -A /config/initdb.d/*.sql 2>/dev/null)" ]]; then
80
- cat /config/initdb.d/*.sql >> "${tempSqlFile}"
81
- fi
82
-
83
- chown -R abc:abc "${tempSqlFile}"
84
-
85
-
86
- # ingest remote sql if REMOTE_SQL is set
87
- if [[ -n "${REMOTE_SQL+set}" ]]; then
88
- IFS=, read -ra URLS <<< "${REMOTE_SQL}"
89
- for URL in "${URLS[@]}"; do
90
- if [[ "$(curl -I -sL -w "%{http_code}" "${URL}" -o /dev/null)" == 200 ]]; then
91
- curl -sL "${URL}" >> "${tempSqlFile}"
92
- fi
93
- done
94
- fi
95
- # set some permissions needed before we begin initialising
96
- chown -R abc:abc /config/log/mysql /var/run/mysqld /var/lib/mysql
97
- chmod -R 777 /config/log/mysql /var/run/mysqld /var/lib/mysql
98
-
99
- # initialise database structure
100
- mysql_install_db --datadir="${DATADIR}" --user=abc --auth-root-authentication-method=normal
101
-
102
- # start mysql and apply our sql commands we set above
103
- start_mysql
104
-
105
- # shut down after apply sql commands, waiting for pid to stop
106
- mysqladmin -u root shutdown
107
- wait "${pid}"
108
- echo "Database Setup Completed"
109
-
110
- # display a message about password if not set or too short
111
- if [[ "${TEST_LEN}" -lt "4" ]]; then
112
- printf '\n\n\n%s\n\n\n' "$(<"${NOPASS_SET}")"
113
- sleep 5s
114
- fi
115
-
116
- # clean up any old install files from /tmp
117
- rm -f "${NOPASS_SET}"
118
- rm -f "${tempSqlFile}"
77
+ echo "Setting Up Initial Databases"
78
+
79
+ # add all sql from a user defined directory on first init
80
+ if [[ -e "/config/initdb.d" ]] && [[ -n "$(/bin/ls -A /config/initdb.d/*.sql 2>/dev/null)" ]]; then
81
+ cat /config/initdb.d/*.sql >>"${tempSqlFile}"
82
+ fi
83
+
84
+ chown -R abc:abc "${tempSqlFile}"
85
+
86
+ # ingest remote sql if REMOTE_SQL is set
87
+ if [[ -n "${REMOTE_SQL+set}" ]]; then
88
+ IFS=, read -ra URLS <<<"${REMOTE_SQL}"
89
+ for URL in "${URLS[@]}"; do
90
+ if [[ "$(curl -I -sL -w "%{http_code}" "${URL}" -o /dev/null)" == 200 ]]; then
91
+ curl -sL "${URL}" >>"${tempSqlFile}"
92
+ fi
93
+ done
94
+ fi
95
+ # set some permissions needed before we begin initialising
96
+ chown -R abc:abc /config/log/mysql /var/run/mysqld /var/lib/mysql
97
+ chmod -R 777 /config/log/mysql /var/run/mysqld /var/lib/mysql
98
+
99
+ # initialise database structure
100
+ mysql_install_db --datadir="${DATADIR}" --user=abc --auth-root-authentication-method=normal
101
+
102
+ # start mysql and apply our sql commands we set above
103
+ start_mysql
104
+
105
+ # shut down after apply sql commands, waiting for pid to stop
106
+ mysqladmin -u root shutdown
107
+ wait "${pid}"
108
+ echo "Database Setup Completed"
109
+
110
+ # display a message about password if not set or too short
111
+ if [[ "${TEST_LEN}" -lt "4" ]]; then
112
+ printf '\n\n\n%s\n\n\n' "$(<"${NOPASS_SET}")"
113
+ sleep 5s
114
+ fi
115
+
116
+ # clean up any old install files from /tmp
117
+ rm -f "${NOPASS_SET}"
118
+ rm -f "${tempSqlFile}"
119
119
120
120
# END: No indentation due to heredocs
121
121
fi
0 commit comments