Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create more than one database when init container #287

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions root-common/usr/share/container-scripts/mysql/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export MYSQL_DATADIR_FIRST_INIT=false
# Be paranoid and stricter than we should be.
# https://dev.mysql.com/doc/refman/en/identifiers.html
mysql_identifier_regex='^[a-zA-Z0-9_]+$'
mysql_dbname_identifier_regex='^[a-zA-Z0-9_,]+$'
mysql_password_regex='^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$'

# Variables that are used to connect to local mysql during initialization
Expand Down Expand Up @@ -169,15 +170,18 @@ EOSQL
fi

if [ -v MYSQL_DATABASE ]; then
log_info "Creating database ${MYSQL_DATABASE} ..."
mysqladmin $admin_flags create "${MYSQL_DATABASE}"
# split MYSQL_DATABASE by ,
for item in $(echo "${MYSQL_DATABASE}" | tr "," "\n"); do
log_info "Creating database ${item} ..."
mysqladmin $admin_flags create "${item}"
if [ -v MYSQL_USER ]; then
log_info "Granting privileges to user ${MYSQL_USER} for ${MYSQL_DATABASE} ..."
log_info "Granting privileges to user ${MYSQL_USER} for ${item} ..."
mysql $mysql_flags <<EOSQL
GRANT ALL ON \`${MYSQL_DATABASE}\`.* TO '${MYSQL_USER}'@'%' ;
GRANT ALL ON \`${item}\`.* TO '${MYSQL_USER}'@'%' ;
FLUSH PRIVILEGES ;
EOSQL
fi
done
fi

log_info 'Initialization finished'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function usage() {
echo "You must either specify the following environment variables:"
echo " MYSQL_USER (regex: '$mysql_identifier_regex')"
echo " MYSQL_PASSWORD (regex: '$mysql_password_regex')"
echo " MYSQL_DATABASE (regex: '$mysql_identifier_regex')"
echo " MYSQL_DATABASE (regex: '$mysql_dbname_identifier_regex')"
echo "Or the following environment variable:"
echo " MYSQL_ROOT_PASSWORD (regex: '$mysql_password_regex')"
echo "Or both."
Expand Down Expand Up @@ -65,7 +65,7 @@ function validate_variables() {
fi

if [ -v MYSQL_DATABASE ]; then
[[ "$MYSQL_DATABASE" =~ $mysql_identifier_regex ]] || usage "Invalid database name"
[[ "$MYSQL_DATABASE" =~ $mysql_dbname_identifier_regex ]] || usage "Invalid database name"
[ ${#MYSQL_DATABASE} -le 64 ] || usage "Database name too long (maximum 64 characters)"
fi

Expand Down