Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MySQL Database and User Generator
#!/usr/bin/env perl
print "MySQL Database and User Generator\n";
print "Input database name: ";
my $db_name = <STDIN>;
chomp $db_name;
if (length($db_name) <= 0) {
exit 0;
}
print "\nProcesses:\n";
print "* Make a database: ${db_name}\n";
print "* Grant priviledges to ${db_name}@localhost for ${db_name}\n\n";
my @sqls = (
'CREATE USER \''. $db_name . '\'@\'localhost\' IDENTIFIED BY \'' . $db_name .'\';',
'GRANT ALL PRIVILEGES ON `'. $db_name . '`.* TO \'' . $db_name . '\'@\'localhost\';'
);
print "Commands:\n";
foreach (@sqls) {
print "* $_\n";
}
print "\nWould you execute above commands? (y/n): ";
my $ans = <STDIN>;
chomp($ans);
if (length($ans) <= 0 || $ans !~ /^y$/i) {
exit 0;
}
print "\n\nPlease input mysql root password: \n";
my $mysql_root_pw = <STDIN>;
chomp($ans);
my $fh = undef;
open($fh, "|mysql -u root -p${mysql_root_pw}")
|| die 'Cant execute mysql command: '. $!;
foreach (@sqls) {
print $fh $_;
}
close $fh;
print "\nDone.";
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.