Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

MySQLの文字コードUTF8対応

Last updated at Posted at 2019-09-06

設定

とりあえず、データベース作る前にmy.iniに↓の設定を入れる。

[mysql]
default-character-set=utf8mb4

[mysqld]
character-set-server = utf8mb4
skip-character-set-client-handshake
collation-server = utf8mb4_bin
init-connect = SET NAMES utf8mb4
innodb_file_format = Barracuda
innodb_file_per_table = 1
innodb_large_prefix

collation-serverは文字の比較の仕様に合わせて変える

確認用クエリ

show variables like '%char%';
show variables like '%coll%';

結果
※クライアントツールを使っている場合、ツール側の設定でクライアントの設定が指定されている場合があるので、クライアント向けの設定が変化しない場合は、コマンドの mysql -h 127.0.0.1 で接続して確認する。

mysql> show variables like '%coll%';
+----------------------+-------------+
| Variable_name        | Value       |
+----------------------+-------------+
| collation_connection | utf8mb4_bin |
| collation_database   | utf8mb4_bin |
| collation_server     | utf8mb4_bin |
+----------------------+-------------+
3 rows in set, 1 warning (0.00 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     | utf8                                   |
| character_sets_dir       | C:\tools\mysql\current\share\charsets\ |
+--------------------------+----------------------------------------+
8 rows in set, 1 warning (0.00 sec)

項目ごとの意味は、こちら の記事に書かれている。

Docker Compose用

commandのところ。

# Use root/password as user/password credentials
services:
  mysql:
    image: mysql:5.7
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_bin --skip-character-set-client-handshake
    environment: 
      MYSQL_ROOT_PASSWORD: password
    ports:
        - 3306:3306
    volumes: 
        - ./mysql-init:/docker-entrypoint-initdb.d
        - ./mysql-local-data:/var/lib/mysql
        - ./mysql-conf:/etc/mysql/conf.d/

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?