Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Tunneling DBD::mysql connections over SSH without using external programs

by Corion (Patriarch)
on Apr 11, 2013 at 20:16 UTC ( [id://1028225]=note: print w/replies, xml ) Need Help??


in reply to Tunneling DBD::mysql connections over SSH without using external programs

This is not a Perl solution, but if the remote end has an ssh daemon running, you can use SSH tunneling to connect to the remote database:

ssh -fNg -L 6603:127.0.0.1:3306 remote_user@mysql.example.com

Then you connecto to localhost:6603, and the communication should get forwarded to the remote machine.

To do this transparently from Perl, I would launch ssh in the background like this:

my $ssh_pid= open("ssh -fNg -L 6603:127.0.0.1:3306 remote_user@mysql.e +xample.com |") or die; my $dbh= DBI->connect("dbd:mysql..."); ... # tear down the connection kill -9 => $ssh_pid;

Replies are listed 'Best First'.
Re^2: Tunneling DBD::mysql connections over SSH without using external programs
by wwinfrey (Acolyte) on Apr 11, 2013 at 21:30 UTC

    Right, I am very familiar with that technique. However, if you'll note from my post, my design goals dictate that I establish the tunneled connection without the use of exec() or open() calls to system binaries like the SSH client binary (/usr/bin/ssh) or socat.

    I am not looking for how to do this from the shell; I am looking to do this completely from within the confines of my Perl application, mostly for portability issues, but also because this "application" I'm working on will be implemented as a custom module in an API I am designing.

    Dependence on external programs in this situation seems sloppy and hard to maintain. I am looking for my fellow developers to simply "use" my module and not have to worry about installing anything else besides other Perl modules.

      Then, there is only a realistic way to do that: write your own ssh implementation using Net::SSH2 able to redirect connections on a TCP port to the remote MySQL port trough SSH.

      Then in your module run this proxy in a new thread or process.

        I concur, Salva, that this is the approach I need to take. I'm more inclined to use Net::OpenSSH as it seems the most portable (and, as was pointed out by Krambambuli below, all the other modules are wrappers to one degree or another for external binaries). In my response below I distilled the issue down to "How do you make DBI connect with DBD::mysql over Net::OpenSSH tunnels ?" and I suppose that's the crux of the challenge.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1028225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2024-04-23 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found