Quickly write Apache access logs to the mysql database in real time

Source: Internet
Author: User
Tags datetime apache log create database mysql database

Add the following content to the apache configuration file or the virtual host configuration file (that is, the configuration of LogFormat and CustomLog:

LogFormat "insert into apachelog (ID, dateTime, IP, URL, code, referer, userAgent, size, request) VALUES (NULL, \ "% {% Y-% m-% d % H: % M: % S} t \", \ "% a \", \ "% U \", \ "%> s \", \ "% {Referer} I \", \ "% {User-Agent} I \", % B, % T); "sqlcomm
CustomLog "|/usr/bin/mysql-user = root-password ="-database = apachealog "sqlcomm


Create a database and a data table in the Mysql database:


Mysql> create database apachealog;
Mysql> show create table apachealog;
Create table 'apacheog '(
'Id' int (11) not null AUTO_INCREMENT,
'Datetime' dateTime not null,
'IP' varchar (15) not null,
'URL' varchar (256) not null,
'Code' varchar (3) not null,
'Referer' varchar (256) not null,
'Useragent' varchar (256) not null,
'Size' int (11) not null,
'Request' float not null,
Primary key ('id '),
KEY 'datetime' ('datetime ')
) ENGINE = MyISAM AUTO_INCREMENT = 40 default charset = latin1;


Implementation ideas:


First, use the LogFormat settings to convert the log to a format close to the SQL, and then call pipe through CustomLog to write data to mysql.
Note: Apache log format can refer to the official documentation: http://httpd.apache.org/docs/2.2/mod/mod_log_config.html # formats, you can also use SetEnvIf with regular expressions to filter out such as images, CSS, JS and other relatively unimportant information.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.