11#!/usr/bin/with-contenv bash
2+ # shellcheck shell=bash
23
34# set start function that creates user and password, used later
4- start_mysql(){
5+ start_mysql() {
56 mysqld --datadir="${DATADIR}" --init-file="${tempSqlFile}" --user=abc &
67 pid="$!"
78 RET=1
89 while [[ RET -ne 0 ]]; do
9- mysql -uroot -e "status" > /dev/null 2>&1
10+ mysql -uroot -e "status" >/dev/null 2>&1
1011 RET=$?
1112 sleep 1
1213 done
@@ -16,20 +17,20 @@ start_mysql(){
1617# BEGIN: No indentation due to heredocs
1718if [[ ! -d "${DATADIR}/mysql" ]]; then
1819
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
2324
24- # set basic sql command
25- tempSqlFile=$(mktemp)
26- cat > "${tempSqlFile}" <<-EOSQL
25+ # set basic sql command
26+ tempSqlFile=$(mktemp)
27+ cat >"${tempSqlFile}" <<-EOSQL
2728DELETE FROM mysql.user WHERE user <> 'mariadb.sys';
2829EOSQL
2930
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
3334#################################################################
3435# No root password or too short a password, min of 4 characters #
3536# No root password will be set, this is not a good thing #
@@ -38,84 +39,83 @@ cat > "${NOPASS_SET}" <<-EOFPASS
3839#################################################################
3940EOFPASS
4041
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
6162CREATE DATABASE \`${MYSQL_DATABASE}\`;
6263CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';
6364GRANT ALL PRIVILEGES ON \`${MYSQL_DATABASE}\`.* TO '${MYSQL_USER}'@'%';
6465USE \`${MYSQL_DATABASE}\`;
6566EOM
66- fi
67+ fi
6768
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
7071$MYSQL_PASS
7172GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
7273DROP DATABASE IF EXISTS test ;
7374$MYSQL_DB_SETUP
7475EONEWSQL
7576
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}"
119119
120120# END: No indentation due to heredocs
121121fi
0 commit comments