今回は下記を参考にさせていただき、
EC2からRDSへの疎通確認をcurlコマンドでやってみました。
書かれている新川さん。
面識はないですが、以下の記事が素敵だなと思い、
書かれているものを探っていたところ、今回のノウハウ記事に出会いました。
DBクライアントがいらないのが便利
元記事に詳細は記載されていますが、今回DBクライアントが不要なところ、
設定内容を確認する術として、curlコマンドを使用されているのがポイントでした。
ほとんどの場合、telnetが一般的かと思うので。
さっそく自分でやってみて挙動を確認。
・EC2インスタンス、RDS(MySQL)を用意
同じVPC内にEC2インスタンス、RDS(MySQL)を用意
まずは、セキュリティグループを設定しないで疎通が取れないパターンを確認
$ curl -v telnet://database-1. xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306
結果
"test" 0L, 0C 0,0-1 全て * Rebuilt URL to: telnet://database-1.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306/ * Trying 10.1.15.128... * TCP_NODELAY set * connect to 10.1.15.128 port 3306 failed: Connection timed out * Failed to connect to database-1. xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com port 3306: Connection timed out * Closing connection 0 curl: (7) Failed to connect to database-1. xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com port 3306: Connection timed out [ec2-user@ip-10-1-11-117 ~]$
接続が失敗している挙動を確認。
続いて、セキュリティグループを設定した状態で再度実施
[ec2-user@ip-10-1-11-117 ~]$ curl -v telnet://database-1.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306 * Rebuilt URL to: telnet://database-1.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306/ * Trying 10.1.15.128... * TCP_NODELAY set * Connected to database-1.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com (10.1.15.128) port 3306 (#0) Warning: Binary output can mess up your terminal. Use "--output -" to tell Warning: curl to output it to your terminal anyway, or consider "--output Warning: <FILE>" to save to a file. * Failed writing body (0 != 25) * Closing connection 0 [ec2-user@ip-10-1-11-117 ~]$
接続自体はできているものの、Warningが出ている
バイナリ出力させるのはあかんで!ってところかな
なので、オプションでoutputしてみる
[ec2-user@ip-10-1-11-117 ~]$ curl -v --output test telnet://database-1.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306 * Rebuilt URL to: telnet://database-1.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306/ % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 10.1.15.128... * TCP_NODELAY set * Connected to database-1.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com (10.1.15.128) port 3306 (#0) * RCVD IAC 193 ※ここでCtrl + c を押下 [ec2-user@ip-10-1-11-117 ~]$
きれいに接続を確認
しかし、outputに指定したtestファイルには何も記載がされていない
このへんの挙動がちょっと謎だが、接続自体を確認する目的は果たしたのでよしとする
curl コマンドについては使えば便利なことが多く、
もう少し仲良くなっていきたいところ。
焦らずひとつひとつ試していきましょ。