ssh tunneling through a mysql dsn

From: Eric Lease Morgan <emorgan_at_nyob>
Date: Wed, 25 Jun 2008 08:59:33 -0400
To: CODE4LIB_at_LISTSERV.ND.EDU
Is there anyway to support SSH tunneling through a MySQL DSN?

I would like to open a database connection to remote host through  
Perl's DBI. The remote database is MySQL, but the server hosting the  
database does not allow outside connections. Instead the systems  
administrators suggest first setting up a local SSH tunnel, and then  
making connections to the host. Something this:

   $ ssh -T -L 3306:mysql.example.org:3306 username_at_login.example.org &
   $ mysql -h mysql.example.org

Alas, this option does not work for two reasons. First, I get prompted  
for my username after the first command and my shell crashes. Second,  
and more importantly, port 3306 is already in use on my local machine.  
The whole thing seems weird anyway.

Maybe I can configure tunneling in the DBD::mysql DSN? I see that  
DBD::mysql supports SSH, but I see no tunneling option:

   use DBI;
   $database = 'foo';
   $user     = 'bar';
   $password = 'baz';
   $host     = 'mysql.example.org';
   $dsn      = "DBI:mysql:database=$database;host=$host;mysql_ssl=1";
   $dbh      = DBI->connect( $dsn, $user, $password );

Am I missing something, or is there some trickery I can use with the  
definition of $host and/or $database?

-- 
Eric Lease Morgan
Head, Digital Access and Information Architecture Department
Hesburgh Libraries, University of Notre Dame

(574) 631-8604
Received on Wed Jun 25 2008 - 07:34:46 EDT