简体   繁体   中英

I Want to create a MySql database in a docker container and execute few DDL and DML Statements in it while the container is starting up

This is my docker-compose.yml file

version: "3.5"
services:
  docker-mysql:
    image: mysql:5.7
    container_name: docker-mysql
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=user_management
      - MYSQL_PASSWORD=password

The default database is created "user_management" and now I have files with DDL and DML statements in it that I want to execute ie DDL statement (create a table) and DML statements (Insert few users by default). Not by entering the bash and manually executing the statements but any automated docker way of loading the files that contain DDL and DML statements and execute it.

In mysql dockerhub says, files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d will be executed in the startup of container to the database specified by the MYSQL_DATABASE variable.

version: "3.5"
services:
  docker-mysql:
     image: mysql:5.7
     container_name: docker-mysql
     environment:
       - MYSQL_ROOT_PASSWORD=password
       - MYSQL_DATABASE=user_management
       - MYSQL_PASSWORD=password
     volumes:
       - /yourscript:/docker-entrypoint-initdb.d

so as above, you can mount volume and add your DDL and DML scripts in that location. Then mysql would execute them on startup. /yourscript is the location of your scripts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM