SSH Port FowardでMySQLを使う

はてなブックマーク - SSH Port FowardでMySQLを使う

気づいたら年が明けていますが俺は元気です。

タイトル通りなんだけど、あるMySQLサーバへつなぐためには踏み台を通らないといけなくて、いちいち

[sourcecode language=“bash” padlinenumbers=“2”] localhost$ ssh -t gateway ssh dbserver dbserver$ mysql -uhoge -p


とかやらないといけなくて、めんどい。というかこれだとローカルの開発環境で、一時的にdbserverで動作してるMySQLのデータを使いたいときとか困る。ダンプしてローカルに持ってくればいいけど、データがものすごいデカかったりしてだるい。どうにかしたい。

というわけで、Port Forwardすればいいじゃないかと思って

[sourcecode language="bash" padlinenumbers="2"]
$ ssh -Nf -L 13306:dbserver:3306 gateway

とかして、

[sourcecode language=“text” padlinenumbers=“2”] $ mysql -uhoge -p -P13306


これで勝つる!!!と思ったら、これでは繋がらない。でもtelnet localhost 13306とか叩いてみると繋がるように見える。

[sourcecode language="text" padlinenumbers="2"]
$ telnet localhost 13306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
L
5.5.8-log
         $;2)s.%n!
                  ?QV;dgXbeUZBymysql_native_password

なにこれどういうことなの、と思って色々試したり調べたりしていたら、答えは意外なところに。

What is SSH Port Forwarding and How It Worksってページに書いてあった。

When the remote SSH host is on a different machine than the MySQL server, replace 127.0.0.1 with the IP address of the MySQL server (relative to the SSH host). Never use "localhost" for hostname.

Never use “localhost” for hostname、だと……??

[sourcecode language=“text” padlinenumbers=“2”] $ mysql -uhoge -p -P13306 -h127.0.0.1 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1058649 Server version: 5.5.8-log MySQL Community Server (GPL)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> ```

をををを……そしてSSH ポートフォワードで MySQL サーバにログインするときのメモ - 酔いどれコード に同じことが書いてあった。damn。localhost指定(-hオプションなしのデフォルト)だと、Unixドメインソケットを使って通信するので、当然ネットワーク越しには繋がらないよというオチでした。2時間くらいハマってしまったのでメモ。


comments powered by Disqus