Skip to content

Commit

Permalink
MDEV-23015: mariadb-setpermission seems incompatible with DBI:MariaDB
Browse files Browse the repository at this point in the history
- Commit 5cc2096 introduced in `10.5` changed DBI:DBD to DBD:MariaDB in this case with redudant `mysql` option.
- According to database handle (dbh) and `connect` method one should follow
  https://metacpan.org/pod/DBD::MariaDB#Class-Methods with proper created data source name (dsn).
- Adding socket precedance over port.
- Adding skipping the comments when reading the `my.cnf` file.
- MDEV-23016: mariadb-setpermission included
  • Loading branch information
an3l committed Apr 13, 2021
1 parent 9636b7c commit 18a8290
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions scripts/mysql_setpermission.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,21 @@ my $sqlhost = "";
my $user = "";

$dbh=$host=$opt_user= $opt_password= $opt_help= $opt_host= $opt_socket= "";
$opt_port=0;
$opt_port=3306;

read_my_cnf(); # Read options from ~/.my.cnf

GetOptions("user=s","password=s","help","host=s","socket=s","port=i");

usage() if ($opt_help); # the help function

## User may have put the port with the host.

if ($opt_host =~ s/:(\d+)$//)
{
$opt_port = $1;
}

if ($opt_host eq '')
{
$sqlhost = "localhost";
Expand All @@ -84,10 +91,30 @@ if ($opt_password eq '')
print "\n";
}

## Socket takes precedence.
my $dsn;
my $prefix= 'mysql';

if (eval {DBI->install_driver("MariaDB")}) {
$dsn ="DBI:MariaDB:;";
$prefix= 'mariadb';
}
else {
$dsn = "DBI:mysql:;";
}

if ($opt_socket and -S $opt_socket)
{
$dsn .= "${prefix}_socket=$opt_socket";
}
else
{
$dsn .= "host=$sqlhost;port=$opt_port";
}

# make the connection to MariaDB
$dbh= DBI->connect("DBI:MariaDB:mysql:host=$sqlhost:port=$opt_port:mariadb_socket=$opt_socket",$opt_user,$opt_password, {PrintError => 0}) ||
die("Can't make a connection to the mysql server.\n The error: $DBI::errstr");
$dbh= DBI->connect($dsn,$opt_user,$opt_password, { RaiseError => 1, PrintError => 0}) ||
die("Can't make a connection to the MariaDB server.\n The error: $DBI::errstr");

# the start of the program
&q1();
Expand Down Expand Up @@ -195,7 +222,8 @@ sub setpwd
{
$pass = "PASSWORD(". $dbh->quote($pass) . ")";
}
my $sth = $dbh->prepare("update user set Password=$pass where User = $user and Host = $host") || die $dbh->errstr;
my $uh= "$user@$host";
my $sth = $dbh->prepare("set password for $uh =$pass") || die $dbh->errstr;
$sth->execute || die $dbh->errstr;
$sth->finish;
print "The password is set for user $user.\n\n";
Expand Down Expand Up @@ -403,7 +431,7 @@ sub user
chomp($answer);
if ($answer)
{
my $sth = $dbh->prepare("select User from user where User = '$answer'") || die $dbh->errstr;
my $sth = $dbh->prepare("select User from mysql.user where User = '$answer'") || die $dbh->errstr;
$sth->execute || die $dbh->errstr;
my @r = $sth->fetchrow_array;
if ($r[0])
Expand Down Expand Up @@ -538,7 +566,7 @@ sub hosts
print "We now need to know which host for $user we have to change.\n";
print "Choose from the following hosts: \n";
$user = $dbh->quote($user);
my $sth = $dbh->prepare("select Host,User from user where User = $user") || die $dbh->errstr;
my $sth = $dbh->prepare("select Host,User from mysql.user where User = $user") || die $dbh->errstr;
$sth->execute || die $dbh->errstr;
while (my @r = $sth->fetchrow_array)
{
Expand All @@ -551,7 +579,7 @@ sub hosts
chomp($answer);
if ($answer)
{
$sth = $dbh->prepare("select Host,User from user where Host = '$answer' and User = $user") || die $dbh->errstr;
$sth = $dbh->prepare("select Host,User from mysql.user where Host = '$answer' and User = $user") || die $dbh->errstr;
$sth->execute || die $dbh->errstr;
my @r = $sth->fetchrow_array;
if ($r[0])
Expand Down Expand Up @@ -597,8 +625,10 @@ sub read_my_cnf
{
if (/^\[(client|perl)\]/i)
{
print "Options read from mycnf:\n";
while ((defined($_=<TMP>)) && !/^\[\w+\]/)
{
next if /^\s*($|#)/; ## skip blanks and comments
print $_;
if (/^host\s*=\s*(\S+)/i)
{
Expand All @@ -621,6 +651,7 @@ sub read_my_cnf
$opt_socket = $1;
}
}
print "------------------------\n";
}
}
close(TMP);
Expand Down

0 comments on commit 18a8290

Please sign in to comment.