Created
February 24, 2016 06:38
-
-
Save mugifly/692a50dcd5426f42d323 to your computer and use it in GitHub Desktop.
MySQL Database and User Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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