LoginSignup
0
1

More than 3 years have passed since last update.

MySQLのjson型

Posted at

はじめに

MySQL5.7.8以降はjson型が使えるようになりました。
JSONを慣れた開発者に対して選択肢が増えました。

数点をメモとして残ります。

The JSON Data Type: https://dev.mysql.com/doc/refman/8.0/en/json.html

WorkbenchからカラムをJSONに変更しようとするとサポートされないエラー

WorkbenchツールでテーブルのカラムをJSON型に設定してみると、サポートされませんってエラーが出る場合があります。

問題1:接続しているサーバは5.7.8以上ではない 

これはバージョンアップするしかないです。

問題2:MySQLサーバは5.7.8以上でもエラー

これはWorkbenchの設定問題かもしれません。
image.png

操作してみる

JSON文字列でインサート

insert into json_test(json_data) values('{"key1":"value1", "key2":"value2"}')

JSON_OBJECT関数でインサート

JSON_OBJECT関数を使って属性を設定する可能です。

insert into json_test(json_data) values(JSON_OBJECT('key11', 'value11', 'key22', 'value22'))

image.png

複数行をJsonArrayに変換

SELECT JSON_ARRAYAGG(JSON_OBJECT('userName', userName, 'email', email)) from User;

MySQL8.0のクライアントから接続で認証失敗?

mysql_native_passwordのユーザー変更で解消できます。

ALTER USER 'dbuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'dbUserPasswordxxxx';

トリガー作成できない?

問題1:権限ありません

ERROR 1419 (HY000) at line 3155: You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)

log_bin_trust_function_creatorsを1設定変更で回避できます。

問題2:DEFINER記載しないと作成権限があってもエラー

CREATE DEFINER=root@localhostTRIGGER xxx の感じでDEFINERを定義で回避できる場合があります。

MySQLのJSONメソッド

image.png

出典: https://dev.mysql.com/doc/refman/8.0/en/json-function-reference.html

以上

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