Python读取NGINX日志将其存入数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python读取NGINX日志将其存入数据库相关的知识,希望对你有一定的参考价值。
日志数据:
#/usr/bin/env python #-*-coding:UTF-8 -*- from datetime import datetime stat_days = [] import pymysql #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) #2018-05-25 22:23:44 #print(datetime.now().strftime("%d/%b-%Y %H:%M:%S")) #25/May-2018 22:23:44格式 #print(datetime.strptime('17/Jun/2017:12:11:16',"%d/%b/%Y:%H:%M:%S")) #格式转换 connect=pymysql.connect( host='127.0.0.1', port=3306, user='root', password='', database='nginxlog', charset="utf8" ) cur = connect.cursor() sql= "insert into nginxlog(ip,time,methods,source,protocol,status) values(%s,%s,%s,%s,%s,%s)" with open("portal_ssl.access.log","r") as ngfile: for line in ngfile: _nodes = line.split() IP= _nodes[0] Time= _nodes[3][1:-1].replace(":"," ",1) #将时间转换为17/Jun/2017 12:43:4格式 Time = datetime.strptime(Time,"%d/%b/%Y %H:%M:%S")#将时间格式化为2017-06-17 12:43:04 Methods = _nodes[5][1:] Source = _nodes[6] Protocol = _nodes[7][:-1] Status = _nodes[8] print(IP,Time,Methods,Source,Protocol,Status) cur.execute(sql,(IP,Time,Methods,Source,Protocol,Status)) connect.commit() connect.close()
SQL语句
create database if not exists nginxlog default charset utf8; create table nginxlog ( id int(10) primary key auto_increment, ip varchar(30) not null default '', time datetime, methods varchar(10) not null default '', source varchar(200) not null default '', protocol varchar(10) not null default '', status varchar(10) not null default '' ) alter table nginxlog modify column source varchar(400);
以上是关于Python读取NGINX日志将其存入数据库的主要内容,如果未能解决你的问题,请参考以下文章
python 读取大文本文件并存入numpy时过于费时的问题及猜测