From 473c7216eee7f7969d6594ad795e13414b0e1601 Mon Sep 17 00:00:00 2001 From: Monika Komorowska <mk8@sanger.ac.uk> Date: Wed, 18 May 2011 10:31:20 +0000 Subject: [PATCH] Script for killing mysql processes by db name pattern. --- misc-scripts/db/kill_process_by_db.pl | 81 +++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 misc-scripts/db/kill_process_by_db.pl diff --git a/misc-scripts/db/kill_process_by_db.pl b/misc-scripts/db/kill_process_by_db.pl new file mode 100644 index 0000000000..bf66128685 --- /dev/null +++ b/misc-scripts/db/kill_process_by_db.pl @@ -0,0 +1,81 @@ +# kill processes by db pattern +# Author: Monika Komorowska +# Date : May 2011 + + +use strict; +use DBI; + +use Getopt::Long; + +my ( $host, $user, $pass, $port, $dbpattern ); + +GetOptions( "host|h=s", \$host, + "user|u=s", \$user, + "pass|p=s", \$pass, + "port|P=i", \$port, + "dbpattern|pattern=s", \$dbpattern, + ); + +if( !$host || !$user || !$pass || !$dbpattern ) { + usage(); +} + +my $dsn = "DBI:mysql:host=$host"; +if( $port ) { + $dsn .= ";port=$port"; +} + +my $db = DBI->connect( $dsn, $user, $pass ); + +my $sth=$db->prepare("SHOW FULL PROCESSLIST") || die $DBI::err.": ".$DBI::errstr; + +$sth->execute || die DBI::err.": ".$DBI::errstr; + +my $ref; + +print "Id \t User \t Host \t db \t Command \t Time \t State \t Info\n"; +my @proc_ids; + +while ( $ref = $sth->fetchrow_hashref() ) { + if ( $$ref{'db'} =~ /$dbpattern/ ) { + push (@proc_ids, $$ref{'Id'}); + print "$$ref{'Id'} \t $$ref{'Host'} \t $$ref{'db'} \t $$ref{'Command'} \t $$ref{'Time'} \t $$ref{'State'} \t $$ref{'Info'}\n"; + } +} + +my $proc_id_count = @proc_ids; +if ($proc_id_count > 0) { + print "Are you sure you want to kill process(es) listed above? (y/n)\n"; +} else { + print "No processes found for db pattern $dbpattern\n"; +} +my $decision = <>; + +if ($decision == 'y') { + my $killed_count = 0; + foreach my $proc_id (@proc_ids) { + if ( $db->do("KILL $proc_id") ) { + $killed_count ++; + } else { print $DBI::errstr; } + } + + print "$killed_count procesess were killed\n"; + +} + + +sub usage { + print STDERR <<EOF + + Usage: kill_process_by_db options + Where options are: -host hostname + -user username + -pass password + -port port_of_server optional + -dbpattern regular expression that the database name has to match +EOF +; + exit; +} + -- GitLab