先日、リリースされたMySQL5.7.12に含まれるmysqlxプラグインと、
同時にリリースされた、MySQL Shellの初期設定と基本動作確認を確認してみました。
基本的にこれらは、MySQL5.7で追加されたJSONデータ型を含むスキーマレスなドキュメントデータを、
mysqlxプラグインを利用する事でより柔軟に処理出来るようにする為に追加され、
MySQLをドキュメントデータベースとしてより利用し易くする為に追加された機能となっています。
MySQL Shellは、MySQLサーバの開発と管理をサポートする、
JavaScript、Python、SQLの対話型のインタフェースをサポートするMySQLの新しいコンポーネント。
データのクエリと更新処理だけでなく、様々な管理操作を実行する為にMySQLのシェルを使用することができます。
※MySQL Shellを利用する為には、事前にMySQL5.7.12以降に含まれているXプラグインをインストールしておく必要があります。
Xプラグイン
Xプロトコルを使用して通信を可能にするMySQLサーバプラグイン。
X DevAPIを実装するクライアントをサポートし、ドキュメントストアとしてMySQLを使用することができます。
Xプロトコル
Xプロトコルは、Xプラグインを実行しているMySQLサーバーと通信します。
Xプロトコルは、SSL経由でCRUDとSQL操作、認証の両方をサポートしていて、
コマンドのストリーミング処理を可能にし、プロトコルとメッセージレイヤ上に拡張可能。
root@localhost [information_schema]> select PLUGIN_NAME,PLUGIN_VERSION,PLUGIN_DESCRIPTION from plugins -> where PLUGIN_NAME = 'mysqlx'; +-------------+----------------+--------------------+ | PLUGIN_NAME | PLUGIN_VERSION | PLUGIN_DESCRIPTION | +-------------+----------------+--------------------+ | mysqlx | 1.0 | X Plugin for MySQL | +-------------+----------------+--------------------+ 1 row in set (0.00 sec) root@localhost [information_schema]>
【X Pluginインストール】
参照:http://dev.mysql.com/doc/refman/5.7/en/x-plugin-installation.html
通常通り、INSTALL PLUGINコマンドでインストール
root@localhost [mysql]> INSTALL PLUGIN mysqlx SONAME 'mysqlx.so'; Query OK, 0 rows affected (0.25 sec) root@localhost [mysql]> show plugins; +----------------------------+----------+--------------------+-------------------+-------------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+-------------------+-------------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | PROPRIETARY | | mysql_native_password | ACTIVE | AUTHENTICATION | NULL | PROPRIETARY | | sha256_password | ACTIVE | AUTHENTICATION | NULL | PROPRIETARY | | InnoDB | ACTIVE | STORAGE ENGINE | NULL | PROPRIETARY | | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | PROPRIETARY | | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | PROPRIETARY | | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | PROPRIETARY | | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | PROPRIETARY | <SNIP> | MyISAM | ACTIVE | STORAGE ENGINE | NULL | PROPRIETARY | | MEMORY | ACTIVE | STORAGE ENGINE | NULL | PROPRIETARY | | CSV | ACTIVE | STORAGE ENGINE | NULL | PROPRIETARY | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | PROPRIETARY | | partition | ACTIVE | STORAGE ENGINE | NULL | PROPRIETARY | | FEDERATED | DISABLED | STORAGE ENGINE | NULL | PROPRIETARY | | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | PROPRIETARY | | ngram | ACTIVE | FTPARSER | NULL | PROPRIETARY | | mecab | ACTIVE | FTPARSER | libpluginmecab.so | PROPRIETARY | | MYSQL_FIREWALL | ACTIVE | AUDIT | firewall.so | PROPRIETARY | | MYSQL_FIREWALL_USERS | ACTIVE | INFORMATION SCHEMA | firewall.so | PROPRIETARY | | MYSQL_FIREWALL_WHITELIST | ACTIVE | INFORMATION SCHEMA | firewall.so | PROPRIETARY | | mysqlx | ACTIVE | DAEMON | mysqlx.so | PROPRIETARY | +----------------------------+----------+--------------------+-------------------+-------------+ 49 rows in set (0.00 sec) root@localhost [mysql]>
【X Plugin オプションと変数について】
設定パラメータに関しては、此方を参照下さい。
http://dev.mysql.com/doc/refman/5.7/en/x-plugin-option-variable-reference.html
【接続方法】
既に、mysqlshをインストール済みなので –sqlオプションを仕様してSQLを流し込んでみる。
mysqlxのPort33060(Default:mysqlx_port)から、ローカルのMySQLにアクセスしてSQLを実行しています。
【サンプルデータベース作成】
参照:
http://downloads.mysql.com/docs/world_x-db.zip
http://dev.mysql.com/doc/refman/5.7/en/mysql-shell-tutorial-javascript-download.html
mysqlsh経由でサンプルデータベースの作成して、mysqlクライアントで接続してスキーマとテーブルの確認
[root@misc01 MID2016]# mysqlsh -u admin -p --sql --recreate-schema world_x < world_x.sql Enter password: Recreating schema world_x... [root@misc01 MID2016]# mysql -u root -p -e "show databases like 'world%'" Enter password: +-------------------+ | Database (world%) | +-------------------+ | world | | world2 | | world_x | +-------------------+ [root@misc01 MID2016]# mysql -u root -p -e "show tables from world_x" Enter password: +-------------------+ | Tables_in_world_x | +-------------------+ | City | | Country | | CountryInfo | | CountryLanguage | +-------------------+
【接続モード】
参考) 接続方法としては、現状では、以下のモードを選択できるようです。
# mysqlsh --help MySQL Shell 1.0.3 Development Preview <SNIP> --sql Start in SQL mode using a node session. --sqlc Start in SQL mode using a classic session. --js Start in JavaScript mode. --py Start in Python mode. <SNIP>
こちらでは、mysqlsh経由でjava scriptモードの状態でJSONデータを処理してみます。
[root@misc01 MID2016]# mysqlsh -u admin -p Creating an X Session to admin@localhost:33060 Enter password: No default schema selected. Welcome to MySQL Shell 1.0.3 Development Preview Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help', '\h' or '\?' for help. Currently in JavaScript mode. Use \sql to switch to SQL mode and execute queries. mysql-js> news_collection = session.getSchema('NEW57').createCollection("X_JSON"); <Collection:X_JSON> mysql-js> news_collection.add({"id": 1, "name": "Document Data", "price": 60000, "Conditions": ["NEW", 2016]}); Query OK, 1 item affected (0.01 sec) mysql-js> news_collection.find('name like :n').bind('n','Document%'); [ { "Conditions": [ "NEW", 2016 ], "_id": "26c8af795703e611630c0800279cea3c", "id": 1, "name": "Document Data", "price": 60000 } ] 1 document in set (0.03 sec) mysql-js>
MySQLに接続してデータを確認した状態
root@localhost [NEW57]> desc X_JSON; +-------+-------------+------+-----+---------+------------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+------------------+ | doc | json | YES | | NULL | | | _id | varchar(32) | NO | PRI | NULL | STORED GENERATED | +-------+-------------+------+-----+---------+------------------+ 2 rows in set (0.00 sec) root@localhost [NEW57]> select * from X_JSON\G *************************** 1. row *************************** doc: {"id": 1, "_id": "26c8af795703e611630c0800279cea3c", "name": "Document Data", "price": 60000, "Conditions": ["NEW", 2016]} _id: 26c8af795703e611630c0800279cea3c 1 row in set (0.00 sec) root@localhost [NEW57]>
【Beta Draft】
X DevAPI User Guide / Overview
http://dev.mysql.com/doc/x-devapi-userguide/en/devapi-users-introduction.html
その他、X DevAPI経由でコーディング出来そう。但し、現時点ではベータリリースなのでもう少ししたら確認。
【その他:Status変数】
こちらで、mysqlx経由の処理を確認する事が可能です。
[root@misc01 MID2016]# mysql -u root -p -e "show status like 'mysqlx%'" Enter password: +-------------------------------------+--------------------------+ | Variable_name | Value | +-------------------------------------+--------------------------+ | Mysqlx_bytes_received | 389773 | | Mysqlx_bytes_sent | 3466 | | Mysqlx_connection_accept_errors | 0 | | Mysqlx_connection_errors | 0 | | Mysqlx_connections_accepted | 2 | | Mysqlx_connections_closed | 2 | | Mysqlx_connections_rejected | 0 | | Mysqlx_crud_delete | 0 | | Mysqlx_crud_find | 0 | | Mysqlx_crud_insert | 0 | | Mysqlx_crud_update | 0 | | Mysqlx_errors_sent | 1 | | Mysqlx_expect_close | 0 | | Mysqlx_expect_open | 0 | | Mysqlx_init_error | 1 | | Mysqlx_notice_other_sent | 76 | | Mysqlx_notice_warning_sent | 2 | | Mysqlx_rows_sent | 18 | | Mysqlx_sessions | 0 | | Mysqlx_sessions_accepted | 1 | | Mysqlx_sessions_closed | 1 | | Mysqlx_sessions_fatal_error | 0 | | Mysqlx_sessions_killed | 0 | | Mysqlx_sessions_rejected | 1 | | Mysqlx_ssl_accepts | 0 | | Mysqlx_ssl_active | | | Mysqlx_ssl_cipher | | | Mysqlx_ssl_cipher_list | | | Mysqlx_ssl_ctx_verify_depth | 18446744073709551615 | | Mysqlx_ssl_ctx_verify_mode | 5 | | Mysqlx_ssl_finished_accepts | 0 | | Mysqlx_ssl_server_not_after | Oct 19 05:28:15 2025 GMT | | Mysqlx_ssl_server_not_before | Oct 22 05:28:15 2015 GMT | | Mysqlx_ssl_verify_depth | | | Mysqlx_ssl_verify_mode | | | Mysqlx_ssl_version | | | Mysqlx_stmt_create_collection | 0 | | Mysqlx_stmt_create_collection_index | 0 | | Mysqlx_stmt_disable_notices | 0 | | Mysqlx_stmt_drop_collection | 0 | | Mysqlx_stmt_drop_collection_index | 0 | | Mysqlx_stmt_enable_notices | 0 | | Mysqlx_stmt_execute_sql | 69 | | Mysqlx_stmt_execute_xplugin | 2 | | Mysqlx_stmt_kill_client | 0 | | Mysqlx_stmt_list_clients | 0 | | Mysqlx_stmt_list_notices | 0 | | Mysqlx_stmt_list_objects | 2 | | Mysqlx_stmt_ping | 0 | | Mysqlx_worker_threads | 2 | | Mysqlx_worker_threads_active | 0 | +-------------------------------------+--------------------------+ [root@misc01 MID2016]#
参考までに、MySQL ShellのSQLモードの場合とjavascriptモードの場合では接続が3306か33060からの接続かどうかは以下のステータス変数でも確認する事が可能です。
【SQLモードの場合】
[root@misc01 MID2016]# echo "INSERT INTO T_UC01 VALUES (1,'mysql shell'),(2,'マイエスキュウエルシェル');" | mysqlsh -u demo_user -ppassword --sql --schema=world_x2 mysqlx: [Warning] Using a password on the command line interface can be insecure. [root@misc01 MID2016]#
root@localhost [sys]> show status like 'Mysqlx_crud%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | Mysqlx_crud_delete | 0 | | Mysqlx_crud_find | 0 | | Mysqlx_crud_insert | 0 | | Mysqlx_crud_update | 0 | +--------------------+-------+ 4 rows in set (0.00 sec) root@localhost [sys]> root@localhost [sys]> show status like 'Mysqlx_crud%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | Mysqlx_crud_delete | 0 | | Mysqlx_crud_find | 0 | | Mysqlx_crud_insert | 0 | | Mysqlx_crud_update | 0 | +--------------------+-------+ 4 rows in set (0.00 sec) root@localhost [sys]>
【JavaScriptモードの場合】
[root@misc01 MID2016]# mysqlsh --uri demo_user@localhost/NEW57 -ppassword mysqlx: [Warning] Using a password on the command line interface can be insecure. Creating an X Session to demo_user@localhost:33060/NEW57 Default schema `NEW57` accessible through db. Welcome to MySQL Shell 1.0.3 Development Preview Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type '\help', '\h' or '\?' for help. Currently in JavaScript mode. Use \sql to switch to SQL mode and execute queries. mysql-js> db.createCollection("x_posts"); <Collection:x_posts> mysql-js> db.x_posts.add({"title":"Hello World", "text":"This is the first post via mysqlx"}); Query OK, 1 item affected (0.01 sec) mysql-js> db.x_posts.find("title = 'Hello World'").sort(["title"]); [ { "_id": "d2c6eb188b08e6113d110800279cea3c", "text": "This is the first post via mysqlx", "title": "Hello World" } ] 1 document in set (0.00 sec) mysql-js>
mysqlx経由のCRUDの実行数が確認出来る。
INSERT x 1回
SELECT x 1回
root@localhost [sys]> show status like 'Mysqlx_crud%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | Mysqlx_crud_delete | 0 | | Mysqlx_crud_find | 0 | | Mysqlx_crud_insert | 0 | | Mysqlx_crud_update | 0 | +--------------------+-------+ 4 rows in set (0.00 sec) root@localhost [sys]> show status like 'Mysqlx_crud%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | Mysqlx_crud_delete | 0 | | Mysqlx_crud_find | 1 | | Mysqlx_crud_insert | 1 | | Mysqlx_crud_update | 0 | +--------------------+-------+ 4 rows in set (0.01 sec) root@localhost [sys]>
【参考】
http://dev.mysql.com/doc/refman/5.7/en/mysql-shell.html
http://dev.mysql.com/doc/refman/5.7/en/document-store.html
http://dev.mysql.com/doc/dev/connector-nodejs/
http://mysqlserverteam.com/mysql-5-7-12-part-3-more-than-just-sql/
http://mysqlserverteam.com/mysql-5-7-12-part-4-a-new-mysql-command-line-shell/
メモ:Node.jsのサンプルもあるので、後日、別途JSONドキュメント処理を実施してみる。
http://dev.mysql.com/doc/dev/connector-nodejs/
PlanetMySQL Voting: Vote UP / Vote DOWN