jmeter连接mysql,保存数据到txt
2021-03-11 本文已影响0人
陈芝麻烂谷子的事
1.jmeter如果要连接mysql 需要首先下载JDBC驱动,下载路径:https://dev.mysql.com/downloads/file/?id=477058,进入之后直接点击如如所示的位置下载即可:

2.下载完成后将文件解压,可以看到有一个jar包,如图所示,将这个包放入jmeter的/lib路径下即可

3.接下来打开jmeter 分别添加 JDBC Connection Configuration 和 JDBC Request


4.配置 JDBC Connection Configuration 的内容:
(1)Variable name created pool : 变量名,需要与JDBC request中varriable name of pool declared in jdbc connection configuration的名字相同。我理解的是将两个元件关联。内容可以任意填写。
(2)然后填写Database Connection Configuration 信息:Database URl是数据库地址,精确到数据库名字即可
jdbc:mysql://172.41.*****:3306/dataname;JDBC Driver class 选择com.mysql.jdbc.Driver;Username为账号名;Password为数据库密码。其他位置默认不动即可

5.配置JDBC Request:
(1)varriable name of pool declared in jdbc connection configuration填写与JDBC Connection Configuration中Variable name created pool相同的名字即可。
(2)QueryType为sql类型:

(3)在输入框中可以编写sql,同样也可以使用${key}引入变量:

6.获取到返回的值:
在JDBC Request 中下方找到 Variable names 定义一个变量名可以接受返回的值,比如这里的id:

这个时候去请求就可以获取从数据库查询的数据了:

7.将数据写入txt中。
(1)新增一个BeanShell取样器。脚本内容如下:
FileWriter fstream=new FileWriter("D:\\IDs.txt",true);
BufferedWriter out=new BufferedWriter(fstream);
out.write(vars.get("id_1")"\n");
out.close();
fstream.close();
id_1指的是取出返回接口中的第一个值,同理id_2代表取第二个值。
8.整体脚本完成,请求即可将获取到的id存入txt中了
