Skip to content

Instantly share code, notes, and snippets.

@mmyoji
Last active April 28, 2023 23:44
Show Gist options
  • Save mmyoji/85922f2fcd4bface34387c5e9727643f to your computer and use it in GitHub Desktop.
Save mmyoji/85922f2fcd4bface34387c5e9727643f to your computer and use it in GitHub Desktop.
Docker MySQL JP settings
mysql> SELECT * FROM information_schema.SCHEMATA WHERE schema_name = "test";
+--------------+-------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+-------------+----------------------------+------------------------+----------+
| def | test | latin1 | latin1_swedish_ci | NULL |
+--------------+-------------+----------------------------+------------------------+----------+
1 row in set (0.00 sec)
-- https://stackoverflow.com/a/6115705
mysql> ALTER DATABASE test CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
mysql> select * from information_schema.SCHEMATA WHERE schema_name = "test";
+--------------+-------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+-------------+----------------------------+------------------------+----------+
| def | test | utf8mb4 | utf8mb4_unicode_ci | NULL |
+--------------+-------------+----------------------------+------------------------+----------+
1 row in set (0.00 sec)

see https://stackoverflow.com/a/3513812

Prepare my.cnf like the following:

# Copy the content of docker image's `/etc/my.cnf` and paste here

[mysql]
default-character-set = utf8mb4

[client]
default-character-set = utf8mb4

[mysqld]
collation-server = utf8mb4_unicode_ci
character-set-server = utf8mb4

mount in docker-compose.yml

services:
  db:
    # ...
    volumes:
      - ./my.cnf:/etc/my.cnf
version: "3.8"
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
MYSQL_DATABASE: "test"
# see https://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html
# 1. This sets OS timezone
# 2. Also changes `@@global.system_time_zone` from 'UTC' to 'JST'
TZ: "Asia/Tokyo"
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment