jjzjj

mysql - DBD::mysql::st 执行失败:MySQL 服务器已离开

coder 2023-10-14 原文

我想从一个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->execute || die”Could not execute SQL statement ... maybe invalid?\n\n $sql\n";"

最佳答案

在任何数据库操作和重新连接(或克隆)之前确保 dbh 仍然连接(ping)

while (<FIFO>)
{
    if ( ! $dbh->ping ) {
        $dbh = $dbh->clone() or die "cannot connect to db";
    }

...

} #end FIFO

另见 http://www.perlmonks.org/?node_id=497849

关于mysql - DBD::mysql::st 执行失败:MySQL 服务器已离开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23866132/

有关mysql - DBD::mysql::st 执行失败:MySQL 服务器已离开的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  4. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  5. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  6. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

  7. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  8. ruby-on-rails - 在 Rails 中调试生产服务器 - 2

    您如何在Rails中的实时服务器上进行有效调试,无论是在测试版/生产服务器上?我试过直接在服务器上修改文件,然后重启应用,但是修改好像没有生效,或者需要很长时间(缓存?)我也试过在本地做“脚本/服务器生产”,但是那很慢另一种选择是编码和部署,但效率很低。有人对他们如何有效地做到这一点有任何见解吗? 最佳答案 我会回答你的问题,即使我不同意这种热修补服务器代码的方式:)首先,你真的确定你已经重启了服务器吗?您可以通过跟踪日志文件来检查它。您更改的代码显示的View可能会被缓存。缓存页面位于tmp/cache文件夹下。您可以尝试手动删除

  9. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  10. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

随机推荐