Skip to content

Commit 9bdf107

Browse files
authored
Merge pull request #27 from thelamer/master
adding optional ability to setup a database on first spinup
2 parents 2939ff2 + cb88d11 commit 9bdf107

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ docker create \
5555
-e PGID=1000 \
5656
-e MYSQL_ROOT_PASSWORD=<DATABASE PASSWORD> \
5757
-e TZ=Europe/London \
58+
-e MYSQL_DATABASE=<USER DB NAME> `#optional` \
59+
-e MYSQL_USER=<MYSQL USER> `#optional` \
60+
-e MYSQL_PASSWORD=<DATABASE PASSWORD> `#optional` \
5861
-p 3306:3306 \
5962
-v <path to data>:/config \
6063
--restart unless-stopped \
@@ -78,6 +81,9 @@ services:
7881
- PGID=1000
7982
- MYSQL_ROOT_PASSWORD=<DATABASE PASSWORD>
8083
- TZ=Europe/London
84+
- MYSQL_DATABASE=<USER DB NAME> #optional
85+
- MYSQL_USER=<MYSQL USER> #optional
86+
- MYSQL_PASSWORD=<DATABASE PASSWORD> #optional
8187
volumes:
8288
- <path to data>:/config
8389
ports:
@@ -96,6 +102,9 @@ Container images are configured using parameters passed at runtime (such as thos
96102
| `-e PGID=1000` | for GroupID - see below for explanation |
97103
| `-e MYSQL_ROOT_PASSWORD=<DATABASE PASSWORD>` | Set this to root password for installation (minimum 4 characters). |
98104
| `-e TZ=Europe/London` | Specify a timezone to use EG Europe/London. |
105+
| `-e MYSQL_DATABASE=<USER DB NAME>` | Specify the name of a database to be created on image startup. |
106+
| `-e MYSQL_USER=<MYSQL USER>` | This user will have superuser access to the database specified by MYSQL_DATABASE. |
107+
| `-e MYSQL_PASSWORD=<DATABASE PASSWORD>` | Set this to the password you want to use for you MYSQL_USER (minimum 4 characters). |
99108
| `-v /config` | Contains the db itself and all assorted settings. |
100109

101110
## User / Group Identifiers
@@ -121,6 +130,8 @@ to set one at the docker prompt...
121130

122131
NOTE changing the MYSQL_ROOT_PASSWORD variable after the container has set up the initial databases has no effect, use the mysqladmin tool to change your mariadb password.
123132

133+
NOTE if you want to use (MYSQL_DATABASE MYSQL_USER MYSQL_PASSWORD) **all three** of these variables need to be set you cannot pick and choose.
134+
124135
Unraid users, it is advisable to edit the template/webui after setup and remove reference to this variable.
125136

126137
Find custom.cnf in /config for config changes (restart container for them to take effect)
@@ -169,6 +180,7 @@ Below are the instructions for updating containers:
169180

170181
## Versions
171182

183+
* **07.03.19:** - Add ability to setup a database and default user on first spinup.
172184
* **26.01.19:** - Add pipeline logic and multi arch.
173185
* **10.09.18:** - Rebase to ubuntu bionic and use 10.3 mariadb repository.
174186
* **09.12.17:** - Fix continuation lines.

readme-vars.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ param_device_map: false
3535
cap_add_param: false
3636

3737
# optional container parameters
38-
opt_param_usage_include_env: false
38+
opt_param_usage_include_env: true
39+
opt_param_env_vars:
40+
- { env_var: "MYSQL_DATABASE", env_value: "<USER DB NAME>", desc: "Specify the name of a database to be created on image startup." }
41+
- { env_var: "MYSQL_USER", env_value: "<MYSQL USER>", desc: "This user will have superuser access to the database specified by MYSQL_DATABASE." }
42+
- { env_var: "MYSQL_PASSWORD", env_value: "<DATABASE PASSWORD>", desc: "Set this to the password you want to use for you MYSQL_USER (minimum 4 characters)." }
3943
opt_param_usage_include_vols: false
4044
opt_param_usage_include_ports: false
4145
opt_param_device_map: false
@@ -51,13 +55,16 @@ app_setup_block: |
5155
5256
NOTE changing the MYSQL_ROOT_PASSWORD variable after the container has set up the initial databases has no effect, use the mysqladmin tool to change your mariadb password.
5357
58+
NOTE if you want to use (MYSQL_DATABASE MYSQL_USER MYSQL_PASSWORD) **all three** of these variables need to be set you cannot pick and choose.
59+
5460
Unraid users, it is advisable to edit the template/webui after setup and remove reference to this variable.
5561
5662
Find custom.cnf in /config for config changes (restart container for them to take effect)
5763
, the databases in /config/databases and the log in /config/log/myqsl
5864
5965
# changelog
6066
changelogs:
67+
- { date: "07.03.19:", desc: "Add ability to setup a database and default user on first spinup." }
6168
- { date: "26.01.19:", desc: "Add pipeline logic and multi arch." }
6269
- { date: "10.09.18:", desc: "Rebase to ubuntu bionic and use 10.3 mariadb repository." }
6370
- { date: "09.12.17:", desc: "Fix continuation lines." }
File renamed without changes.

root/etc/cont-init.d/40-initialise-db

+13
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,21 @@ else
4444
MYSQL_PASS="CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;"
4545
fi
4646

47+
# Make sure all user and database settings are set and pass is more than 4 characters
48+
if [ "${MYSQL_USER+x}" ] && \
49+
[ "${MYSQL_DATABASE+x}" ] && \
50+
[ "${MYSQL_PASSWORD+x}" ] && \
51+
[ "${#MYSQL_PASSWORD}" -gt "4" ]; then
52+
read -r -d '' MYSQL_DB_SETUP << EOM
53+
CREATE DATABASE ${MYSQL_DATABASE};
54+
CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';
55+
GRANT ALL PRIVILEGES ON ${MYSQL_DATABASE}.* TO '${MYSQL_USER}'@'%';
56+
EOM
57+
fi
58+
4759
# add rest of sql commands based on password set or not
4860
cat >> "$tempSqlFile" <<-EONEWSQL
61+
$MYSQL_DB_SETUP
4962
$MYSQL_PASS
5063
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
5164
DROP DATABASE IF EXISTS test ;

0 commit comments

Comments
 (0)