Script to dump grants for all mysql users. Password is encrypted, use to copy
and paste users from one mysql server to another. [download here](http://files.technomage.net/dump_grants.pl.txt)
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use English qw( -no_match_vars );
use Carp;
my $dsn = q{DBI:mysql:database=mysql};
my $dbh = DBI->connect( $dsn, 'root', 'PASSWORD' )
or croak qq{ERROR: $DBI::errstr};
my $grants_sql = q{SELECT CONCAT("SHOW GRANTS FOR '",user,"'@'",host,"';")
as query
FROM mysql.user
WHERE host='localhost'};
my $grants_sth = $dbh->prepare( $grants_sql )
or croak qq{ERROR: $DBI::errstr};
$grants_sth->execute or croak qq{ERROR: $DBI::errstr};
while ( my ($grant_query) = $grants_sth->fetchrow_array ) {
# SHOW GRANTS FOR 'zo00143_RW'@'localhost';
my $sth = $dbh->prepare( $grant_query )
or croak qq{ERROR: $DBI::errstr};
$sth->execute or croak qq{ERROR: $DBI::errstr};
while ( my ($grant) = $sth->fetchrow_array ) {
print qq{$grant;\n};
}
$sth->finish or croak qq{ERROR: $DBI::errstr};
}
$grants_sth->finish or croak qq{ERROR: $DBI::errstr};
$dbh->disconnect;