Skip to content

Instantly share code, notes, and snippets.

@stefanotorresi
Last active February 1, 2018 07:52
Show Gist options
  • Save stefanotorresi/a039768c6b44cb4f4eff27cb8a021799 to your computer and use it in GitHub Desktop.
Save stefanotorresi/a039768c6b44cb4f4eff27cb8a021799 to your computer and use it in GitHub Desktop.
Initialising multiple databases inside a Docker MySQL image
#!/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
@netpedro-com
Copy link

netpedro-com commented Feb 1, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment