diff options
Diffstat (limited to 'new-mysql-user.pl')
-rwxr-xr-x | new-mysql-user.pl | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/new-mysql-user.pl b/new-mysql-user.pl new file mode 100755 index 0000000..555cefd --- /dev/null +++ b/new-mysql-user.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +use strict; +use warnings; +use DBI; +use File::Basename; + +if (@ARGV != 1) { + print "usage: ", basename($0), " <username>\n"; + exit 1; +} + +sub random_string { + my $length = shift; + my @chars = ("A".."Z", "a".."z", 0..9); + my $string; + $string .= $chars[rand @chars] for 1..$length; + return $string; +} + +my $user = $ARGV[0]; +my $password = random_string(15); + +my @dsn = ('DBI:mysql:mysql_read_default_file='.$ENV{HOME}.'/.my.cnf'); +my $db = DBI->connect(@dsn) || exit 1; + +my $result; + +$db->do("CREATE USER '$user'\@'localhost' IDENTIFIED BY '*';"); +$db->do("GRANT USAGE ON *.* TO '$user'\@'localhost' IDENTIFIED BY '$password' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;"); +$db->do("CREATE DATABASE IF NOT EXISTS `$user`;"); +$db->do("GRANT ALL PRIVILEGES ON `$user`.* TO '$user'\@'localhost';"); +$db->do("GRANT ALL PRIVILEGES ON `${user}_%`.* TO '$user'\@'localhost';"); +$db->disconnect(); +print "Password for user $user: $password\n"; + |