Skip to content

Commit 18a8290

Browse files
committed
MDEV-23015: mariadb-setpermission seems incompatible with DBI:MariaDB
- 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
1 parent 9636b7c commit 18a8290

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

scripts/mysql_setpermission.sh

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,21 @@ my $sqlhost = "";
5656
my $user = "";
5757

5858
$dbh=$host=$opt_user= $opt_password= $opt_help= $opt_host= $opt_socket= "";
59-
$opt_port=0;
59+
$opt_port=3306;
6060

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

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

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

67+
## User may have put the port with the host.
68+
69+
if ($opt_host =~ s/:(\d+)$//)
70+
{
71+
$opt_port = $1;
72+
}
73+
6774
if ($opt_host eq '')
6875
{
6976
$sqlhost = "localhost";
@@ -84,10 +91,30 @@ if ($opt_password eq '')
8491
print "\n";
8592
}
8693

94+
## Socket takes precedence.
95+
my $dsn;
96+
my $prefix= 'mysql';
97+
98+
if (eval {DBI->install_driver("MariaDB")}) {
99+
$dsn ="DBI:MariaDB:;";
100+
$prefix= 'mariadb';
101+
}
102+
else {
103+
$dsn = "DBI:mysql:;";
104+
}
105+
106+
if ($opt_socket and -S $opt_socket)
107+
{
108+
$dsn .= "${prefix}_socket=$opt_socket";
109+
}
110+
else
111+
{
112+
$dsn .= "host=$sqlhost;port=$opt_port";
113+
}
87114

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

92119
# the start of the program
93120
&q1();
@@ -195,7 +222,8 @@ sub setpwd
195222
{
196223
$pass = "PASSWORD(". $dbh->quote($pass) . ")";
197224
}
198-
my $sth = $dbh->prepare("update user set Password=$pass where User = $user and Host = $host") || die $dbh->errstr;
225+
my $uh= "$user@$host";
226+
my $sth = $dbh->prepare("set password for $uh =$pass") || die $dbh->errstr;
199227
$sth->execute || die $dbh->errstr;
200228
$sth->finish;
201229
print "The password is set for user $user.\n\n";
@@ -403,7 +431,7 @@ sub user
403431
chomp($answer);
404432
if ($answer)
405433
{
406-
my $sth = $dbh->prepare("select User from user where User = '$answer'") || die $dbh->errstr;
434+
my $sth = $dbh->prepare("select User from mysql.user where User = '$answer'") || die $dbh->errstr;
407435
$sth->execute || die $dbh->errstr;
408436
my @r = $sth->fetchrow_array;
409437
if ($r[0])
@@ -538,7 +566,7 @@ sub hosts
538566
print "We now need to know which host for $user we have to change.\n";
539567
print "Choose from the following hosts: \n";
540568
$user = $dbh->quote($user);
541-
my $sth = $dbh->prepare("select Host,User from user where User = $user") || die $dbh->errstr;
569+
my $sth = $dbh->prepare("select Host,User from mysql.user where User = $user") || die $dbh->errstr;
542570
$sth->execute || die $dbh->errstr;
543571
while (my @r = $sth->fetchrow_array)
544572
{
@@ -551,7 +579,7 @@ sub hosts
551579
chomp($answer);
552580
if ($answer)
553581
{
554-
$sth = $dbh->prepare("select Host,User from user where Host = '$answer' and User = $user") || die $dbh->errstr;
582+
$sth = $dbh->prepare("select Host,User from mysql.user where Host = '$answer' and User = $user") || die $dbh->errstr;
555583
$sth->execute || die $dbh->errstr;
556584
my @r = $sth->fetchrow_array;
557585
if ($r[0])
@@ -597,8 +625,10 @@ sub read_my_cnf
597625
{
598626
if (/^\[(client|perl)\]/i)
599627
{
628+
print "Options read from mycnf:\n";
600629
while ((defined($_=<TMP>)) && !/^\[\w+\]/)
601630
{
631+
next if /^\s*($|#)/; ## skip blanks and comments
602632
print $_;
603633
if (/^host\s*=\s*(\S+)/i)
604634
{
@@ -621,6 +651,7 @@ sub read_my_cnf
621651
$opt_socket = $1;
622652
}
623653
}
654+
print "------------------------\n";
624655
}
625656
}
626657
close(TMP);

0 commit comments

Comments
 (0)