Re: How to extract db hostname from the $dbh handle?

[email protected] (Ken Youens-Clark)
Newsgroups perl.dbi.users
Message-ID <[email protected]>
On Thu, Mar 11, 2010 at 5:54 AM, Cosimo Streppone <[email protected]> wrote:
> I've been looking at how to do this for some time,
> but I seem to don't get it.
>
> If I have a $dbh handle, is it possible to know which
> hostname is that $dbh connected to?
>
> I know it's a bit stupid, because I'm supplying the
> hostname in the first place, but anyway...

Cosimo,

I use DBI::parse_dsn and then examine the "driver_dsn" string it
returns.  Since I pretty much only use MySQL, I know I can look for
the "host=foo" part of that:

my ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn)
                = DBI->parse_dsn( $dsn );

if ( !$driver_dsn ) {
    die "No driver DSN from '$dsn'\n";
}

my $db_host;
if ( $driver_dsn =~ /host=(\w+)/ ) {
    $db_host = $1;
}
else {
    die "Can't figure out db host from '$driver_dsn'\n";
}

HTH,

ky
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.