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