Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
16
Help us understand the problem. What is going on with this article?
@k8uwall

MySQLにおける文字コードutf8mb4の設定

More than 1 year has passed since last update.

MySQLの文字コード事情
の27ページの表が出るようにmy.cfを調整してみる

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     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

準備

$ ls
docker-compose.yml  my.cnf

my.cf

$ cat my.cnf
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_bin
log-bin=/var/log/mysql/bin-log
server-id=1

[client]
default-character-set = utf8mb4

character-set-serverパラメータ

MySQL :: MySQL 5.6 リファレンスマニュアル :: 5.1.4 サーバーシステム変数

他にオプションファイルで指定可能なcharacter_set*といえばcharacter-set-filesystemがあるが、こちらはデフォルトのままが良いようだ。

default-character-setについては
MySQL 5.5ではdefault-character-setが廃止されてた… | イガラシ研
の通り、[mysqld]セクションからは削除されている。ただし、mysqlコマンドは[client]セクションを読んでいるので設定

collation-serverパラメータ

MySQLの文字コードとCollation - Qiita
より、

utf8mb4_bin
A ≠ a 区別する
:sushi::beer: 区別する
は≠ぱ≠ば 区別する

を選択

docker-compose.yml

$ cat docker-compose.yml
version: '2'
services:
  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    volumes:
      - ./mysql-data:/var/lib/mysql
      - ./my.cnf:/etc/mysql/conf.d/my.cnf
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      TZ: "Asia/Tokyo"

接続

$ docker-compose up -d
$ docker-compose exec mysql bash

# mysql -u root
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     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
16
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
k8uwall

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
16
Help us understand the problem. What is going on with this article?