Tunnels PDO and SSH2

Is there a way to connect to the database using PDO and SSH tunnels and not execute any code on the command line, as in the next section?

Zend_Db: How to connect to MySQL database through SSH tunnel?

Thanks in advance for your reply.

+4
source share
3 answers

Without calling the ssh tunnel in a separate process, this means that you will need to create a new tunnel for each script call - and you will not be able to use the tunnel created by another instance, since you do not know when it will end. Thus, in addition to the connection overhead, you need to manage the pool of local sockets.

The short answer is that this is simply not possible.

The longer answer is that you can run daemons / long-running processes from PHP, but there are a few caveats . Therefore, if you can use the program execution functions and have access to the POSIX toolkit using command line tools, then this is possible. This will be much easier to implement if you can configure a key pair (with a private private key) to avoid the need for parsing I / O in the program, to enter the password at the right point (or use something like ' expect ' for processing).

+1
source

I was able to do this using a combination of ssh2_tunnel , socket_create_listen() and pcntl_fork . Basically, a tunnel is what we want to use to connect to a port in a MySQL box, but since PDO does not support a socket connection, we need to create a port through socket_create_listen on a dedicated php plug that simply transfers data from the unix socket for the tunnel ssh.

 PDO (php main proc) -> random socket port on 127.0.0.1 -> data proxy (php fork proc) -> ssh2_tunnel resource -> ssh server -> mysql process. 
+2
source

If you have php with ssh2 extension, this should be possible.

http://php.net/manual/en/function.ssh2-tunnel.php

0
source

All Articles