summaryrefslogtreecommitdiffstats
path: root/new-mysql-user.pl
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2013-09-05 17:38:50 +0200
committerFlorian Pritz <bluewind@xinu.at>2013-09-05 17:38:50 +0200
commitb74120596dc1748db6b0af789a4e653256d976e4 (patch)
tree2af7dc6c9b3cb18b6faa5e946fa0d96e007b51e1 /new-mysql-user.pl
parent463068b43f573364bf8893da3fdd640414a88523 (diff)
downloadbin-b74120596dc1748db6b0af789a4e653256d976e4.tar.gz
bin-b74120596dc1748db6b0af789a4e653256d976e4.tar.xz
Add new-mysql-user.pl
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'new-mysql-user.pl')
-rwxr-xr-xnew-mysql-user.pl35
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";
+