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?

More than 1 year has passed since last update.

Windowsで UTF-8 でかかれた スクリプトを MyBatis Migration で MySQL に流し込むと文字化けエラーとなる件

Posted at

困っていること

うっかり、macOS で作った MyBatis Migration 用の Script は大抵 UTF-8 で書いてしまうと思いますが、そんなスクリプトを Windows でそのまま動かすと、コメントもデータ内容も文字化けしてしまう事象にはまってしまいました。

以下、PowerShell は肌に合わないので、Git Bash で動かしてます。

だめだった時

$ migrate.cmd up

 :
 :

------------------------------------------------------------------------
-- MyBatis Migrations SUCCESS
-- Total time: 1s
-- Finished at: Sat Jul 09 16:07:16 JST 2022
-- Final Memory: 15M/500M
------------------------------------------------------------------------

MySQL で確認

mysql> set character_set_client=utf8mb4;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_connection=utf8mb4;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_results=utf8mb4;
Query OK, 0 rows affected (0.00 sec)

mysql> show full columns from role;
+------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field      | Type         | Collation       | Null | Key | Default | Extra          | Privileges                      | Comment
                                                                                                                                                                                      |
+------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id         | int          | NULL            | NO   | PRI | NULL    | auto_increment | select,insert,update,references | ID:繧ェ繝シ繝医う繝ウ繧ッ繝ェ繝。繝ウ繝�
                                                                                                                                                           |
| role       | varchar(50)  | utf8_general_ci | NO   | UNI | NULL    |                | select,insert,update,references | 讓ゥ髯�:USER:蛻ゥ逕ィ閠�,APPLICANT:逕ウ隲玖��,APPROVER:謇ソ隱崎��,ADMINISTRATOR:邂。逅�閠�,遲�縺セ繧∫キィ髮�閠��シ哥 |
| role_name  | varchar(200) | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 讓ゥ髯仙錐
                                                                                                                                                                                |
| version_no | int          | NULL            | NO   |     | NULL    |                | select,insert,update,references | 繝舌�シ繧ク繝ァ繝ウNo
                                                                                                                                                                       |
| created_at | datetime     | NULL            | NO   |     | NULL    |                | select,insert,update,references | 逋サ骭イ譌・
                                                                                                                                                                             |
| created_by | varchar(100) | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 逋サ骭イ閠�
                                                                                                                                                                             |
| updated_at | datetime     | NULL            | NO   |     | NULL    |                | select,insert,update,references | 譖エ譁ー譌・
                                                                                                                                                                             |
| updated_by | varchar(100) | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 譖エ譁ー閠�
                                                                                                                                                                             |
| is_actived | varchar(1)   | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 譛牙柑繝輔Λ繧ー:繧ウ繝シ繝牙�、�シ壹ヵ繝ゥ繧ー�シ�0:繧ェ繝輔��1:繧ェ繝ウ�シ�        
                                                                                                                          |
+------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
9 rows in set (0.00 sec)


化け化けです。

解決方法

JAVACMD に -Dfile.encoding=UTF-8 を渡してあげるとうまく行きました。

$ JAVACMD="java -Dfile.encoding=UTF-8" migrate.cmd up

 :
 :
コンソール上は文字化けしてますが・・
 :
------------------------------------------------------------------------
-- MyBatis Migrations SUCCESS
-- Total time: 1s
-- Finished at: Sat Jul 09 16:12:42 JST 2022
-- Final Memory: 15M/500M
------------------------------------------------------------------------

MySQLで再確認

mysql> set character_set_client=utf8mb4;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_connection=utf8mb4;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_results=utf8mb4;
Query OK, 0 rows affected (0.00 sec)

mysql> show full columns from role;
+------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field      | Type         | Collation       | Null | Key | Default | Extra          | Privileges                      | Comment
                                                                                              |
+------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id         | int          | NULL            | NO   | PRI | NULL    | auto_increment | select,insert,update,references | ID:オートインクリメント
                                                                                    |
| role       | varchar(50)  | utf8_general_ci | NO   | UNI | NULL    |                | select,insert,update,references | 権限:USER:利用者,APPLICANT:申請者,APPROVER:承認者,ADMINISTRATOR:管理者 |
| role_name  | varchar(200) | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 権限名
                                                                                           |
| version_no | int          | NULL            | NO   |     | NULL    |                | select,insert,update,references | バージョンNo
                                                                                         |
| created_at | datetime     | NULL            | NO   |     | NULL    |                | select,insert,update,references | 登録日
                                                                                           |
| created_by | varchar(100) | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 登録者
                                                                                           |
| updated_at | datetime     | NULL            | NO   |     | NULL    |                | select,insert,update,references | 更新日
                                                                                           |
| updated_by | varchar(100) | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 更新者
                                                                                           |
| is_actived | varchar(1)   | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 有効フラグ:コード値:フラグ(0:オフ、1:オン)
                                                                          |
+------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
9 rows in set (0.00 sec)

文字化け解消されました。

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?