如何在mysql服务器8 docker-compose文件中使用custom.cnf?

man*_*i77 4 mysql docker docker-compose mysql-8.0

尝试在 docker-compose mysql 8 中使用自定义配置文件。 docker-compose.yml 看起来像

version: '3'
services:
  db:
    container_name: test-mysql8
    image: mysql/mysql-server:8.0
    volumes:
      - <host path to>/mycustom.cnf:/etc/mysql/conf.d/custom.cnf
    environment:
      - MYSQL_ROOT_PASSWORD=pass
      - MYSQL_DATABASE=testdb
      - MYSQL_USER=testuser
      - MYSQL_PASSWORD=testpassword
    ports:
      - "3306:3306"
    restart: always

  adminer:
    image: adminer:latest
    restart: always
    ports:
      - "8080:8080"
Run Code Online (Sandbox Code Playgroud)

以下是自定义配置文件 mycustom.cnf

[mysqld]
max_allowed_packet=32M
character-set-server=utf8
collation-server=utf8_bin
transaction_isolation=READ-COMMITTED
Run Code Online (Sandbox Code Playgroud)

运行docker-compose up -d并验证变量docker exec -it test-mysql8 bash

mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%max_allowed_packet';
+---------------------------+------------+
| Variable_name             | Value      |
+---------------------------+------------+
| max_allowed_packet        | 67108864   |
| mysqlx_max_allowed_packet | 67108864   |
| slave_max_allowed_packet  | 1073741824 |
+---------------------------+------------+
3 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

mycustom.cnf期望as中给出的变量值而 max_allowed_packet=33554432不是max_allowed_packet=67108864

我知道该命令可以在撰写文件中使用,如下所示,但我想使用自定义文件

......
command: --character-set-server=utf8 --collation-server=utf8_bin --max-allowed-packet=32M --transaction-isolation=READ-COMMITTED
......
Run Code Online (Sandbox Code Playgroud)

小智 9

我的方式没有配置文件。当我们只想更改一些参数时,这很优雅:

mysqldb:
    image: mysql:8.0.29
    command: 
        --default-authentication-plugin=mysql_native_password
        --max_connections=666
        --bind-address=0.0.0.0
        --transaction-isolation=READ-COMMITTED
    ports:
    - 3306:3306

    environment:
    - MYSQL_ROOT_PASSWORD=pass
    - MYSQL_TCP_PORT=3399
    - MYSQL_DATABASE=appdb

    volumes:
    - mysql:/var/lib/mysql
    - mysql_config:/etc/mysql
Run Code Online (Sandbox Code Playgroud)

检查一下:

mysql -uroot -ppass -h127.0.0.1 -P3306 -e 'show global variables like "max_connections"';
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

配置文件的替代方案。详情参见https://hub.docker.com/_/mysql官方mysql docker镜像。