LoginSignup
8
3

More than 5 years have passed since last update.

踏み台経由でMySQLにアクセスする方法、多段sshトンネリング

Posted at

多段sshトンネリングしてみるw

踏み台経由でMySQLにアクセスする方法、sshトンネリング で作った環境で、多段sshトンネリングしてみる。メリットは、php, java で直接MySQLにアクセスできるようになるので(PDOで、sshトンネリングする方法が判らなかっただけですw)いいかなと。

image

矢印が間違っていたら指摘してくださいw

windowsのコマンドでssh使うお

コマンドラインで「ssh」が使えればいいので、手っ取り早いのは msysgit をインスコする。open-ssh 入るなうw

で、Git Bash を立ち上げます(Cygwinみたいだけど)。ssh コマンドを使えるか確認しておきます。

段階的にコマンドを実行する

192.168.11.119:22 <--> localhost:30022 にsshトンネリングする。

$ ssh -L 30022:192.168.11.119:22 -fN vagrant@192.168.11.118
vagrant@192.168.11.118's password:

192.168.11.121:3306 <--> localhost:30023 にsshトンネリングする。

$ ssh -L 30023:192.168.11.121:3306 -fN -p 30022 vagrant@localhost
vagrant@localhost's password:

A5:SQL Mk-2 でアクセスしてみる

image

きたー

PDO 経由でもw

phpで当然のことながらw

$dsn = 'mysql:dbname=mysql;host=localhost;port=30023';
$user = 'root';
$password = '';

try{
    $dbh = new PDO($dsn, $user, $password);
}catch (PDOException $e){
    print('Error:'.$e->getMessage());
    die();
}

できたー

参考にさせて頂きました

多段ローカルポートフォワード(PostgreSQL接続)
teratermを使用した多段ssh転送Add Star

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3