Multiple Databases in Official MySql Container

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.


The official mysql image has some features in its entrypoint script to help you initialize your database. Check out 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.


1 Like

@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 :slight_smile:

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

Here is an example:
You can pass a shell script creating the dbs

# The official 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'

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