Amazon RDS プロキシを使用して、Amazon RDS MySQL DB インスタンスまたは Aurora MySQL DB クラスターに接続する方法を教えてください。
最終更新日: 2020 年 9 月 21 日
簡単な説明
Amazon RDS プロキシを使って、アプリケーションへの接続を管理できます。RDS プロキシでサポートされているエンジンの詳細については、Amazon RDS プロキシとの接続を管理するを参照してください。
解決方法
開始する前に、Amazon RDS プロキシはデータベースと同じ VPC に存在している必要があります。データベースをパブリックにアクセス可能にすることはできますが、プロキシはパブリックにアクセス可能にすることはできません。
1. AWS Secrets Manager でデータベース認証情報を作成します。
注: シークレットを作成するときは、データベースと同じユーザー名とパスワードを使用してください。
2. AWS Identity and Access Management (IAM) ポリシーと IAM ロールを作成します。
注意: この手順はオプションです。Amazon RDS プロキシは、ステップ 3 で IAM ロールを自動的に作成できます。独自の IAM ロールを作成する場合は、この手順に従います。
4. プロキシエンドポイントに到達可能であることを確認します。
nc -zv <proxy_endpoint> 3306
このコマンドは以下のような出力を返します。
nc -z test-proxy.proxy-xxxxxxxxxxxxx.eu-west-1.rds.amazonaws.com 3306
Connection to test-proxy.proxy-xxxxxxxxxxxxx.eu-west-1.rds.amazonaws.com 3306 port [tcp/mysql] succeeded!
注意: このコマンドは、RDS プロキシと同じ VPC にある EC2 マシンでのみ実行できます。このコマンドは、ローカルマシンでは実行されません。
5. Amazon RDS プロキシを使用して RDS DB インスタンスに接続します。
mysql -h <proxy_end_point> -u username -p
このコマンドは以下のような出力を返します。
mysql -h test-proxy.proxy-xxxxxxxxxxxxx.eu-west-1.rds.amazonaws.com -u admin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2946664484
Server version: 5.7.28-log
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
または、以下の手順に従って、SSL 接続で Amazon RDS プロキシを使用して RDS DB インスタンスに接続できます。
1. オプションで、Amazon RDS プロキシの作成時に、[Require Transport Layer Security] (トランスポート層セキュリティを要求する) を有効にします。既存の RDS プロキシを変更して、このオプションを有効にすることもできます。
注意: パラメータを [Required] (要求済み) に変更すると、接続で SSL を使用する必要があります。プレーンテキスト接続はすべて拒否されます。このパラメータが有効でない場合、Amazon RDS プロキシは SSL の有無にかかわらず RDS DB インスタンスに接続できます。
2. Amazon Trust Services から Amazon ルート CA 1 信頼ストア .pem ファイルをダウンロードします。
wget https://www.amazontrust.com/repository/AmazonRootCA1.pem
3. SSL を使用して接続します。
mysql -h test-proxy.proxy-xxxxxxxxxxxxx.eu-west-1.rds.amazonaws.com -u admin --ssl-mode=REQUIRED --ssl-ca=AmazonRootCA1.pem -p
または、次のコマンドを実行して SSL を使って接続します。
mysql -h test-proxy.proxy-xxxxxxxxxxxxx.eu-west-1.rds.amazonaws.com -u admin --ssl-mode=VERIFY_IDENTITY --ssl-ca=AmazonRootCA1.pem -p
注: Amazon RDS プロキシはワイルドカード証明書を使用するため、MySQL クライアントを使用して SSL モード VERIFY_IDENTITY で接続する場合には、MySQL 8.0 互換 mysql コマンドを使用する必要があります。
接続すると、次の出力が返されます。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12659040
Server version: 5.7.28-log
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.