forked from clue/reactphp-ssh-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31-mysql-ssh-tunnel.php
More file actions
32 lines (26 loc) · 1.27 KB
/
31-mysql-ssh-tunnel.php
File metadata and controls
32 lines (26 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
// A more advanced example to show how a MySQL server can be accessed through an SSH tunnel.
// The SSH proxy can be given through the SSH_PROXY env and defaults to localhost otherwise.
// The MySQL server can be given through the MYSQL_LOGIN env and default to localhost otherwise.
//
// You can assign the SSH_PROXY and MYSQL_LOGIN environment and prefix this with
// a space to make sure your login credentials are not stored in your bash
// history like this:
//
// $ export SSH_PROXY=alice:password@example.com
// $ export MYSQL_LOGIN=user:password@localhost
// $ php examples/31-mysql-ssh-tunnel.php
//
// See also https://github.com/friends-of-reactphp/mysql
require __DIR__ . '/../vendor/autoload.php';
$url = getenv('SSH_PROXY') !== false ? getenv('SSH_PROXY') : 'alice@localhost';
$proxy = new Clue\React\SshProxy\SshProcessConnector($url);
$url = getenv('MYSQL_LOGIN') !== false ? getenv('MYSQL_LOGIN') : 'user:pass@localhost';
$factory = new React\MySQL\Factory(null, $proxy);
$client = $factory->createLazyConnection($url);
$client->query('SELECT * FROM (SELECT "foo" UNION SELECT "bar") data')->then(function (React\MySQL\QueryResult $query) {
var_dump($query->resultRows);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
$client->quit();