#!/usr/local/bin/perl
# script: mysqllog
## The mysqllog Program Relays Log Entries from Apache to mySQL

use DBI;

use constant DSN       => 'dbi:mysql:www';
use constant DB_TABLE  => 'access_log';
use constant DB_USER   => 'nobody';
use constant DB_PASSWD => 'not-the-real-password';

$PATTERN = '"([^"]+)" (\S+) "(\S+) (\S+) [^"]+" (\S+) "([^"]+)" "([^"]+)" (\d+) 
(\S+)';

$db = DBI->connect(DSN,DB_USER,DB_PASSWD) || die DBI->errstr;
$sth = $db->prepare("INSERT INTO ${\DB_TABLE} VALUES(?,?,?,?,?,?,?,?,?)") 
  || die $db->errstr;
while (<>) {
  chomp;
  my($date,$host,$method,$url,$user,$browser,$referrer,$status,$bytes)
        = map { $_ eq '-' ? undef : $_} /$PATTERN/o;
  $sth-
>execute($date,$host,$method,$url,$user,$browser,$referrer,$status,$bytes)
    || die $sth->errstr;
}
$sth->finish;
$db->disconnect;

---