English 中文(简体)
自动选定MySQL查询书
原标题:Autokill select MySQL queries perl script

我使用下文的文字来杀害用户进行的所有选定的链接,但我想对<>root用户作例外处理,因为根本的查询要么重要,要么是辅助性的。

下面是我的法典

#!/usr/bin/perl -w

use DBI;
use strict;

$| = 1;

#------------------------------------------------------------------------------
# !!! Configure check time and timeout. These are both in seconds.

my $check =      15;    # check processes every $check seconds
my $slow_time =  60;    # stop processes that run for >= $slow_time seconds

# !!! Configure log file - All slow queries also get logged to this file

my $logfile =    "./check_mysql_query.log"; # log slow queries to this file

# !!! Configure the database connection parameters

my $db_string = "dbi:mysql:mysql";  # DBI resource to connect to
my $db_user =   "root";     # DBI username to connect as
my $db_pass =   "password";     # DBI password to connect with

# !!! Configure path to sendmail program

my $sendmail_bin = "/usr/sbin/sendmail";

#
#------------------------------------------------------------------------------


my ($dbh,$sth,$sth2,$thread,$state,$time,$query,$explain);

print "connecting
";
my $opt = {
     RaiseError =>0,
     PrintError =>0
};
$dbh = DBI->connect($db_string,$db_user,$db_pass,$opt);
unless ($dbh) {
    print "Error: Unable to connect to database: $DBI::errstr
";
    exit 1;
}

$SIG{ TERM } = sub {
    print "caught sig TERM!
exiting!
";
    $dbh->disconnect;
    exit 1;
};

print "preparing
";
unless ($sth = $dbh->prepare("show full processlist")) {
    print "error preparing query: $DBI::errstr
exiting!
";
    $dbh->disconnect;
    exit 1;
}

print "initialized.. starting loop
";
while(1) {
    unless ($sth->execute) {
        print "statement execute failed: ".$sth->errstr."
exiting!
";
        last;
    }
    while(my @tmp = $sth->fetchrow) {
        $thread = $tmp[0];
        $state = $tmp[4];
        $time = $tmp[5];
        $query = $tmp[7];
        if ($state eq "Query" && $query =~ /^SELECT/ && $time >= $slow_time) {
            print "killing slow query thread=$thread state=$state time=$time
";
            $dbh->do("kill $thread");
            unless (log_query($logfile,$query)) {
                print "log_query failed! exiting!
";
                last;
            }


        }
    }
    sleep($check);
}

$sth->finish;
$dbh->disconnect;

exit 1;


sub log_query {
    my ($file,$query) = @_;
    unless (open(O,">>".$file)) {
        print "error opening log file  $file : $!
";
        return undef;
    }
    print O $query."
-----
";
    close(O);
    return 1;
}

script credit: http://code.google.com/p/mysql-killquery/ i removed most of the script code for simplicity.

最佳回答

我认为,我从我的阅读中了解到,你的第二栏是用户。 因此,作为用户根基将原木输入数据库,因此,你可能这样做:

while(my @tmp = $sth->fetchrow) {
    next if $tmp[1] eq  root ;
    ...
}
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签