Last active
February 1, 2018 07:52
-
-
Save stefanotorresi/a039768c6b44cb4f4eff27cb8a021799 to your computer and use it in GitHub Desktop.
Initialising multiple databases inside a Docker MySQL image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ** usage ** | |
# initialize a MYSQL_DATABASES env variable in the container with space separated db names | |
# e.g. MYSQL_DATABASES="foo bar" | |
# assumes that MYSQL_ROOT_PASSWORD and MYSQL_USER are set | |
function create_database() { | |
local DATABASE=$1 | |
echo "Creating database '${DATABASE}'" | |
mysql -uroot -p${MYSQL_ROOT_PASSWORD} <<-EOSQL | |
CREATE DATABASE ${DATABASE}; | |
GRANT ALL ON \`${DATABASE}\`.* TO '${MYSQL_USER}'@'%'; | |
EOSQL | |
} | |
for DATABASE in ${MYSQL_DATABASES[*]}; do | |
create_database $DATABASE | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just forked your code. I made a little change in
CREATE DATABASE \`${DATABASE}\`;
to allow dashed names (e.g.foo-dev
). Thanks for this code snippet.