#!/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 add user and database with the same name to mariadb myuser -d add user and database to mariadb myuser -x add only user to mariadb ENDOFHELP exit(); } ############################################################################################################ END