使用数据库和表对MySQL进行Dockerize [英] Dockerize MySQL with database and tables

查看:94
本文介绍了使用数据库和表对MySQL进行Dockerize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Dockerfile创建一个具有初始数据库及其表的MySQL容器。

I want to create a MySQL container with an initial database and its tables, from a Dockerfile.

我也有一个特定的 / etc /mysql/mysql.conf.d/mysqld.cnf 文件(我可以在Dockerfile中简单地使用 ADD 指令。)

I also have a specific /etc/mysql/mysql.conf.d/mysqld.cnf file (I can simply use ADD directive in Dockerfile.)

我希望我的Dockerfile用一个名为DBNAME的数据库和以下表格创建一个MySQL映像:

I want my Dockerfile to create a MySQL image with a database named, for example, DBNAME and with the following tables:

CREATE TABLE utente (
     ID int  NOT NULL AUTO_INCREMENT,
     nome VARCHAR(255),
     cognome VARCHAR(255),
     username VARCHAR(255),
     password VARCHAR(255),
     PRIMARY KEY (ID)
);

CREATE TABLE sdplines (
    ID int NOT NULL AUTO_INCREMENT,
    value VARCHAR(255),
    session_id VARCHAR(255),
    u_id int,
    PRIMARY KEY (ID),
    FOREIGN KEY (u_id) REFERENCES utente(ID)
);

我已经在这里搜索解决了我的问题,但没有找到解决方法。

I've already searched to solve my problem here but I've found no solution.

推荐答案

如果您使用的是dockerfile,则添加类似内容

If your using a dockerfile - then add something like

ADD schema.sql /docker-entrypoint-initdb.d

或者使用docker-compse

Or if your using docker-compse

volumes:
 - ./SQL:/docker-entrypoint-initdb.d

在我的情况下,最后一个目录中有一个名为SQL的目录,其中包含了脚本。

In my case the last one has a directory called SQL with the scripts in it.

这篇关于使用数据库和表对MySQL进行Dockerize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆