为了方便统计个客户访问后台状态等,决定吧access_log入库
例如:
{"remote_addr":"10.77.40.3, 119.xxx.xxx.149","local_addr":"100.xxx.xxx.124","user":"xxxx@xxxxx.com","time":"[08/Nov/2017:11:20:18 +0800]","url":"POST /api/v1/applicant/risk?source=vc1.2 HTTP/1.0","status":"200","body_bytes_sent":"133","request":"117"}
这样一条json格式的日志,我先把它变成sql语句
命令:
awk -F'"' '{print "insert test(remote_addr,local_addr,user,time,url,status,body_bytes_sent,request) value(\""$4"\",\""$8"\",\""$12"\",\""$16"\",\""$20"\",\""$24"\",\""$28"\",\""$32"\")",";"}' /root/access.txt > input.sql
数据库的准备:
create database test;
create table(remote_addr char(64),local_addr char(64),user char(64),time char(64),url char(64),status char(64),body_bytes_sent char(64),request char(64));
表里定义可根据自己需求更改。
导入数据:
mysql -uroot -pxxxxxxxxx test < /root/input.sql