0
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?

【MYSQL】日本語が文字化けになった時の対策

Posted at

問題
保存される日本語データはDB上で文字化けになった問題が起こった。

解決策
まずデータベースやテーブルがUTF-8対応できるかどうかを見る。

SHOW VARIABLES LIKE 'character_set%';

`character_set_server や character_set_database が utf8mb4 になっているか確認する。

+--------------------------+--------------------+
| 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/ |
+--------------------------+--------------------+

character_set_server および character_set_databaseutf8mb4 になっていることを確認する。
他の値(例: latin1)が設定されている場合、文字化けの原因となることがある。もしlatin1だと、下記のようにデータベースの文字セットを変える。

ALTER DATABASE `your_database_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

データベースではなく、テーブルの文字セットを変えたい場合は、下記のように変える。

ALTER TABLE `your_table_name` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

最後に、変更が反映されているかを確認する。

SHOW VARIABLES LIKE 'character_set%';
0
0
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
0
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?