はじめに
備忘録
タイトルの通りです。
どなたかのお役に立てれば幸いです。
解決方法
ファイル構造は以下とします。
root/
├─ db/
│ ├─ conf.d/
│ │ └─ my.cnf
│ │
│ ├─ data/
│ └─ Dockerfile
│
└─ docker-compose.yml
また、my.cnf
は以下のようになっています。
[mariadb]
query_cache_limit=1M
query_cache_size=32M
query_cache_type=1
Docker Hubの、MariaDBのページ(こちら)にも書いてある通り、.cnf
の最初に[mariadb]
をつけないと、設定が正しく反映されません。
Custom configuration files should end in .cnf and be mounted read only at the directory /etc/mysql/conf.d. These files should contain the minimal changes from the MariaDB workload required for your application/environment. A MariaDB configuration file will have a [mariadb] group followed by variable = value settings per Setting Server System Variables or option-prefix-variable.
docker-compose.yml
のvolumes
の部分に、- ./db/conf.d:/etc/mysql/conf.d
を追加して、conf.d/
をマウントします。
services:
db:
build: ./db
ports:
- 3306:3306
volumes:
- ./db/data:/var/lib/mysql
- ./db/conf.d:/etc/mysql/conf.d
コンテナを立ち上げて、ちゃんとマウントできているか確認します。
$ docker compose up -d --build
[+] Building ...
︙
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e467bc1155b db "cron -f" 30 minutes ago Up 30 minutes db-1
$ docker container exec -it db-1 bash
root@1e467bc1155b:/#
root@1e467bc1155b:/# cd /etc/mysql/conf.d
root@1e467bc1155b:/etc/mysql/conf.d# pwd
/etc/mysql/conf.d
root@1e467bc1155b:/etc/mysql/conf.d# ls
my.cnf
MariaDBに入ってクエリキャッシュの設定を確認してみます。
root@1e467bc1155b:/# mysql -h localhost -P 3306 -u username -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.8.2-MariaDB-1:10.8.2+maria~focal mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
SHOW VARIABLES LIKE '%query_cache%';
でクエリキャッシュの設定を表示してみます。
MariaDB [(none)]> SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 33554432 |
| query_cache_strip_comments | OFF |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
7 rows in set (0.002 sec)
query_cache_type
がON
となっている(クエリキャッシュが起動している)ことを確認できました。