mysql 5.7.12 新增 X plugin 詳解

X plugin是mysql新發版本5.7.12中新增的插件,利用它實現mysql作爲文件存儲數據庫,也就是利用mysql 5.7版本json支持的特性完成,安裝很簡單,需要下載5.7.12版本並且安裝mysqlsh工具。

[root@slave-3 src]# vim /etc/yum.repos.d/mysql-community.repo 
[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/6/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[root@slave-3 src]# yum install mysql-shell

這樣就可以直接安裝上mysqlsh工具,官網給的爲gpgcheck=1,但是我這安裝需要修改爲gpgcheck=0才能正常安裝,這個各位自行檢查吧。

[root@slave-3 src]# mysqlsh -u root -h localhost -p --classic --dba enableXProtocol

執行上面命令之後就爲mysql安裝好X plugin了,可以到mysql查看是否開啓

mysql> show plugins;
| mysqlx                     | ACTIVE   | DAEMON             | mysqlx.so | GPL     |

有上面顯示的plugin就已正常安裝,改插件需要啓用單獨的協議,所以能看到對應的網絡監聽端口,默認爲33060,現在來進行一些測試

[root@slave-3 src]# mysqlsh -u root
Creating an X Session to root@localhost:33060
Enter password: 
No default schema selected.
Type '\help', '\h' or '\?' for help.

Currently in JavaScript mode. Use \sql to switch to SQL mode and execute queries.
mysql-js> db;

mysql-js>

命令和mongodb類似,現在還沒schema和collection,用官網的示例文件world_x-db.zip直接導入

mysqlsh -u root --sql --recreate-schema world_x < /usr/local/src/world_x-db/world_x.sql
Enter password: ****Recreating schema world_x...
[root@slave-3 src]# mysqlsh -u root -p world_x
mysql-js> db
<Schema:world_x>
mysql-js> db.collections;
{
    "CountryInfo": <Collection:CountryInfo>
}
mysql-js> db.CountryInfo.find().limit(1);
[
    {
        "GNP": 828,
        "IndepYear": null,
        "Name": "Aruba",
        "_id": "ABW",
        "demographics": {
            "LifeExpectancy": 78.4000015258789,
            "Population": 103000
        },
        "geography": {
            "Continent": "North America",
            "Region": "Caribbean",
            "SurfaceArea": 193
        },
        "government": {
            "GovernmentForm": "Nonmetropolitan Territory of The Netherlands",
            "HeadOfState": "Beatrix"
        }
    }
]
1 document in set (0.00 sec)

mysql-js>

可以看到有點類似於mongodb的操作,現在自己來創建schema和collection進行步驟熟悉

mysql-js> CREATE SCHEMA test_1;
SyntaxError: Unexpected identifier at (shell):1:7
in CREATE SCHEMA test_1;
          ^^^^^^
SyntaxError: Unexpected identifier
mysql-js> \q
Bye!
[root@slave-3 src]# mysqlsh -u root  --recreate-schema test_1
Creating an X Session to root@localhost:33060/test_1
ArgumentError: Recreate schema option can only be used in classic or node sessions
[root@slave-3 src]# mysqlsh -u root --recreate-schema test_1 --sql < aa.sql
Enter password: 
Recreating schema test_1...
[root@slave-3 src]# mysqlsh -u root -p test_1
mysql-js> db
<Schema:test_1>
mysql-js> \q
Bye!
[root@slave-3 src]# cat aa.sql
DROP SCHEMA test_1;
CREATE SCHEMA test_1;

上面操作可以看出要創建schema只能利用--sql的方式,事先寫入到一個sql文件才能正常創建。假如要進行schema切換使用 db = session.getSchema("test_1"),如下:

mysql-js> db;
<Schema:test_1>
mysql-js> db = session.getSchema("world_x");
<Schema:world_x>

下面再來對collection創建操作進行測試:

mysql-js> db;
<Schema:test_1>
mysql-js> db.collections;
{
    "CountryInfo": <Collection:CountryInfo>, 
    "xz_test": <Collection:xz_test>
}
mysql-js> db.createCollection("a");
<Collection:a>
mysql-js> db.collections;
{
    "CountryInfo": <Collection:CountryInfo>, 
    "a": <Collection:a>, 
    "xz_test": <Collection:xz_test>
}
mysql-js> session.dropCollection("world_x","a");
Query OK (0.00 sec)

mysql-js> db.collections;
{
    "CountryInfo": <Collection:CountryInfo>, 
    "xz_test": <Collection:xz_test>
}
mysql-js>

創建collection和mongodb類似,刪除操作有點不同...............對collection的查找、更新、刪除和索引添加等操作都有所不同,可以到官網查看有詳細的介紹,太長就不寫啦。



現在來瞧瞧它是用的什麼引擎,原理又是啥.............

[root@slave-3 src]# mysqlsh -u root -p world_x;
Creating an X Session to root@localhost:33060/world_x
Enter password: 
mysql-js> db.collections;
{
    "CountryInfo": <Collection:CountryInfo>, 
    "xz_test": <Collection:xz_test>
}
mysql-js> db.xz_test.find().limit(1);
[
    {
        "_id": "1a5501cc7efde511d814000c290c4817",
        "age": 123,
        "name": "xiaozhong"
    }
]
1 document in set (0.00 sec)
mysql-js>db.xz_test.createIndex("age").field("age", "INTEGER", false).execute();
Query OK (0.01 sec)
mysql-js>

我這先給我自己創建的測試collection的age字段創建了一個索引,現在直接用mysql連接進去看看結構

[root@slave-3 src]# mysql -uroot 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_1             |
| test_a             |
| world_x            |
+--------------------+
7 rows in set (0.00 sec)

mysql> use world_x;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_world_x |
+-------------------+
| City              |
| Country           |
| CountryInfo       |
| CountryLanguage   |
| xz_test           |
+-------------------+
5 rows in set (0.00 sec)
mysql> show create table xz_test\G;
*************************** 1. row ***************************
       Table: xz_test
Create Table: CREATE TABLE `xz_test` (
  `doc` json DEFAULT NULL,
  `_id` varchar(32) GENERATED ALWAYS AS (json_unquote(json_extract(`doc`,'$._id'))) STORED NOT NULL,
  `$ix_i_F177B50B40803DD7D3962E25071AC5CAA3D1139C` int(11) GENERATED ALWAYS AS (json_extract(`doc`,'$.age')) VIRTUAL,
  UNIQUE KEY `_id` (`_id`),
  KEY `age` (`$ix_i_F177B50B40803DD7D3962E25071AC5CAA3D1139C`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

由上面的操作可以看出剛纔創建的schema和collection都能看到,我剛纔創建的age列索引是利用了5.7新特性虛擬列,爲_id創建了一個唯一約束並存儲到磁盤,利用的innodb引擎,這就可以讓collection支持事物行鎖等innodb引擎的特性。


總結上面的操作對X plugin特性可以得出:

    1、X plugin利用獨有的協議可以模擬類似mongodb操作

    2、創建schema略顯坑

    3、全程需要注意大小寫

    4、可以利用innodb引擎所有特性

    5、索引利用虛擬列完成

    6、_id字段利用uuid函數生成的數據,去掉了中間連接符"-",所以在上面加了個unique約束

效率未曾測試,如果有興趣可以自己測試,官網也有對文檔存儲的詳細介紹,可以自行進行查找。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章