如何访问AWS私有网络中的RDS (Mysql)

文章目录

小结

在AWS私有网络中部署了RDS (Mysql), 尝试通过外网成功地进行了访问.

问题及解决

连接问题

在AWS私有网络中部署了RDS (Mysql), 进行外网进行访问碰到了各种问题.

以下连接超时:
ERROR 2003 (HY000): Can't connect to MySQL server on 'sql1.example.com:3306' (110)

shell 复制代码
~]# perror 110
OS error code 110:  Connection timed out

以上问题基本上是由于路由端口不通之类的问题导致的。

首先, 能过外网是无法直接访问AWS私有网络中部署了RDS (Mysql)的, 需要设置一个Jump Host, 注意Jump Host需要与RDS Mysql在同一个VPC中,设置好安全组Security Group,各种公私密钥在此不赘述.

假如10.0.0.106是这个Jump Host,需要在这个机器上安装Mysql:

shell 复制代码
[ec2-user@ip-10-0-0-106 ~]$ sudo dnf install mysql80-community-release-el9-3.noarch.rpm
[ec2-user@ip-10-0-0-106 ~]$ sudo dnf install mysql-community-server

安装Telnet并测试端口是否可访问, 这里spring-petclinic-rest-petclinicdb-lnprknzirs7d.c08lozdhnc5k.ap-southeast-1.rds.amazonaws.com是数据库RDS (Mysql)的访问地址Endpoint:

shell 复制代码
[ec2-user@ip-10-0-0-106 ~]$ yum install telnet
Error: This command has to be run with superuser privileges (under the root user on most systems).
[ec2-user@ip-10-0-0-106 ~]$ sudo yum install telnet

[ec2-user@ip-10-0-0-106 ~]$ telnet spring-petclinic-rest-petclinicdb-lnprknzirs7d.c08lozdhnc5k.ap-southeast-1.rds.amazonaws.com 3306
Trying 10.0.20.11...
Connected to spring-petclinic-rest-petclinicdb-lnprknzirs7d.c08lozdhnc5k.ap-southeast-1.rds.amazonaws.com.
Escape character is '^]'.
J
8.0.337x|\L|(ÿ66Sak
                   Tjr?mysql_native_password^CConnection closed by foreign host.

Mysql访问成功如下:

shell 复制代码
[ec2-user@ip-10-0-0-106 ~]$ mysql -h spring-petclinic-rest-petclinicdb-lnprknzirs7d.c08lozdhnc5k.ap-southeast-1.rds.amazonaws.com -P 3306 -u PetClinicDB -p

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 312
Server version: 8.0.33 Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> exit
Bye

如果不清楚用户名,可以使用AWS指令来获取MasterUsername:

shell 复制代码
[ec2-user@ip-10-0-3-241 ~]$ aws rds describe-db-instances   --filters "Name=engine,Values=mysql"   --query "*"
[
    [
        "spring-petclinic-rest-petclinicdb-lnprknzirs7d",
        "spring-petclinic-rest-petclinicdb-lnprknzirs7d.c08lozdhnc5k.ap-southeast-1.rds.amazonaws.com",
        3306,
        "PetClinicDB"
    ]
]


[ec2-user@ip-10-0-3-241 ~]$ aws rds describe-db-instances --region ap-south-1
{
    "DBInstances": []
}
[ec2-user@ip-10-0-3-241 ~]$ aws rds describe-db-instances --region ap-southeast-1
{
    "DBInstances": [
        {
            "DBInstanceIdentifier": "spring-petclinic-rest-petclinicdb-lnprknzirs7d",
            "DBInstanceClass": "db.t2.medium",
            "Engine": "mysql",
            "DBInstanceStatus": "available",
            "MasterUsername": "PetClinicDB",
            "DBName": "PetClinicDB",
            "Endpoint": {
                "Address": "spring-petclinic-rest-petclinicdb-lnprknzirs7d.c08lozdhnc5k.ap-southeast-1.rds.amazonaws.com",
                "Port": 3306,
                "HostedZoneId": "Z2G0U3KFCY8NZ5"
            },
            "AllocatedStorage": 5,
            "InstanceCreateTime": "2023-12-14T04:01:18.309000+00:00",
            "PreferredBackupWindow": "20:52-21:22",
            "BackupRetentionPeriod": 1,
            "DBSecurityGroups": [],
            "VpcSecurityGroups": [
                {
                    "VpcSecurityGroupId": "sg-089602d25785464ca",
                    "Status": "active"
                }
            ],
            "DBParameterGroups": [
                {
                    "DBParameterGroupName": "default.mysql8.0",
                    "ParameterApplyStatus": "in-sync"
                }
            ],
........

如何使用本地的Mysql Workbench对RDS进行访问

如果需要用本地的Mysql Workbench对RDS进行访问,那么就需要通过Jump Host建立的SSH连接建立一个隧道Tunnuel进行连接。

这里以Xshell为例进行设置,对SSH连接进行TCP/IP转发进行设置:

Mysql Workbench直接连接本地的3306端口即可访问:

参考

stackoverflow: grant remote access of MySQL database from any IP address
mySQL / MariaDB - Resolve "ERROR 2003 (HY000) Cant connect to MySQL server (110)" (connection timed out)
Using a Jump host to access an RDS database in a private subnet
serverfault: How do you recover you RDS master user username?
AWS user guide: Connecting to a DB instance running the MySQL database engine
Linux Howto: Installing MySQL 8 server & client on Amazon Linux 2023
kinsta: How to Change Your MySQL Password in XAMPP (3 Methods)

相关推荐
2603_969579282 小时前
内网数据自动备份工具:隔离网络下的数据保护方案
网络
逗脑IDE2 小时前
零基础学ESP32:RFID无线射频卡——让ESP32拥有“刷卡“能力!
开发语言·网络·人工智能·python·esp32·硬件开发
cws2004012 小时前
监控摄像头选型指南
运维·网络·监控
和裕3 小时前
定制纸箱刀模费全解析:费用定义与可减免合作场景
大数据·运维·网络·人工智能·算法
troy1283 小时前
智能体沙箱:如何为 Agent 模型搭建安全试验场,降低幻觉带来的损失
网络·安全
LHR_friendship3 小时前
HCIP课程全部内容
网络·笔记·学习·hcip·华为ensp
Chengbei114 小时前
拒绝无效洞!蚂蚁Src榜一Clown 2.0 SRC SKill 第二代带状态闭环的白盒 & 黑盒审计 Skill
网络·人工智能·安全·web安全·网络安全·自动化·安全架构
2301_777998344 小时前
TCP协议:滑动窗口与流量控制
服务器·网络·网络协议·tcp/ip·php
156082072194 小时前
使用JFMRFVU3P多通道同步相位测试
网络·人工智能
晚风叙码4 小时前
MySQL 数据类型详解:从数值到字符串,一篇讲透
android·mysql·adb