mysql 5.7.12 新增 X plugin x 协议 详解
http://xiaozhong991.blog.51cto.com/2354914/1763792
x 协议 操作nosql数据库
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_testG;
*************************** 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约束
mysql8.0.17:x协议 dev api,操作创建mgr比运维平台更加方便