Files
2026-08-10 16:12:09 +02:00

44 lines
1.1 KiB
Perl
Executable File

#!/usr/bin/perl
########################################################################## mysql/mariadb user manager (mwx'2021)
use DBI;
use Getopt::Std;getopts('hd:x');
$VERSION='4.0.0';
&help() if ($ARGV[0]=~/^\s*$/ || $opt_h);
$USER=$DB=$ARGV[0];
$DB=$opt_d if ($opt_d!~/^\s*$/);
$PW=sprintf("$USER%03d",int(rand(100000)));
my $dsn = "DBI:mysql:mysql;mysql_read_default_file=$ENV{HOME}/.my.cnf";
$dbh=DBI->connect($dsn,undef,undef,{PrintError=>1});
&sqldo("CREATE DATABASE IF NOT EXISTS $DB") if (!$opt_x);
&sqldo("DROP USER IF EXISTS '$USER'");
&sqldo("CREATE USER '$USER' IDENTIFIED BY '$PW'");
&sqldo("GRANT ALL PRIVILEGES ON $DB.* TO '$USER'");
sub sqldo() {
my($sql)=@_;
print "$sql\n";
$dbh->do($sql);
}
sub help() {
print <<"ENDOFHELP";
myuser - mariadb user manager (v$VERSION)
myuser <NAME> add user and database with the same name to mariadb
myuser -d <DBNAME> <NAME> add user and database to mariadb
myuser -x <NAME> add only user to mariadb
ENDOFHELP
exit();
}
############################################################################################################ END