@@ -46,6 +46,7 @@ existing higher-level protocol implementation.
4646 * [ Plain TCP connections] ( #plain-tcp-connections )
4747 * [ Secure TLS connections] ( #secure-tls-connections )
4848 * [ HTTP requests] ( #http-requests )
49+ * [ Database tunnel] ( #database-tunnel )
4950 * [ Connection timeout] ( #connection-timeout )
5051 * [ DNS resolution] ( #dns-resolution )
5152 * [ Password authentication] ( #password-authentication )
@@ -323,6 +324,51 @@ When using the `SshSocksConnector` (recommended), this works for both plain HTTP
323324and TLS-encrypted HTTPS requests. When using the ` SshProcessConnector ` , this only
324325works for plaintext HTTP requests.
325326
327+ ### Database tunnel
328+
329+ We should now have a basic understanding of how we can tunnel any TCP/IP-based
330+ protocol over an SSH proxy server. Besides using this to access "external"
331+ services, this is also particularly useful because it allows you to access
332+ network services otherwise only local to this SSH server from the outside, such
333+ as a firewalled database server.
334+
335+ For example, this allows us to combine an
336+ [ async MySQL database client] ( https://github.com/friends-of-reactphp/mysql ) and
337+ the above SSH proxy server setup, so we can access a firewalled MySQL database
338+ server through an SSH tunnel. Here's the gist:
339+
340+ ``` php
341+ $loop = React\EventLoop\Factory::create();
342+ $proxy = new Clue\React\SshProxy\SshProcessConnector('user@example.com', $loop);
343+
344+ $uri = 'test:test@localhost/test';
345+ $factory = new React\MySQL\Factory($loop, $proxy);
346+ $connection = $factory->createLazyConnection($uri);
347+
348+ $connection->query('SELECT * FROM book')->then(
349+ function (QueryResult $command) {
350+ echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
351+ },
352+ function (Exception $error) {
353+ echo 'Error: ' . $error->getMessage() . PHP_EOL;
354+ }
355+ );
356+
357+ $connection->quit();
358+
359+ $loop->run();
360+ ```
361+
362+ See also [ example #21 ] ( examples ) for more details.
363+
364+ This example will automatically launch the ` ssh ` client binary to create the
365+ connection to a database server that can not otherwise be accessed from the
366+ outside. From the perspective of the database server, this looks just like a
367+ regular, local connection. From this code's perspective, this will create a
368+ regular, local connection which just happens to use a secure SSH tunnel to
369+ transport this to a remote server, so you can send any query like you would to a
370+ local database server.
371+
326372### Connection timeout
327373
328374By default, neither the ` SshProcessConnector ` nor the ` SshSocksConnector ` implement
0 commit comments