manvi77
manvi77

Reputation: 570

how to use custom.cnf in mysql server 8 docker-compose file?

trying to use custom configuration file in docker-compose mysql 8. The docker-compose.yml looks like

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"

Below is the custom config file, mycustom.cnf

[mysqld]
max_allowed_packet=32M
character-set-server=utf8
collation-server=utf8_bin
transaction_isolation=READ-COMMITTED

Run the docker-compose up -d and verify the variables 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)

Expecting the variable value as given in mycustom.cnf as max_allowed_packet=33554432 instead of max_allowed_packet=67108864

I know that command can be used in compose file as below, but I want to use custom file

......
command: --character-set-server=utf8 --collation-server=utf8_bin --max-allowed-packet=32M --transaction-isolation=READ-COMMITTED
......

Upvotes: 5

Views: 10199

Answers (2)

Sharak
Sharak

Reputation: 990

Docker volumes are directories so instead of

volumes:
      - path/to/config/mycustom.cnf:/etc/mysql/conf.d/custom.cnf

you should use

volumes:
      - path/to/config:/etc/mysql/conf.d

Here's my config that works perfectly:

config/mysql/my.cnf

[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
max_allowed_packet = 32M

docker-compose.yml

volumes:
  db-data:

services:
  mysql:
    image: mysql:8
    ports:
      - "3306:3306"
    volumes:
      - db-data:/var/lib/mysql
      - ./config/mysql:/etc/mysql/conf.d
    environment:
      MYSQL_ROOT_PASSWORD: secretpassword
      MYSQL_DATABASE: projectdb
    restart: unless-stopped

Upvotes: 0

Barrrettt
Barrrettt

Reputation: 819

My way without config file. This is elegant when we only want to change a few parameters:

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

Check this with:

mysql -uroot -ppass -h127.0.0.1 -P3306 -e 'show global variables like "max_connections"';

enter image description here

An alternative to the configuration files. See details https://hub.docker.com/_/mysql official mysql docker image.

Upvotes: 10

Related Questions