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

I'm embarrased to ask this question but what is the syntax for connecting to a database on a different server?

This is what I have tried:

my $dbh = DBI->connect("DBI:mysql:'dbname:hostname'","","") or die "ERROR 1: $DBI::errstr; stopped";

but I get this error -
Unknown MySQL Server Host 'hostname'

I've tried fully qualified hostname and IP as well.

When I tried 'hostname:dbname' I got Unknown MySQL Server Host 'dbname' so obviously THAT isn't right. Duh.

Any help would be very appreciated. Thanks.

goosefairy

Replies are listed 'Best First'.
Re: Connect to DB on different server
by tcf22 (Priest) on Sep 09, 2003 at 18:18 UTC
    Try this (directly from DBD::mysql docs on CPAN)
    use strict; use warnings; use DBI; my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port"; my $dbh = DBI->connect($dsn, $user, $password);


    - Tom

      Thanks, I had forgotten all about CPAN. That did the trick.

      goosefairy
        Not only are the docs on CPAN, but the DBD::mysql perldocs come with DBD::mysql when you install it. After installing DBD::mysql, just do
        perldoc DBD::mysql
        at a command prompt.

        This is true of all(?) the modules available on CPAN. HTH.
Re: Connect to DB on different server
by Mr. Muskrat (Canon) on Sep 09, 2003 at 18:17 UTC
    Lose the single quotes around the database and host names.