mysql - 使用 docker-compose 来创建 MySQL 模式/数据库

如果 mysql 数据库/模式尚不存在,我正在尝试创建它。

这是我尝试过的:

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;

它不起作用。如果架构不存在,使用 docker/docker-compose 创建架构的最佳方式是什么?

最佳答案

我终于找到了解决方案的开始。

MySQL 镜像采用环境变量,即 MYSQL_DATABASE,它在镜像启动时使用数据库名称初始化容器。完整的 documentation 参见此处.

或阅读以下摘录:

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:
  image: mysql:5.6.26
  environment:
   - MYSQL_ROOT_PASSWORD=root
   - MYSQL_DATABASE=bignibou
  ports:
    - "3306:3306"

我现在需要一种方法来指定默认排序规则,但那是另一回事...

编辑:对于那些有兴趣指定与默认排序规则不同的排序规则的人,这里是使用另一个配置文件来覆盖默认排序规则的说明。见下文:

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.

https://stackoverflow.com/questions/32025914/

相关文章:

docker - 在 Dockerbuild 文件中使用 docker-compose 环境变量

docker - 如何将 Capistrano 与 Docker 集成进行部署?

amazon-web-services - 如何将 Kubernetes 日志发送到 AWS Clo

docker - 多个 Docker 容器可以使用相同的主机/端口运行吗?

docker - 如何增加 kubernetes 容器的 shm 大小(--shm-size 相当于

python - 如何使用 pip 安装本地软件包作为 docker 构建的一部分?

docker - 中间容器是如何形成的?

docker - boot2docker 存储的 docker 镜像在哪里?

windows - docker 警告 : failed to get default regist

docker - 如何为 Docker 中的容器分配域名?