The Perl Toolchain Summit 2025 Needs You: You can help 🙏 Learn more

Build Status Coverage Status

NAME

MySQL::GrantParser - parse SHOW GRANTS and return as hash reference

SYNOPSIS

# connect with existing dbh
my $dbh = DBI->connect(...);
my $grant_parser = MySQL::GrantParser->new(
dbh => $dbh;
);
# connect with user, password
my $grant_parser = MySQL::GrantParser->new(
user => 'root',
password => 'toor',
hostname => '127.0.0.1',
);
# and parse!
my $grants = $grant_parser->parse; # => HashRef

DESCRIPTION

MySQL::GrantParser is SHOW GRANTS parser for MySQL, inspired by Ruby's Gratan.

This module returns privileges for all users as following hash reference.

{
'USER@HOST' => {
'user' => USER,
'host' => HOST,
'objects' => {
'DB_NAME.TABLE_NAME' => {
privs => [ PRIV_TYPE, PRIV_TYPE, ... ],
with => 'GRANT OPTION',
},
...
},
'options' => {
'identified' => '...',
'required' => '...',
},
},
{
...
},
}

For example, this GRANT statement

GRANT SELECT, INSERT, UPDATE, DELETE ON orcl.* TO 'scott'@'%' IDENTIFIED BY 'tiger' WITH GRANT OPTION;

is represented as following.

{
'scott@%' => {
user => 'scott',
host => '%',
objects => {
'*.*' => {
privs => [
'USAGE'
],
with => '',
},
'`orcl`.*' => {
privs => [
'SELECT',
'INSERT',
'UPDATE',
'DELETE',
],
with => 'GRANT OPTION',
}
},
options => {
identified => "PASSWORD XXX",
required => '',
},
},
}

METHODS

Class Methods

new(%args:Hash) :MySQL::GrantParser

Creates and returns a new MySQL::GrantParser instance. Dies on errors.

%args is following:

dbh => DBI:db

Database handle object.

user => Str
password => Str
hostname => Str
socket => Str

Path of UNIX domain socket for connecting.

Mandatory arguments are dbh or hostname or socket.

Instance Methods

parse() :HashRef

Parse privileges and return as hash reference.

AUTHOR

HIROSE Masaaki <hirose31@gmail.com>

REPOSITORY

https://github.com/hirose31/MySQL-GrantParser

patches and collaborators are welcome.

SEE ALSO

Gratan, http://dev.mysql.com/doc/refman/5.6/en/grant.html

COPYRIGHT

Copyright HIROSE Masaaki

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.