😸

踏み台経由でアクセスする MySQL へ ssh トンネルを掘る

に公開

昔散々やったが、令和になってもこんな場面があるんだなという...

手でやる場合

$ ssh -i ~/.ssh/foobar.pem foobar-user@bastion.example.com

してから

$ mysql -u root -h mysql-host.example.com -p

的なことをしているケース

ssh_config に設定

ssh_config をかいて

$ cat ~/.ssh/config

Host mysql-host
  HostName bastion.example.com
  User foobar-user
  LocalForward 23306 mysql-host.example.com:3306
  IdentityFile ~/.ssh/foobar.pem

トンネルを掘ってmysql接続

トンネルを掘る

ssh -f -N mysql-host

-f: バックグラウンド実行
-N: リモートでコマンドなどを起動しない(シェルなどを起動しない)

ポートのLISTEN状況の確認

$ lsof -iTCP:23306

プロセス確認

$ pgrep -fl mysql-host

mysql 接続

$ mysql -uroot -h 127.0.0.1 -P 23306 -p

トンネルをkill

kill

$ pkill -f mysql-host

Discussion