Created
February 29, 2016 16:57
-
-
Save blobaugh/8c4e7784f0b44a84c17e to your computer and use it in GitHub Desktop.
Who is connected to a mysql server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use DBI; | |
use 5.010; | |
use Getopt::Long; | |
# Set defaults | |
my $host = 'localhost'; | |
my $db = 'information_schema'; | |
my $user = ''; | |
my $pass = ''; | |
# Get commandline options | |
GetOptions ( | |
"host=s" => \$host, | |
"db=s" => \$db, | |
"user=s" => \$user, | |
"pass=s" => \$pass, | |
) or die ( "Valid options\n--host\n--user\n--password\n--db database" ); | |
# Create database connection | |
my $dbh = DBI->connect( "DBI:mysql:$db:$host", $user, $pass, {'PrintError'=>0} ) | |
or die "** Connection error!\nTry different options:\n--host\n--user\n--password\n--db database\n\n $DBI:: +++errstr\n"; | |
# Get the current processes to check user | |
my $sql = "show processlist"; | |
my $sth = $dbh->prepare($sql); | |
$sth->execute() or die "SQL error: $DBI::errstr\n"; | |
# Create an array of all the users | |
my %user; | |
while (my @row = $sth->fetchrow_array()) { | |
$user{$row[1]}++; | |
} | |
# Display User details | |
print "MySQL - Logged in users\n"; | |
print "=" x 15, "\n"; | |
for my $x (keys %user) { | |
print "$x - $user{$x} connections\n"; | |
} | |
print "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment