From b74120596dc1748db6b0af789a4e653256d976e4 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 5 Sep 2013 17:38:50 +0200 Subject: Add new-mysql-user.pl Signed-off-by: Florian Pritz --- new-mysql-user.pl | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 new-mysql-user.pl (limited to 'new-mysql-user.pl') 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), " \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"; + -- cgit v1.2.3-24-g4f1b