我想将fifo管道中的数据插入到mysql表中
我的脚本如下:
#!/usr/bin/perl
#Script to read data out of a named pipe and write to MySQL database.
$| = 1;
use strict;
use DBI();
my $filename;
my $inputline;
my $linenumber;
my @arr;
$filename = "./SEC_fifo";
open(FIFO, "+< $filename") or die "FIFO error on $filename $!";
my $dbh = DBI->connect("DBI:mysql:database=ecdb;host=localhost",
"user", "[pwd]",
{'RaiseError' => 1});
while (<FIFO>)
{
$inputline = $_;
@arr = split(/,/,$inputline);
# Quit read loop when requested.
last if($inputline =~ /quit/i);
chop $inputline;
$linenumber++;
print "Got: [$inputline], ";
my $sql="";
my $sth="";
my @row;
print "output.\n".$arr[0],$arr[1],$arr[2],$arr[3],$arr[4],$arr[5],$arr[6],$arr[7],$arr[8],$arr[9],$arr[10],$arr[11],$arr[12],$arr[13]."\n";
# perl trim function - remove leading and trailing whitespace
my $str = $arr[6] ;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
if($str ne 'Normal')
{
print "arr[6]=".$arr[6]."\n";
$sql = "select Hid from Devices where hostname = '$arr[2]'";
print $sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql
\n";
@row=$sth->fetchrow_array;
my $hid1=@row[0];
$sql = "select Hid from Devices where hostname = '$arr[3]'";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $hid2=@row[0];
$sql = "select Eid from Event where Eventname = '$arr[8]' and severity = '$arr[9]' and Trapoid='$arr[10]'";
print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $eid1=@row[0];
#print "eid1=".@row[0]."\n";
$sql = "select Eid from Event where Eventname = '$arr[11]' and severity = '$arr[12]' and Trapoid='$arr[13]'";
#print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $eid2=@row[0];
print "eid1=".$eid1."\n";
print "eid2=".@row[0]."\n";
$sql = "insert into secresult (Hid_1,Hid_2,interface_1,interface_2,ifindex_1,ifindex_2,Eid_1,Eid_2,start_time_1,start_time_2,effect_range,description) values ($hid1,$hid2,'$arr[4]','$arr[5]',$arr[6],$arr[7],$eid1,$eid2,'$arr[0]','$arr[1]','$arr[14]','$arr[15]')";
print $sql."\n";
$dbh->do($sql);
print "inserted it.\n";
}
else
{
$sql = "select Hid from Devices where hostname = '$arr[1]'";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $hid=@row[0];
$sql = "select Eid from Event where trapoid = '$arr[0]' and severity != 'Normal'";
#print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $eid=@row[0];
$sql = "select id from (SELECT * FROM secresult WHERE end_time_1 ='' and Hid_1=$hid and interface_1 ='$arr[4]' and ifindex_1=$arr[5] ORDER BY start_time_1 DESC LIMIT 1) result where result.eid_1 = $eid";
print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $id=@row[0];
if($id !="")
{
$sql = "update secresult set end_time_1 = '$arr[2]' where id = '$id'";
print $sql."\n";
$dbh->do($sql);
print "updated it.\n";
}
else
{
$sql = "select id from (SELECT * FROM secresult WHERE end_time_2 ='' and Hid_1=$hid and interface_2 ='$arr[4]' and ifindex_2=$arr[5] ORDER BY start_time_2 DESC LIMIT 1) result where result.eid_2 = $eid";
print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
$id=@row[0];
$sql = "update secresult set end_time_2 = '$arr[2]' where id = '$id'";
print $sql."\n";
$dbh->do($sql);
print "updated it.\n";
}
}
}
my $sth = $dbh->prepare("SELECT * FROM secresult"); $sth->execute(); while (my $ref = $sth->fetchrow_hashref()) {
print "Found a row: id = $ref->{'id'}, line = $ref->{'textline'}\n"; }
$sth->finish();
$dbh->disconnect();
exit;
我可以通过脚本插入到数据库中,但是当我执行脚本来监听时,几分钟后从fifo获取数据,脚本将显示错误消息“DBD :: mysql :: st execute failed:MySQL服务器已经离开./Db_code.pl第66行,第5行“
第66行是“$ sth-&gt; execute || die”无法执行SQL语句...可能无效? \ n \ n $ sql \ n“;”
答案 0 :(得分:6)
在任何数据库操作之前确保dbh仍然连接(ping)并重新连接(或克隆)
while (<FIFO>)
{
if ( ! $dbh->ping ) {
$dbh = $dbh->clone() or die "cannot connect to db";
}
...
} #end FIFO
答案 1 :(得分:5)
作为替代方案,您可以使用mysql自动重新连接功能。
$dbh->{mysql_auto_reconnect} = 1;
答案 2 :(得分:1)
导致此错误的主要原因是客户端由于某种原因无法访问服务器。它可以在服务器超时并关闭连接后发生。
在这种情况下,你必须收集一些统计信息来检查你的MySQL服务器是否很慢,并检查你的SQL语句。
在这里,您可以参考官方Mysql文档中的错误文档:MySQL Server has gone away。