# Multiple Databases in Official MySql Container

**URL:** <https://forums.docker.com/t/multiple-databases-in-official-mysql-container/8324>\
**Category:** General\
**Created:** [April 4, 2016, 2:35pm UTC](https://forums.docker.com/t/multiple-databases-in-official-mysql-container/8324 "2016-04-04T14:35:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthikv](https://avatars.discourse-cdn.com/v4/letter/k/e9a140/32.png) [@karthikv](https://forums.docker.com/u/karthikv)\
**Post date:** [April 4, 2016, 2:35pm UTC](https://forums.docker.com/t/multiple-databases-in-official-mysql-container/8324/1 "2016-04-04T14:35:24Z")

</div>

Hi,  
I’m able a create a new single database with Docker run command  
$ docker run --name some-mysql -e MYSQL\_ROOT\_PASSWORD=my-secret-pw -d mysql:tag

Then I bash into the mysql container with exec and create another database in their at the mysql\> prompt. Now how do I do that with the docker run command itself.

Cheers!!!  
K

---

<div class="post-metadata">

**Author:** ![programmerq](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/programmerq/32/208_2.png) [@programmerq](https://forums.docker.com/u/programmerq)\
**Post date:** [April 5, 2016, 8:40pm UTC](https://forums.docker.com/t/multiple-databases-in-official-mysql-container/8324/2 "2016-04-05T20:40:34Z")

</div>

The official mysql image has some features in its entrypoint script to help you initialize your database. Check out [https://hub.docker.com/\_/mysql/](https://hub.docker.com/_/mysql/) and scroll down to “Initializing a fresh instance”. Basically you can drop in some .sql or .sh scripts into a certain location that can import whatever you want into your database.

Cheers!

---

<div class="post-metadata">

**Author:** ![yuklia](https://avatars.discourse-cdn.com/v4/letter/y/e9c0ed/32.png) [@yuklia](https://forums.docker.com/u/yuklia)\
**Post date:** [October 13, 2017, 7:57am UTC](https://forums.docker.com/t/multiple-databases-in-official-mysql-container/8324/3 "2017-10-13T07:57:30Z")

</div>

@programmerq It is wrong suggestion about creating another one Database through /docker-entrypoint-initdb.d  
just because mouthed sql files will be imported by default to the database specified by the MYSQL\_DATABASE variable. Since you can’t create DB in DB 🙂

---

<div class="post-metadata">

**Author:** ![abagayev](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/abagayev/32/26172_2.png) [@abagayev](https://forums.docker.com/u/abagayev)\
**Post date:** [June 8, 2018, 10:11pm UTC](https://forums.docker.com/t/multiple-databases-in-official-mysql-container/8324/4 "2018-06-08T22:11:45Z")

</div>

Created clear example of docker-compose with multiple databases, just use for your purposes:

> **[abagayev/docker-bootstrap-collection](https://github.com/abagayev/docker-bootstrap-collection/tree/master/mysql-few-databases)**
>
> docker-bootstrap-collection - Docker bootstrap templates to start new applications faster

---

<div class="post-metadata">

**Author:** ![mkagesawa](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mkagesawa](https://forums.docker.com/u/mkagesawa)\
**Post date:** [August 21, 2018, 8:14am UTC](https://forums.docker.com/t/multiple-databases-in-official-mysql-container/8324/5 "2018-08-21T08:14:31Z")

</div>

Here is an example: [https://gist.github.com/MKagesawa/a03892b8c44c015cd991c2c5311f1768](https://gist.github.com/MKagesawa/a03892b8c44c015cd991c2c5311f1768)  
You can pass a shell script creating the dbs

```auto
# 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 

```
