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 | |
by Llew_Llaw_Gyffes (Scribe) on Jan 30, 2019 at 13:44 UTC | |
by pryrt (Abbot) on Jan 30, 2019 at 14:57 UTC | |
by Llew_Llaw_Gyffes (Scribe) on Jan 31, 2019 at 19:01 UTC | |
by pryrt (Abbot) on Feb 01, 2019 at 15:28 UTC | |
| |
by Anonymous Monk on Feb 01, 2019 at 05:05 UTC | |
|
Re: Reading host from defaults file with DBD::mysql
by Danny (Chaplain) on Jul 25, 2024 at 18:56 UTC |