Created
March 15, 2020 21:01
-
-
Save ibeucaly/d50805f3e159acd6c1adcc51927ca3ae to your computer and use it in GitHub Desktop.
creates multiple databases on MySQL Docker by using an script
This file contains hidden or 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
-- This file is placed at ./initdb/sql/db1.sql | |
-- SQL for initializing db1 | |
CREATE TABLE table1 (id int NOT NULL PRIMARY KEY, name varchar(50) NOT NULL); |
This file contains hidden or 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
-- This file is placed at ./initdb/sql/db2.sql | |
-- SQL for initializing db2 | |
CREATE TABLE table2 (id int NOT NULL PRIMARY KEY, name varchar(50) NOT NULL); |
This file contains hidden or 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
version: "3" | |
services: | |
mysql: | |
image: mysql:5.7 | |
ports: | |
- "3306:3306" | |
environment: | |
MYSQL_RANDOM_ROOT_PASSWORD: 1 | |
MYSQL_USER: your_user | |
MYSQL_PASSWORD: your_password | |
volumes: | |
- ./initdb:/docker-entrypoint-initdb.d |
This file contains hidden or 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
#!/bin/bash | |
# This file is placed at ./initdb/init.sh | |
docker_process_sql () { | |
mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" "$@" | |
} | |
docker_process_sql << EOS | |
CREATE DATABASE db1; | |
CREATE DATABASE db2; | |
GRANT ALL ON db1.* TO '$MYSQL_USER'@'%'; | |
GRANT ALL ON db2.* TO '$MYSQL_USER'@'%'; | |
FLUSH PRIVILEGES; | |
EOS | |
docker_process_sql db1 < /docker-entrypoint-initdb.d/sql/db1.sql | |
docker_process_sql db2 < /docker-entrypoint-initdb.d/sql/db2.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment