내용으로 건너뛰기

Linux 또는 macOS 머신에서 배스천 호스트를 사용해 Amazon RDS DB 인스턴스에 연결하려면 어떻게 해야 합니까?

3분 분량
0

공개적으로 액세스할 수 없는 Amazon Relational Database Service(Amazon RDS) DB 인스턴스가 있습니다. Linux 또는 macOS 머신에서 이 인스턴스에 연결하려고 합니다.

간략한 설명

프라이빗 Amazon RDS 또는 Amazon Aurora DB 인스턴스에 연결하려면 VPN 또는 AWS Direct Connect를 사용하는 것이 좋습니다. VPN 또는 AWS Direct Connect를 사용할 수 없으면 대신 배스천 호스트를 사용하면 됩니다. 이 방법은 Linux 또는 macOS 머신에서 인스턴스의 가상 프라이빗 클라우드(VPC) 외부로부터 Amazon Aurora Serverless 및 Amazon RDS Proxy로 연결하는 데에도 사용할 수 있습니다.

해결 방법

배스천 호스트를 사용해 Linux 또는 macOS 머신에서 Amazon RDS DB 인스턴스에 연결하려면 다음 단계를 따르십시오.

  1. Amazon RDS DB 인스턴스를 프라이빗으로 설정합니다. 이렇게 하려면 다음 파라미터로 DB 인스턴스를 수정하십시오.
    publicly accessible 파라미터를 no로 설정하고 프라이빗 서브넷을 사용합니다.
    라우팅 테이블에서 Internet gateway - igw를 사용하지 마십시오.
    DB가 모든 IP 주소에서 포트 5432 및 3306을 허용하도록 보안 그룹을 설정합니다.

  2. DB 인스턴스와 동일한 VPC에서 사용 가능한 가장 작은 EC2 인스턴스를 시작합니다.
    라우팅 테이블에서 Internet gateway - igw를 사용합니다. Amazon Elastic Compute Cloud(Amazon EC2) 인스턴스가 퍼블릭 서브넷에 있는 경우, 이 값이 해당 인스턴스를 인터넷에 열어 줍니다.
    보안 그룹이 연결하려는 Linux 또는 macOS 머신의 IP 주소를 허용하도록 설정합니다.

  3. 연결을 위한 터널을 만들려면 Linux 또는 macOS 머신에서 다음 명령을 실행합니다.

    Syntax 1:
    ssh -i <identity_file> -f -l <bastion-host-username> -L
    <local-port-you-connect-to>:<rds-endpoint>:<rds:listening-port>
    <bastion-host-public-ip> -v
    
    Example command:
    ssh -i "private_key.pem" -f -l ec2-user -L 5432:172.31.39.62:5432  3.133.141.189 -v

    SSH 터널링을 위해 이 명령을 실행할 때 이러한 값을 사용해 다음 설정을 구성하십시오.
    debug1: Local connections to LOCALHOST: 5432 forwarded to remote address 172.31.39.62:5432
    debug1: 127.0.0.1 포트 5432 에서 로컬 포워딩 수신 debug1: channel 0: new [port listener]
    debug1: Local forwarding listening on ::1 port 5432

    Syntax 2:
    ssh -i "Private_key.pem" -f -N -L 5432:RDS_Instance_Endpoint:5432 ec2-user@EC2-Instance_Endpoint -v
    
    Example command:
    ssh -i "private.pem" -f -N -L
    5432:pg115.xxxx.us-east-2.rds.amazonaws.com:5432
    ec2-user@ec2-xxxx-xxx9.us-east-2.compute.amazonaws.com -v
  4. 이제 로컬 머신에서 DB 인스턴스에 연결할 수 있습니다. 다음 명령 예시는 PostgreSQL로 연결하지만, 이 방식을 동일하게 사용해 MySQL 또는 여타 모든 엔진에 연결할 수 있습니다.

    Syntax -
    psql -hlocalhost -Upostgres -p<local-port-you-connect-to> -d postgres
    
    -h = localhost
    
    -U = the username present in the DB for connectivity
    
    -p = 'local-port-you-connect-to' from the SSH Tunneling command
    
    -d = Any DB, user who wants to connect.
    
    Example command -
    a483e73d651f:.ssh rahul_saha$ psql -hlocalhost -Upostgres -p5432 -d postgres
    Password for user postgres:
    psql (12.1, server 11.5)
    SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
    Type "help" for help.
    
    postgres=> \l
                                      List of databases
       Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    -----------+----------+----------+-------------+-------------+-----------------------
     postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
     rdsadmin  | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin
     template0 | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin          +
               |          |          |             |             | rdsadmin=CTc/rdsadmin
     template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
    (4 rows)
    
    $ psql -hlocalhost -Upostgres -p -d postgres
    $ mysql -h127.0.0.1 -uroot -p

참고: DB 인스턴스에 연결할 때 키워드 localhost를 사용하는 경우, MySQL이 연결하는 데 소켓을 사용하려 시도합니다. 호스트 이름 127.0.0.1을 사용해 MySQL DB 인스턴스에 액세스해야 합니다. 자세한 내용은 MySQL 웹 사이트에서 Can't connect to [local] MySQL server를 참조하십시오.

관련 정보

AWS 기반 리눅스 배스천 호스트

Amazon EC2 인스턴스를 배스천 호스트로 사용하여 로컬 시스템에서 프라이빗 Amazon RDS DB 인스턴스에 어떻게 연결할 수 있습니까?

AWS 공식업데이트됨 2년 전