跳至内容

我如何使用 Amazon RDS 代理连接到我的 Amazon RDS for MySQL 数据库实例或 Aurora MySQL 兼容版数据库集群?

2 分钟阅读
0

我想使用 Amazon Relational Database Service (Amazon RDS) 代理连接到我的 Amazon RDS for MySQL 数据库实例或 Amazon Aurora MySQL 兼容版数据库集群。

解决方案

**先决条件:**您的代理必须与数据库位于同一虚拟私有云 (VPC) 中。您可以将数据库设置为可公开访问,但不能将代理设置为可公开访问。

创建代理并将其连接到您的数据库实例

完成以下步骤:

  1. 在 AWS Secrets Manager 中创建数据库凭证
    **注意:**创建密钥时,请使用与数据库相同的用户名和密码。

  2. (可选)创建 AWS Identity and Access Management (IAM) 策略和 IAM 角色
    **注意:**Amazon RDS 代理将自动创建 IAM 角色。但是,您可以创建自己的 IAM 角色。

  3. 创建代理

  4. 运行以下命令,以检查是否可访问代理端点:

    nc -zv proxy_endpoint 3306

    **注意:**请将 proxy_endpoint 替换为您的代理服务器的地址。仅在与代理位于同一 VPC 中的 Amazon Elastic Compute Cloud (Amazon EC2) 计算机上运行上述命令。该命令不在本地计算机上运行。
    示例输出:

    Connection to test-proxy.proxy-#############.eu-west-1.rds.amazonaws.com 3306 port [tcp/mysql] succeeded
  5. 运行以下命令,以使用代理连接到 RDS 数据库实例:

    mysql -h proxy_endpoint -u username -p

    **注意:**请将 proxy_endpoint 替换为您的代理服务器的地址。
    示例输出:

    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>

(可选)使用启用了 TLS/SSL 连接的代理

完成以下步骤:

  1. 创建代理时,打开 Require Transport Layer Security(需要传输层安全)。您也可以修改现有代理
    **注意:**当您使用 TLS/SSL 协议时,代理会拒绝纯文本连接。

  2. 从 Amazon Trust Services 下载 Amazon 根 CA 1 信任存储 .pem 文件:

    wget https://www.amazontrust.com/repository/AmazonRootCA1.pem
  3. 运行以下命令,以使用 TLS/SSL 连接到数据库实例

    mysql -h test-proxy.proxy-#############.eu-west-1.rds.amazonaws.com -u admin --ssl-mode=REQUIRED --ssl-ca=AmazonRootCA1.pem -p

    或者,您可以使用 TLS/SSL VERIFY_IDENTITY 模式运行以下命令来连接到数据库实例:

    mysql -h test-proxy.proxy-#############.eu-west-1.rds.amazonaws.com -u admin --ssl-mode=VERIFY_IDENTITY --ssl-ca=AmazonRootCA1.pem -p

    **注意:**代理使用通配符证书。如果您使用 MySQL 客户端连接 SSL/TLS 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.

相关信息

RDS 代理故障排除

Amazon RDS 代理