Skip to content

Instantly share code, notes, and snippets.

@icot
Created June 25, 2013 15:31
Show Gist options
  • Save icot/5859436 to your computer and use it in GitHub Desktop.
Save icot/5859436 to your computer and use it in GitHub Desktop.
Find and kill queries which have been running for more than an hour in MySQL (Perl)
#!/usr/bin/env perl
use DBI;
use warnings;
# DB Connection
my $dsn = "DBI:mysql:information_schema:dbod-testinstance:5500:mysql_read_default_file=$ENV{HOME}/.my.cnf";
$dbh = DBI->connect($dsn,undef, undef, {RaiseError => 1})
or die "Connection Error: $DBI::errstr\n";
# Fetch Offending ID's
my $sql = "select id from processlist where time > 3600 and command=\'query\'";
my $sth = $dbh->prepare($sql);
$sth->execute() or die "SQL Error: $DBI::errstr\n";
my $ids = $sth->fetchrow_arrayref();
# Killing loop
for my $id (@{$ids}) {
$sql = "kill $id";
$sth->prepare($sql);
$sth->execute() or die "SQL Error: $DBI::errstr\n";
}
$sth->finish();
$dbh->disconnect();
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment