安装工具
功能: 读取生产环境的网络包或者 pcap 包文件,并解析网络包中的SQL执行语句和执行结果保存到本地
go install github.com/ssoor/mysql-replay@latest
使用方式
1 抓取生产环境的网络包
执行下面语句抓取网络包并存储在 pcaps/ens37.pcap 文件中
下面的命令指定了在网卡 ens37 上抓取协议为 tcp 并且端口为 3306 的网络包存储在 pcaps/ens37.pcap 文件中,具体参数可以查 tcpdump 的使用说明
tcpdump -i ens37 -w pcaps/ens37.pcap tcp and port 3306
2 解析网络包中的SQL执行语句和执行结果
通过下面语句解析网络包文件 ./pcaps/mysql.pcap 中的SQL执行语句和执行结果,最终结果会保存到 output/mysql_events.json 文件中
mysql-replay text replay --srcPort=3306 ./pcaps/mysql.pcap
3 检查SQL执行顺序和结果是否正确
最终结果输出格式如下:
{
"02e6266c799050c1": [
{
"conn": {
"dst": "192.168.4.198:3306",
"src": "192.168.4.195:56398"
},
"time": 1652943125032441000,
"type": "handshake",
"db": "config_test",
"username": "jn_cn",
"packet_res": []
},
{
"conn": {
"dst": "192.168.4.198:3306",
"src": "192.168.4.195:56398"
},
"time": 1652943125033208000,
"type": "query",
"query": "SET NAMES 'utf8'",
"packet_res": []
},
{
"conn": {
"dst": "192.168.4.198:3306",
"src": "192.168.4.195:56398"
},
"time": 1652943125033595000,
"type": "query",
"query": "SELECT * FROM `functionupgrade` WHERE (`organizationId`='39fd9286-f365-1620-574a-e4b7eb1a58ae') AND (`functionName`='禁用转介')",
"packet_res": []
},
{
"conn": {
"dst": "192.168.4.198:3306",
"src": "192.168.4.195:56398"
},
"time": 1652943125084277000,
"type": "query",
"query": "SELECT * FROM `p_mini_program` WHERE `mini_app_id`='wxefe5a0c8c09'",
"packet_res": [
{
"application_code": "5032",
"created_by": "400",
"created_on": "2021-11-12 11:33:19",
"id": "3a002461-c89c-fccd-c8ed-36511de5ad1e",
"mini_app_id": "wxefe5a0c8c09",
"modified_by": "400",
"modified_on": "2021-11-12 11:33:19",
"subject_id": "lojwvr162868"
}
]
}
],
"0735d71670b4ef5e": [
{
"conn": {
"dst": "192.168.4.198:3306",
"src": "192.168.4.195:56082"
},
"time": 1652943122225499000,
"type": "handshake",
"db": "config_test",
"username": "jtuan_cn",
"packet_res": []
},
{
"conn": {
"dst": "192.168.4.198:3306",
"src": "192.168.4.195:56082"
},
"time": 1652943122230773000,
"type": "query",
"query": "SET NAMES 'utf8'",
"packet_res": []
},
{
"conn": {
"dst": "192.168.4.198:3306",
"src": "192.168.4.195:56082"
},
"time": 1652943122233428000,
"type": "query",
"query": "SELECT * FROM `import_task` WHERE `status`=1 ORDER BY `created_on`",
"packet_res": []
},
{
"conn": {
"dst": "192.168.4.198:3306",
"src": "192.168.4.195:56082"
},
"time": 1652943122241219000,
"type": "quit",
"packet_res": []
}
]
}
具体含义如下:
{
"连接ID": [
{
"conn": {
"dst": "连接目标地址",
"src": "连接来源地址"
},
"type": "SQL类型",
"time": "SQL发生时间",
"db": "使用的数据库",
"username": "登录使用的用户名",
"query": "执行的SQL语句",
"stmtID": "预解析语句ID",
"package_res": "SQL执行的结果"
}
]
}
SQL类型:
- handshake=建立连接
- query=执行SQL查询
- stmt_prepare=预解析语句
- stmt_execute=执行预解析语句
- stmt_close=删除预解析语句
- quit=关闭连接
|