in reply to Using Net::SSH2 with DBI

I find shocking that it is still not possible to pass an already open socket to the DBD::mysql constructor!

Because the right way to do connect to a remote DB through a SSH gateway would be to call socketpair, attach the command ssh $remote_host -W${db_host}:${db_port} to one side and then pass the other to the DBD::mysql.

Replies are listed 'Best First'.
Re^2: Using Net::SSH2 with DBI
by oiskuu (Hermit) on Jan 21, 2016 at 12:45 UTC

    Here's some skeleton code to try. I haven't tested it much, but it seems to connect alright.

    #! /usr/bin/perl use strict; use warnings; use Socket; use POSIX; sub named_socket_spawn { my $code = shift; ref $code eq 'CODE' or return; my ($serv, $conn); socket($serv, AF_UNIX, SOCK_STREAM, PF_UNSPEC) || die "socket: $!"; my $sname = tmpnam(); bind($serv, sockaddr_un($sname)) || die "bind: $!"; listen($serv, 1) || die "listen: $!"; my $pid = fork() // die "fork: $!"; if ($pid) { close $serv; return $sname; } accept($conn, $serv) || warn "accept: $!"; unlink($sname) || warn "unlink: $!"; open STDIN, "<&", $conn; open STDOUT, ">&", $conn; close $serv; close $conn; exit $code->(); } use DBD::mysql; #my $sqlsock = named_socket_spawn(sub{exec "ssh host -W host:3306"}); my $sqlsock = named_socket_spawn(sub{ exec "nc localhost 3306" }); my $dsn = "DBI:mysql:database=test;mysql_socket=$sqlsock"; my $dbh = DBI->connect($dsn, "test", "hunter5");

Re^2: Using Net::SSH2 with DBI
by Anonymous Monk on Jan 20, 2016 at 15:26 UTC

    Hm. According to DBD::mysql, there's the mysql_socket specifier to pass the unix socket name. Does this not work?

    So basically, you'd need a small helper sub to handle the tedium of attaching a process to a socket

    my $unixname = named_sock_spawn(sub{ exec "ssh" }); sub named_sock_spawn { socket();bind();listen(); fork() and return $name; accept();dup();dup();$fun->(); }

    The perlipc has an example on unix-domain TCP. But are there any modules to provide named pipe utilities?

      This is only for a socket name unfortunately, not for an in-memory socket structure.

        It's the socket name that the mysql client will use to connect to the server. No?