1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DockerでFastAPI + MySQLでAPIの開発中に実際のDBを見てみようとselectしてみたら

mysql> SELECT * FROM TASKS;
+----+--------------+
| id | title        |
+----+--------------+
|  1 | ???????????? |
|  2 | ???????????? |
|  3 | ???????????? |
+----+--------------+
3 rows in set (0.01 sec)

デフォルトのままだと文字コードがutf8mb4に設定されていなく、かつ今回やった方法をピンポイントで紹介している記事がなかったので書きます。

やり方(結論)

docker-compose.ymlがあるディレクトリにdb用ディレクトリを切り、my.cnfを配置してコンテナ内にマウントする、これだけです。

手順

1. my.cnfを作成する

(project root)
├── Dockerfile
├── docker-compose.yaml
├── poetry.lock
├── pyproject.toml
├── api
│   ├── __init__.py
│   └── ...
└── db
    └── my.cnf ←追加する

自分の場合は新しくディレクトリを切って/db/my.cnfを追加しました

2. my.cnf内に必要な設定を記述する

my.cnf
[mysqld]
character_set_server = utf8mb4
collation-server=utf8mb4_unicode_ci

[mysql]
default-character-set = utf8mb4

[client]
default-character-set = utf8mb4

3. コンテナにマウントしてdocker-compose up

docker-compose.yml
version: '3'
services:
  db:
    image: mysql:8.0
    platform: linux/x86_64
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
      MYSQL_DATABASE: 'demo'
      TZ: 'Asia/Tokyo'
    volumes:
      - mysql_data:/var/lib/mysql
      - ./db/my.cnf:/etc/mysql/conf.d/my.cnf #日本語化対応
    command: --default-authentication-plugin=mysql_native_password  
    ports:
      - 33306:3306
volumes:
  mysql_data:

dbのvolumesに/db/my.cnf:/etc/mysql/conf.d/my.cnfを追加します。
こちらを追記した後、docker-compse upします

4. 動作確認

mysql> select * from tasks;
+----+--------------------------------------+
| id | title                                |
+----+--------------------------------------+
|  1 | クリーニングを取りに行く             |
|  3 | クリーニングを取りに行く             |
|  4 | クリーニングを取りに行く             |
+----+--------------------------------------+
3 rows in set (0.01 sec)

無事日本語化対応できました。

一応文字コードの設定も見ておきます

mysql> SHOW VARIABLES LIKE '%char%';
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8mb4                        |
| character_set_connection | utf8mb4                        |
| character_set_database   | utf8mb4                        |
| character_set_filesystem | binary                         |
| character_set_results    | utf8mb4                        |
| character_set_server     | utf8mb4                        |
| character_set_system     | utf8mb3                        |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.06 sec)

参考にさせて頂いた記事

基礎MySQL ~その2~ my.cnf (設定ファイル)

文字コードの確認と設定方法

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?