Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How do you use Net::OpenSSH to query mysql database via ssh tunnel

by Anonymous Monk
on May 09, 2017 at 10:52 UTC ( [id://1189876]=note: print w/replies, xml ) Need Help??


in reply to How do you use Net::OpenSSH to query mysql database via ssh tunnel

Looks like you're opening a TCP tunnel, TCP != UNIX sockets. Try connecting to the MySQL DB via TCP.
  • Comment on Re: How do you use Net::OpenSSH to query mysql database via ssh tunnel

Replies are listed 'Best First'.
Re^2: How do you use Net::OpenSSH to query mysql database via ssh tunnel
by nysus (Parson) on May 09, 2017 at 10:58 UTC

    I'm not sure I follow. But here's why I'm trying to do it via an SSH tunnel: the database server I eventually want to connect to is locked down so that it only accepts queries from trusted IP addresses. I want a solution that will work from any machine.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      Your database client needs to connect to a local network port (127.0.0.1:12345).

      This local network port is forwarded by OpenSSH to a remote network port (127.0.0.1:12345 -> (openssh) -> 127.0.0.1:3306).

      The 127.0.0.1:3306 is on the remote server where MySQL runs.

      You cannot pass a socket variable in a string to the MySQL client library. You need to pass it as host:post.

      In my example, this is 127.0.0.1:12345.

        OK, that helped. So this code works:

        my $ssh = Net::OpenSSH->new('me@10.0.1.17'); my ($socket, $pid) = $ssh->open_tunnel('10.0.1.17', '3306'); my $dsn = "DBI:mysql:database=calendar;host=127.0.0.1:3306"; my $dbh = DBI->connect($dsn, 'user', 'password');
        UPDATE: I think this is just connecting me to my local database, not the remote one.

        Many thanks!

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks

      Wait, crap. I think I'm just connecting to my local database with my code above, not the remote one.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found