简体   繁体   English

我想在Docker容器中创建MySql数据库并在容器启动时在其中执行一些DDL和DML语句

[英]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 这是我docker-compose.yml文件

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). 默认数据库创建为“ user_management” ,现在我要执行的文件中包含DDL和DML语句,即DDL语句(创建表)和DML语句(默认情况下插入几个用户)。 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. 不是通过输入bash并手动执行语句,而是通过任何自动docker方式加载包含DDL和DML语句的文件并执行它。

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. 在mysql dockerhub中说,在/docker-entrypoint-initdb.d中找到的扩展名为.sh,.sql和.sql.gz的文件将在容器启动时执行到MYSQL_DATABASE变量指定的数据库。

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. 因此,如上所述,您可以装载卷并在该位置添加DDL和DML脚本。 Then mysql would execute them on startup. 然后mysql将在启动时执行它们。 /yourscript is the location of your scripts /yourscript是脚本的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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