Skip to content

Instantly share code, notes, and snippets.

@netpedro-com
Forked from stefanotorresi/databases.sh
Last active February 1, 2018 07:45
Show Gist options
  • Save netpedro-com/4e48aecd2284d8dba0f6355cf2ba548f to your computer and use it in GitHub Desktop.
Save netpedro-com/4e48aecd2284d8dba0f6355cf2ba548f 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment