Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Reading host from defaults file with DBD::mysql

by Llew_Llaw_Gyffes (Scribe)
on Jan 30, 2019 at 03:03 UTC ( [id://1229142]=perlquestion: print w/replies, xml ) Need Help??

Llew_Llaw_Gyffes has asked for the wisdom of the Perl Monks concerning the following question:

Brethren,

I am using DBD::mysql and reading connection parameters from MySQL defaults files, and it is MOSTLY working. I've been doing it for years, with code similar to the following:

sub open_db { my ($dsn, $dbh, $defaults); $defaults = (defined $opts{defaults} && -f $opts{defaults}) ? $opts{defaults} : (-e $ENV{HOME}.'/.my.cnf') ? $ENV{HOME}.'/.my.cnf' : (-e '/root/.my.cnf') ? '/root/.my.cnf' : (-e './.my.cnf') ? './.my.cnf' : 'NOTFOUND'; die "No valid defaults file found!" if ($defaults eq 'NOTFOUND' && + !defined $opts{host}); $dsn = defined $opts{host} && defined $opts{port} ? sprintf("DBI:mysql:mysql:host=%s;port=%d;mysql_read_default +_file=%s;mysql_read_default_group=mysql;", $opts{host}, $opts{port}, $defaults) : defined $opts{host} ? sprintf("DBI:mysql:mysql:host=%s;mysql_read_default_file=%s +;mysql_read_default_group=mysql;", $opts{host}, $defaults) : defined $opts{socket} && -e $opts{socket} ? sprintf("DBI:mysql:mysql;mysql_read_default_file=%s;mysql_r +ead_default_group=mysql;mysql_socket=%s;", $defaults, $opts{socket}) : sprintf("DBI:mysql:mysql;mysql_read_default_file=%s;mysql_r +ead_default_group=mysql;", $defaults); $dbh = DBI->connect($dsn, undef, undef, {RaiseError => 1, AutoComm +it => 0}); $dbh->do(sprintf('SET SESSION wait_timeout = %d', $opts{interval} > 3500 ? $opts{interval} + 100 : +3600)) || die "Could not set session wait_timeout"; return ($dbh); }

Now, the one respect in which this is not working is that it ignores a host specified in the defaults file. Username is picked up correctly, password is picked up and used, host is ignored.

Can anyone tell me why this should be?

Replies are listed 'Best First'.
Re: Reading host from defaults file with DBD::mysql
by NetWallah (Canon) on Jan 30, 2019 at 04:05 UTC
    It's hard to tell without input data .

    Your best bet is to run this under the perl debugger:

    perl -d <your code file>
    and step through (s command in debugger), to see what path it takes, and examine variables as it steps through (x command in the debugger).

                    As a computer, I find your faith in technology amusing.

      Oh, I've traced through my code and I know that it's doing what it should and reading the correct section of the defaults file. The problem is that DBD::mysql does not appear to be passing along the host specification along with the username and password, and I'm wondering whether I have missed something in my usage. The documentation leads me to believe that it SHOULD be working, but it isn't.

        Have you gone through Basic debugging checklist? Sounds like you've now done part (using the debugger), but maybe not everything: Does your $opts{defaults} really contain what you think it does when it enters the sub you showed? If not, then the problem lies outside of what you've shown. If it does contain what you think it should, then give us a security-redacted version of $opt{defaults}, because we cannot debug your logic in that sub without knowing what's there. See also SSCCE, How to ask better questions using Test::More and sample data, and How do I change/delete my post?


        edit: add "everything": I had intended the "but maybe not: ..." to be one logical thought, separate from "you've now done part"... but when re-reading, it looked more like I was saying OP hadn't done even the running-the-debugger, which was not what I meant. sorry. /edit

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1229142]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found