使用 docker-compose 创建 MySQL 模式/数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32025914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using docker-compose in order to create a MySQL schema/database
提问by balteo
I am trying to create a mysql database/schema if it doesn't already exist.
如果它不存在,我正在尝试创建一个 mysql 数据库/模式。
Here is what I have tried:
这是我尝试过的:
docker-compose.yml
docker-compose.yml
mysql:
image: mysql:5.6.26
environment:
- MYSQL_ROOT_PASSWORD=root
command: "mysql -uroot -proot < createDB.sql"
ports:
- "3306:3306"
createDB.sql
创建数据库
CREATE DATABASE IF NOT EXISTS bignibou;
It does not work. What would be the best way to use docker/docker-compose in order to create a schema if it does not exist?
这是行不通的。如果模式不存在,使用 docker/docker-compose 来创建模式的最佳方法是什么?
回答by balteo
I finally found the beginning of a solution.
我终于找到了解决方案的开始。
The MySQL image takes an environment variable i.e. MYSQL_DATABASEthat initialize the container with the name of the database on image startup See here for full documentation.
MySQL 映像采用一个环境变量,即MYSQL_DATABASE,该变量在映像启动时使用数据库名称初始化容器,请参阅此处获取完整文档。
Or read the excerpt below:
或阅读以下摘录:
MYSQL_DATABASE
This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.
MYSQL_DATABASE
此变量是可选的,允许您指定要在映像启动时创建的数据库的名称。如果提供了用户/密码(见下文),那么该用户将被授予对该数据库的超级用户访问权限(对应于 GRANT ALL)。
Here is what I came up with:
这是我想出的:
mysql:
image: mysql:5.6.26
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=bignibou
ports:
- "3306:3306"
I now need a way to specify the default collation but that is another story...
我现在需要一种方法来指定默认排序规则,但那是另一回事了...
edit: For those interested in specifying a different collation from the default, here are the instructions to use another config file that will override the default one. See below:
编辑:对于那些有兴趣指定与默认排序规则不同的人,这里是使用另一个配置文件的说明,该文件将覆盖默认设置。见下文:
Using a custom MySQL configuration file The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.
If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.
使用自定义 MySQL 配置文件 MySQL 启动配置在文件 /etc/mysql/my.cnf 中指定,该文件又包含在 /etc/mysql/conf.d 目录中以 .cnf 结尾的所有文件。此目录中文件中的设置将增加和/或覆盖 /etc/mysql/my.cnf 中的设置。如果要使用自定义 MySQL 配置,可以在主机上的目录中创建备用配置文件,然后将该目录位置挂载为 mysql 容器内的 /etc/mysql/conf.d。
如果/my/custom/config-file.cnf是你的自定义配置文件的路径和名称,你可以这样启动你的mysql容器(注意这个命令中只使用了自定义配置文件的目录路径):
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag 这将启动一个新的容器 some-mysql 其中 MySQL instance 使用来自 /etc/mysql/my.cnf 和 /etc/mysql/conf.d/config-file.cnf 的组合启动设置,后者的设置优先。
回答by Codenator81
To not lost your data better use volumes as well:
为了不丢失您的数据,最好也使用卷:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- mysql-db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: my_db_name
ports:
- "3307:3306"
volumes:
mysql-db:
回答by mohamnag
probably what you are trying to do needs an additional script. So if building an image instead of directly using a prebuilt image is an option for you, you need to use a Dockerfile and use a script file which first imports the script in MySql and then runs the service itself.
可能您正在尝试做的事情需要一个额外的脚本。因此,如果您可以选择构建镜像而不是直接使用预构建的镜像,那么您需要使用 Dockerfile 并使用脚本文件,该文件首先将脚本导入 MySql 中,然后运行服务本身。
take a look at this answer: Docker - Initialize mysql database with schema
回答by Grasshopper
From the docker-compose documentation - see Define Services- you can tell which Dockerfile it will use to build the image. Therefore you can create a Dockerfile based on the mysql image and create the database inside it using standard Dockerfile commands.
从 docker-compose 文档 -请参阅定义服务- 您可以知道它将使用哪个 Dockerfile 来构建映像。因此,您可以基于 mysql 映像创建 Dockerfile,并使用标准 Dockerfile 命令在其中创建数据库。
回答by Dean Sha
This might be useful in case someone lands here in future. The real issue appears to be the "command" statement in the docker-compose file. Once the command finishes successfully the container will get destroyed. This sql script must be run only after docker-compose has run and containers have been created. docker-compose "command" is really to start a service in the container. In this case you overrode the mysql service with your command.
如果将来有人登陆这里,这可能会很有用。真正的问题似乎是 docker-compose 文件中的“命令”语句。一旦命令成功完成,容器将被销毁。这个 sql 脚本必须在 docker-compose 运行并创建容器后运行。docker-compose“命令”其实就是在容器中启动一个服务。在这种情况下,您使用命令覆盖了 mysql 服务。