@@ -56,14 +56,21 @@ my $sqlhost = "";
56
56
my $user = " " ;
57
57
58
58
$dbh =$host =$opt_user = $opt_password = $opt_help = $opt_host = $opt_socket = " " ;
59
- $opt_port =0 ;
59
+ $opt_port =3306 ;
60
60
61
61
read_my_cnf(); # Read options from ~/.my.cnf
62
62
63
63
GetOptions(" user=s" ," password=s" ," help" ," host=s" ," socket=s" ," port=i" );
64
64
65
65
usage() if ($opt_help ); # the help function
66
66
67
+ # # User may have put the port with the host.
68
+
69
+ if ($opt_host =~ s / :(\d +)$// )
70
+ {
71
+ $opt_port = $1 ;
72
+ }
73
+
67
74
if ($opt_host eq ' ' )
68
75
{
69
76
$sqlhost = " localhost" ;
@@ -84,10 +91,30 @@ if ($opt_password eq '')
84
91
print " \n " ;
85
92
}
86
93
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
+ }
87
114
88
115
# 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 " );
91
118
92
119
# the start of the program
93
120
&q1();
@@ -195,7 +222,8 @@ sub setpwd
195
222
{
196
223
$pass = " PASSWORD(" . $dbh -> quote($pass ) . " )" ;
197
224
}
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;
199
227
$sth -> execute || die $dbh -> errstr;
200
228
$sth -> finish;
201
229
print " The password is set for user $user .\n\n " ;
@@ -403,7 +431,7 @@ sub user
403
431
chomp ($answer );
404
432
if ($answer )
405
433
{
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;
407
435
$sth -> execute || die $dbh -> errstr;
408
436
my @r = $sth -> fetchrow_array;
409
437
if ($r [0])
@@ -538,7 +566,7 @@ sub hosts
538
566
print " We now need to know which host for $user we have to change.\n " ;
539
567
print " Choose from the following hosts: \n " ;
540
568
$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;
542
570
$sth -> execute || die $dbh -> errstr;
543
571
while (my @r = $sth -> fetchrow_array)
544
572
{
@@ -551,7 +579,7 @@ sub hosts
551
579
chomp ($answer );
552
580
if ($answer )
553
581
{
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;
555
583
$sth -> execute || die $dbh -> errstr;
556
584
my @r = $sth -> fetchrow_array;
557
585
if ($r [0])
@@ -597,8 +625,10 @@ sub read_my_cnf
597
625
{
598
626
if (/ ^\[ (client|perl)\] /i )
599
627
{
628
+ print " Options read from mycnf:\n " ;
600
629
while ((defined ($_ =<TMP>)) && !/^\[\w+\]/)
601
630
{
631
+ next if / ^\s *($| #)/ ; # # skip blanks and comments
602
632
print $_ ;
603
633
if (/ ^host\s *=\s *(\S +)/i )
604
634
{
@@ -621,6 +651,7 @@ sub read_my_cnf
621
651
$opt_socket = $1 ;
622
652
}
623
653
}
654
+ print " ------------------------\n " ;
624
655
}
625
656
}
626
657
close (TMP);
0 commit comments