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

Support multiple databases creation #15

Closed
visay opened this Issue Jun 30, 2015 · 17 comments

Comments

Projects
None yet
@visay
Copy link

visay commented Jun 30, 2015

Currently I only have the option to create only one database in my docker-compose.yml

db:
  image: mariadb:10.0
  environment:
    MYSQL_DATABASE: mydbname

It would be good if multiple databases is supported (something similar to this merge request in mysql https://github.com/docker-library/mysql/pull/18/files), so I can write:

db:
  image: mariadb:10.0
  environment:
    MYSQL_DATABASES:
      - mydbname1
      - mydbname2
@yosifkit

This comment has been minimized.

Copy link
Member

yosifkit commented Jun 30, 2015

Although your yaml syntax would not work, there was discussion of letting MYSQL_DATABASE be a space separated string of databases: docker-library/mysql#18.

@bennythomps

This comment has been minimized.

Copy link

bennythomps commented Apr 2, 2016

Keep in mind @visay, best practice would be to have a separate container for each application if that's what you're trying to do. Maybe your one application for some reason needs more than one database, though.

@Crafter6432

This comment has been minimized.

Copy link

Crafter6432 commented Jun 10, 2016

+1

1 similar comment
@Vingtoft

This comment has been minimized.

Copy link

Vingtoft commented Jun 14, 2016

+1

@madalinignisca

This comment has been minimized.

Copy link

madalinignisca commented Jun 29, 2016

-1

the idea for docker is minimalism. one database per container. you can start multiple containers with different databases.

@robbinalexander

This comment has been minimized.

Copy link

robbinalexander commented Jul 17, 2016

@madalinignisca Probably ok during development. What about the additional resource required to run another container? And additional configurations needed, especially when I need a mysql cluster?

@madalinignisca

This comment has been minimized.

Copy link

madalinignisca commented Jul 22, 2016

You can make a simple Dockerfile using this one as the image source and just add your own extra env variables to add more databases to your service. Easy as that. The base one isn't supposed to cover all possible scenarios we think about, but to offer the foundation on which we build the solutions to our problems.

Building it it might take an extra second or less ;)

@lecaoquochung lecaoquochung referenced this issue Oct 2, 2016

Open

PDCA 2016 #1

5 of 8 tasks complete
@amir20

This comment has been minimized.

Copy link

amir20 commented Oct 5, 2016

Reading the README file suggests

Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d.

So adding your own .sql file should be able to create multiple databases.

@alexanikanov

This comment has been minimized.

Copy link

alexanikanov commented Nov 10, 2016

+1

@qmcree

This comment has been minimized.

Copy link

qmcree commented Feb 3, 2017

Thanks, @amir20, that did it -- total duh moment LOL

@visay visay closed this Feb 3, 2017

@armansd

This comment has been minimized.

Copy link

armansd commented Mar 16, 2017

Just update startup file under mysql directory:

DROP USER IF EXISTS 'MYSQL_USER';
CREATE USER 'MYSQL_USER'@'%';
CREATE DATABASE IF NOT EXISTS MYSQL_DATABASE;
GRANT ALL ON MYSQL_DATABASE.* TO 'MYSQL_USER'@'%' IDENTIFIED BY 'MYSQL_PASSWORD';
--------------your new testDB----------------------
CREATE DATABASE IF NOT EXISTS testDB;
GRANT ALL ON testDB.* TO 'MYSQL_USER'@'%' IDENTIFIED BY 'MYSQL_PASSWORD';

@sirgalleto

This comment has been minimized.

Copy link

sirgalleto commented Mar 30, 2017

What's the status of this issue? why is closed?

@visay

This comment has been minimized.

Copy link

visay commented Mar 31, 2017

@sirgalleto because you can already do it with our own script if they are mounted into /docker-entrypoint-initdb.d.

See the description at https://hub.docker.com/_/mariadb/ section "Initializing a fresh instance"

@Denis4yk

This comment has been minimized.

Copy link

Denis4yk commented Dec 14, 2017

To my point of view it could be quite useful and should be reopened

@abagayev

This comment has been minimized.

Copy link

abagayev commented Jun 8, 2018

Created clear example of docker-compose with multiple databases, just use for your purposes:
https://github.com/abagayev/docker-bootstrap-collection/tree/master/mysql-few-databases

@MKagesawa

This comment has been minimized.

Copy link

MKagesawa commented Aug 21, 2018

Take a look here: https://gist.github.com/MKagesawa/a03892b8c44c015cd991c2c5311f1768
You can pass a shell script creating the dbs

# The official MySQL (https://hub.docker.com/_/mysql/) supports only one MYSQL_DATABASE environment variable.
# By modifying the entrypoint and passing shell script, you can create multiple dbs without having to make a mysql image just for this purpose.
  
version: '3'

services:
  # Some other service connecting to mysql
  
  db:
    image: mysql:5.6
    environment:
      - MYSQL_USER=root
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    entrypoint:
      sh -c "
        echo 'CREATE DATABASE IF NOT EXISTS firstDB; CREATE DATABASE IF NOT EXISTS secondDB;' > /docker-entrypoint-initdb.d/init.sql;
        /usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
      "
    ports:
      - 3306:3306 
@AndresPineros

This comment has been minimized.

Copy link

AndresPineros commented Sep 2, 2018

@MKagesawa Thanks!!!! Although this alternative is a little hacky. You could use a config file for Swarm or a ConfigMap for Kubernetes in order to inject the init.sql

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