如何使用Docker compose创建mysql数据库? [英] How can I create a mysql db with Docker compose?
问题描述
我正在尝试在docker上托管一个使用MySQL数据库的应用程序.我正在使用docker compose.我的yml文件看起来像这样:
I'm trying to host on docker an application which uses MySQL db. I'm using docker compose. my yml file looks like this:
version: '2'
volumes:
data_sql:
services:
medical-mysql:
image: mysql
hostname: medical-mysql
volumes:
- data_sql:/dbcreation.sql
environment:
MYSQL_DATABASE: Medical
MYSQL_ROOT_PASSWORD: root
STARTUP_SQL: data_sql
ports:
- "3306:3306"
medical-main:
build: .
ports:
- "8080:8080"
depends_on:
- medical-mysql
我有一个dbcreation.sql
,它创建db模式,并保存在yml文件夹中.当我运行yml文件时,似乎该文件未运行.
I have a dbcreation.sql
which creates the db schema and is kept in the yml folder. When I run the yml file it seems as if the file is not running.
我错过了什么?
推荐答案
只需将您的.sql文件(dbcreation.sql)放在一个文件夹(即/mysql_init)中,然后将该文件夹作为卷添加即可,如下所示:>
Simply just put your .sql file (dbcreation.sql) in a folder (ie. /mysql_init) and add the folder as a volume like this:
volumes:
- /mysql_init:/docker-entrypoint-initdb.d
MySQL映像将在启动时执行/docker-entrypoint-initdb.d中的所有.sql,.sh和.sql.gz文件.
The MySQL image will execute all .sql, .sh and .sql.gz files in /docker-entrypoint-initdb.d on startup.
这篇关于如何使用Docker compose创建mysql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!