0
1

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.

Azure Database for MySQLを4分で日本語対応

Last updated at Posted at 2023-06-09

概要

Azure Database for MySQL
を使用しており、テーブルのレコードに対して日本語を入れてもpymysql から怒られないように
したかったのですが、案の定時間を溶かしたので備忘録的にメモしておきます。

4ステップで完了します。
1ステップ1分で完了させて、4分後にはタスク完了しましょう。

ステップ1

pymysql のコネクションストリングの変更

ASYNC_DB_URL=mysql+aiomysql://user:password@host_ip:3306/test?charset=utf8mb4

というように、charset=utf8mb4
を追加していなければ追加します。

ステップ2

Azure コンソールから、Server Parameters を変更

Azure Database for MySQL のコンソールから、
Screen Shot 2023-06-09 at 14.18.33.png

ここにあるように、

  • character_set_server
  • collation_server

をそれぞれ

  • utf8mb4
  • utf8mb4_general_ci

と設定します。
1分で完了と書きましたが、このパラメータの変更により、データベースは再度構築されますので、
10分ほど待つ必要があります。
データベースがリスタート完了したら、ステップ3に行きましょう。

ステップ3

MySQL サーバにsshし、以下の設定を反映

show variables like "chara%";

でヒットしたもののうち、

character_set_client
character_set_connection
character_set_database
character_set_results
character_set_server
character_set_system

たちを、

SET character_set_client=utf8mb4;
SET character_set_connection=utf8mb4;
SET character_set_database=utf8mb4;
SET character_set_results=utf8mb4;
SET character_set_server=utf8mb4;
SET character_set_system=utf8mb4;

などを使って全てutf8mb4に設定。

ステップ4

MySQL サーバにsshし、データベースをAlterします。

ALTER DATABASE test character set utf8mb4 collate utf8mb4_general_ci;

上は、testというデータベースに対してコマンドを実行しています。

お疲れ様でした、これでおそらく日本語でレコードをインサートできるようになっているはずです。

何回やってもこういうタスクは時間がかかりますね。。

今回はこの辺で。

@kenmaro

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?