26
21

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 5 years have passed since last update.

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

Last updated at Posted at 2019-03-23

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)
26
21
0

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
26
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?