如何使用 Amazon RDS 代理连接到我的 Amazon RDS MySQL 数据库实例或 Aurora MySQL 数据库集群?
上次更新时间: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 数据库实例:
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>
您也可以按照以下步骤,使用 Amazon RDS 代理和 SSL 连接连接到 RDS 数据库实例:
1. (可选)在创建 Amazon RDS 代理时,启用需要传输层安全性。您也可以修改现有 RDS 代理,以启用此选项。
注意:将此参数更改为必填意味着连接必须使用 SSL。任何纯文本连接都将被拒绝。如果不启用参数,则无论是否存在 SSL,Amazon RDS 代理都能连接到 RDS 数据库实例。
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.