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?


In reply to Reading host from defaults file with DBD::mysql by Llew_Llaw_Gyffes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.