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

标签 mysql docker docker-compose

如果 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.

关于mysql - 使用 docker-compose 来创建 MySQL 模式/数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025914/

相关文章:

nginx - 用于 Docker Flask 应用程序的 Nginx 反向代理的 502 错误网关

docker compose 网络别名不起作用

Dockerfile 复制不适用于 Nextcloud 容器

php - laravel 中区分大小写的 where 语句

mysql - drupal 查询按匹配结果中关键字出现次数排序

linux - qemu 臂 qemu : uncaught target signal 11 (Segmentation fault) - core dumped

docker - Kubernetes deployment.yaml for django+gunicorn+nginx

mysql - 如何使用触发器更新表中的单行或单列

MySQL FULLTEXT 搜索出现次数计数

sockets - 有没有直接的方法从 Go 中的 unix 套接字获取 html 响应(就像 curl 一样)?