title: perl script to connect two databases at same time
body: <p>I am trying to connect to 2 databases on the same instance of MySQL from 1 Perl script.</p>
  
  <p>I am using this in a migration script where I am grabbing data from the original database and inserting it into the new one.</p>
  
  <p>Connecting to 1 database and then trying to initiate a second connection with the same user just changes the current database to the new one.</p>
  
  <pre><code>#!/usr/bin/perl
  
  use DBI;
  use strict;
  
  my $driver = "mysql";
  my $database1 = "db1";
  my $dsn1 = "DBI:$driver:database=$database1";
  my $userid = "userhead";
  my $password = "pwdhead";
  my $database2 = "db2";
  my $dsn2 = "DBI:$driver:database=$database2";
  
  my $dbh1 = DBI-&gt;connect($dsn1, $userid, $password ) or die $DBI::errstr;
  my $dbh2 = DBI-&gt;connect($dsn2, $userid, $password ) or die $DBI::errstr;
  
  my $sth = $dbh2-&gt;prepare("INSERT INTO Persons") $dbh1-&gt;prepare("SELECT *FROM Persons");
  $sth-&gt;execute() or die $DBI::errstr;
  print "Number of rows found :" + $sth-&gt;rows;
  </code></pre>
  
  <p>In the above example i am trying to copy from one database table to another datbase table. but i am getting error while running the script. Please help me out</p>