-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-docker-entrypoint.sh
executable file
·39 lines (30 loc) · 1.13 KB
/
multi-docker-entrypoint.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
#!/bin/bash
function waitForConnection() {
{
echo "SELECT user FROM mysql.user" | mysql -h localhost -u root -p"${MARIADB_ROOT_PASSWORD}" > /dev/null 2>&1
} || {
sleep 1
waitForConnection
}
}
function initializeMultiDatabases() {
echo "Waiting connection..."
waitForConnection || exit 1
echo "Connection established!"
echo "Initializing multi databases..."
for ((i = 1; ; i++)); do
db_name="$(eval echo "\$MARIADB_${i}_NAME")"
db_user="$(eval echo "\$MARIADB_${i}_USER")"
db_pwd="$(eval echo "\$MARIADB_${i}_PASSWORD")"
if [ "${db_name}" = '' ] || [ "${db_user}" = '' ]; then
break
fi
echo "CREATE DATABASE IF NOT EXISTS ${db_name};" | mysql -h localhost -u root -p"${MARIADB_ROOT_PASSWORD}"
echo "CREATE USER IF NOT EXISTS '${db_user}' IDENTIFIED BY '${db_pwd}';" | mysql -h localhost -u root -p"${MARIADB_ROOT_PASSWORD}"
echo "GRANT ALL PRIVILEGES ON ${db_name}.* TO '${db_user}';" | mysql -h localhost -u root -p"${MARIADB_ROOT_PASSWORD}"
echo "Database ${db_name} initialized!"
done
echo "Multi databases initialized!"
}
initializeMultiDatabases &
docker-entrypoint.sh "$@"