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

Hello monks, How can i connect to database on remote server. I'm able to connect to the database on my server using the DBI connect. But how can i get connected to a different server from a perl script. Do I need to use remote login inside my perl script or is there any other connection mechanism that i can use? Thanks in advance for your replies.
  • Comment on How can i connect to database on remote server

Replies are listed 'Best First'.
Re: How can i connect to database on remote server
by jbrugger (Parson) on Nov 07, 2005 at 12:38 UTC
    Remember you need the server where the database is running to accept remote connections (eg 3306 for mysql).
    my $mysql_server = 192.168.1.100; my $mysql_user = "mysqlUser"; my $mysql_password = "mysqlPass"; my $mysql_db = "yourDatabase"; $DBH::conn = DBI->connect("DBI:mysql:$mysql_db;$mysql_server",$mysql_u +ser,$mysql_password,{ PrintError => 1, RaiseError => 0,) || die $DBI: +:errstr; return $DBH::conn;
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: How can i connect to database on remote server
by Perl Mouse (Chaplain) on Nov 07, 2005 at 12:40 UTC
    First you make sure that the client libraries you use for your database are configured correctly - for some database, it's just a matter of installing the libraries, others might need to a mapping of symbolic names to ip numbers and ports.

    Then make sure you can actually connect to the remote server using an interactive tool (most database have at least one interactive tool).

    Then you change the connection string you use to create a database handle when using the DBI. Depending on the database server, this might require changes in hostname, port, databasename, username and/or password.

    Perl --((8:>*
Re: How can i connect to database on remote server
by McDarren (Abbot) on Nov 07, 2005 at 13:42 UTC
    If you are connecting to a remote server across the public internet, then you might want to consider the security issues - especially if the data you are dealing with is sensitive/confidential in nature.

    One way to reduce the risk is to establish a connection across an ssh tunnel. At my workplace we use a homegrown library for this purpose which makes use of IPC::Open2. (The code for this is quite old - about 4-5 years - but it works, so we continue to use it). There are quite possibly newer modules available these days that take care of this task, so it's probably worth your while having a fossick about on CPAN.

    Cheers,
    --Darren
Re: How can i connect to database on remote server
by jZed (Prior) on Nov 07, 2005 at 16:46 UTC
    It depends a bit on which DBMS/DBD you are using. Some, like MySQL, allow you to open a database handle directly to a remote server. With others you may need to use a combination of DBI::ProxyServer and DBD::Proxy, both part of the DBI distribution.