Useful when migrating. Point at source DB instance, retrieve grants using this script, pipe through the Add a semicolon in PERL script (below), and voila, recreate grants on destination instance.
#!/bin/bash
(
mysql --batch --skip-column-names -e "SELECT user, host FROM user" mysql
) | while read user host
do
echo "# $user @ $host"
mysql --batch --skip-column-names -e"SHOW GRANTS FOR '$user'@'$host'"
done
Quickie to add a semicolon to end of non-commented lines (useful paired with the SHOW GRANTS bash script)
#!/usr/bin/perl --
open DATA, "$ARGV[0]";
while () {
$liner=$_;
if ("$liner" =~ /^#/) {
print $liner;
} else {
chomp($liner);
print "$liner" . ";n";
}
}
close DATA;