Skip to content

Instantly share code, notes, and snippets.

@agramajo
Last active August 29, 2015 14:19
Show Gist options
  • Save agramajo/df456b6ec2baff3377cb to your computer and use it in GitHub Desktop.
Save agramajo/df456b6ec2baff3377cb to your computer and use it in GitHub Desktop.
show mysql grants
use DBI;
my $dbname = $ARGV[0] || "mysql";
my $dbuser = $ARGV[1] || "root";
my $dbpass = $ARGV[2] || "pass";
my $dbhost = $ARGV[3] || "localhost";
my $socket = "";
$dsn = "DBI:mysql:database=$dbname;host=$dbhost;mysql_socket=$socket";
$dbh = DBI->connect($dsn, $dbuser, $dbpass);
$sth = $dbh->prepare("select user,host from mysql.user");
$sth->execute;
while (my @r = $sth->fetchrow_array) {
my $user = $r[0];
my $host = $r[1];
my @vec;
$sth2 = $dbh->prepare("show grants for \'$user\'@\'$host\'");
$sth2->execute;
while (my @r2 = $sth2->fetchrow_array) {
push @vec, $r2[0];
}
print "user=$user host=$host grants=\n\t";
print join "\n\t", @vec;
print "\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment