- GreatSQL社区原创内容未经受权不得随便应用,转载请分割小编并注明起源。
导语
有时候用 Postman 接口测试须要获取MySQL的查问后果做接口输入的校验,这里介绍下 Postman 通过 Restful API 接口拜访 MySQL 的工具xmysql的应用办法。
步骤
一、应用nmp装置xmysql
注:npm的装置和配置自行百度,这里次要讲xmysql,就不赘述了
C:\Users\wmp>node -vv12.16.3C:\Users\wmp>C:\Users\wmp> npm install -g xmysqlC:\Users\wmp\AppData\Roaming\npm\xmysql -> C:\Users\wmp\AppData\Roaming\npm\node_modules\xmysql\bin\index.js> es5-ext@0.10.59 postinstall C:\Users\wmp\AppData\Roaming\npm\node_modules\xmysql\node_modules\es5-ext> node -e "try{require('./_postinstall')}catch(e){}"+ xmysql@0.5.1added 131 packages from 68 contributors in 22.806s
装置完输出xmysql能够查看具体参数,同时也示意装置胜利
C:\Users\wmp>xmysqlUsage: index [options]Options: -V, --version output the version number -h, --host <n> hostname of database / localhost by default -u, --user <n> username of database / root by default -p, --password <n> password of database / empty by default -d, --database <n> database schema name -r, --ipAddress <n> IP interface of your server / localhost by default -n, --portNumber <n> port number for app / 3000 by default -o, --port <n> port number for mysql / 3306 by default -S, --socketPath <n> unix socket path / not used by default -s, --storageFolder <n> storage folder / current working dir by default / available only with local -i, --ignoreTables <n> comma separated table names to ignore -a, --apiPrefix <n> api url prefix / "/api/" by default -y, --readOnly readonly apis / false by default -c, --useCpuCores <n> use number of CPU cores (using cluster) / 1 by default -h, --help output usage information Examples: $ xmysql -u username -p password -d databaseSchemaError: password for database is missingError: database name is missingC:\Users\wmp>
连贯数据库:
xmysql -h host_name -o port -u user_name -p user_password -d database_name
C:\Users\wmp>xmysql -h 192.168.5.103 -u root -p 123456 -o 3301 -d test Generating REST APIs at the speed of your thought.. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Database : test Number of Tables : 3 【test库表的数量】 REST APIs Generated : 62 【生成的api数量】 Xmysql took : 0 seconds API's base URL : localhost:3000 【api的拜访形式,端口3000能够通过参数 -n进行批改为其余】 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
xmysql的默认端口是3000,上面用postman调用api的形式试下数据库的增删改查(注: 在postman中执行sql语句时,cmd窗口和msqll都不能够敞开)
API Overview
HTTP Type | API URL | Comments |
---|---|---|
GET | / | 获取所有 REST API |
GET | /api/tableName | 列出表的行 |
POST | /api/tableName | 创立一个新行 |
PUT | /api/tableName | 用新行替换现有行 |
POST | /api/tableName/bulk | 创立多行 - 在申请注释中发送对象数组 |
GET | /api/tableName/bulk | 列出多行 - /api/tableName/bulk?_ids=1,2,3 |
DELETE | /api/tableName/bulk | 删除多行 - /api/tableName/bulk?_ids=1,2,3 |
GET | /api/tableName/:id | 按主键检索一行 |
PATCH | /api/tableName/:id | 按主键更新行元素 |
DELETE | /api/tableName/:id | 按主键删除一行 |
GET | /api/tableName/findOne | 作为列表工作,但取得单个记录匹配条件 |
GET | /api/tableName/count | 计算表中的行数 |
GET | /api/tableName/distinct | 表中的不同行 - /api/tableName/distinct?_fields=col1 |
GET | /api/tableName/:id/exists | 是否存在行 |
GET | /api/parentTable/:id/childTable | 获取具备父表外键的子表行列表 |
GET | /api/tableName/aggregate | 汇总数字列的后果 |
GET | /api/tableName/groupby | 按列的后果分组 |
GET | /api/tableName/ugroupby | 应用一次调用按后果进行多个分组 |
GET | /api/tableName/chart | 基于 (min,max,step) 或 (step array) 或 (automagic) 的数值列散布 |
GET | /api/tableName/autochart | 与 Chart 雷同,但会自动识别哪些是数字列 |
GET | /api/xjoin | 解决连贯 |
GET | /dynamic | 应用参数执行动静 mysql 语句 |
GET | /upload | 上传单个文件 |
GET | /uploads | 上传多个文件 |
GET | /download | 下载文件 |
GET | /api/tableName/describe | 形容每个表的列 |
GET | /api/tables | 获取数据库中的所有表 |
GET | /_health | 获取过程和 mysql 的运行状况 |
GET | /_version | 获取 Xmysql、mysql、node 的版本 |
查看对象表下所有可用的接口,能够参数调用并查看后果
http://localhost:3000/
以下是t1表可用的接口:
用t1表做一个简略的查问测试:
1、查问t1的整表数据
GET /api/t1SQL: select * from `t1` ;
2、带条件查问
GET /api/t1?_where=(ida,eq,1)SQL: select * from `t1` where `ida`=1;
3、分页查问
4、in查问
GET /api/t1/bulk?_ids=1,2,3SQL: select * from `t1` where `ida` in (1,2,3) limit 0,20;
其余的增删改参考上述的API Overview,上面演示下xmysql在postman接口测试中的利用:
利用一:接口测试数据校验
- 启动xmysql服务:(测试时放弃服务启动状态不要敞开)
xmysql -h 192.168.5.103 -u root -p Great@123 -o 3306 -d SCOTT
- 创立postman测试collection,蕴含测试接口和xmysql获取mysql数据接口
#测试接口1:获取全国行政区划http://127.0.0.1:8369/query?fid={{pregion_code}}#接口2:xmysql获取mysql数据接口http://localhost:3000/api/AREAS?_where=(parent_region_code,eq,{{pregion_code}})
接口参数化,可在postman collection中设置全局参数并赋值
//定义变量做接口的输出传参并设置为全局变量var pregion_code=110100;pm.environment.set("pregion_code", pregion_code);
接口输入参数化设置:
在接口1Tests中用js脚本对接口输入后果须要断言的局部转参数并设置环境变量,以便在接口2的数据库输入中进行比拟
//返回body转jsonvar jsonData = JSON.parse(responseBody);var list=jsonData.result;var regionCode=[];num=list.length;for (let i=0;i<num;i++){ regionCode.push(list[i].id);}//设置为全局变量,用于数据库查问接口的数据校验pm.environment.set("regionCode", regionCode);//console.log(regionCode)
接口2Tests中用js脚本对数据库查问到的json脚本进行解决,获取须要的数据,参数化后与接口1的输入进行比拟
//返回body转jsonvar jsonData = JSON.parse(responseBody);var res_id=[];num=jsonData.length;//获取数据库查问后果的region_code列for (let i=0;i<num;i++){ res_id.push(jsonData[i].region_code+'');}//获取接口1输入的后果参数regionCodevar regionCode=pm.environment.get("regionCode");//断言 比拟接口和数据库的输入后果tests["接口断言胜利!!"] = res_id.sort().toString === regionCode.sort().toString ; //console.log(res_id);
- 运行测试collection
利用二:利用postman+xmysql实现MySQL疾速生成大量测试数据
建表脚本
drop table if exists t1;CREATE TABLE t1(id int NOT NULL AUTO_INCREMENT PRIMARY KEY comment '用户ID', person_name varchar(30) comment '用户名称')
这里演示插入10000条:
- 创立insert 接口
- 设置collection并执行
3、执行结束,查看表数量
调用表行数查问接口查问后果:
申明:xmysql工具次要用于测试环境,联合接口测试等,次要用于内部测试,在生产还是不倡议应用,因为这款工具会造成重大的数据安全问题。
参考文档:
https://gitee.com/thinkyoung/...
Enjoy GreatSQL :)
文章举荐:
面向金融级利用的GreatSQL正式开源
https://mp.weixin.qq.com/s/cI...
Changes in GreatSQL 8.0.25 (2021-8-18)
https://mp.weixin.qq.com/s/qc...
MGR及GreatSQL资源汇总
https://mp.weixin.qq.com/s/qX...
GreatSQL MGR FAQ
https://mp.weixin.qq.com/s/J6...
在Linux下源码编译装置GreatSQL/MySQL
https://mp.weixin.qq.com/s/WZ...
# 对于 GreatSQL
GreatSQL是由万里数据库保护的MySQL分支,专一于晋升MGR可靠性及性能,反对InnoDB并行查问个性,是实用于金融级利用的MySQL分支版本。
Gitee:
https://gitee.com/GreatSQL/Gr...
GitHub:
https://github.com/GreatSQL/G...
Bilibili:
https://space.bilibili.com/13...
微信&QQ群:
可搜寻增加GreatSQL社区助手微信好友,发送验证信息“加群”退出GreatSQL/MGR交换微信群
QQ群:533341697
微信小助手:wanlidbc
本文由博客一文多发平台 OpenWrite 公布!