如何访问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)

相关推荐
wanhengidc1 小时前
造成云手机闪退的原因有哪些?
服务器·网络·安全·智能手机·媒体
聚铭网络3 小时前
聚铭安全管家平台2.0实战解码 | 安服篇(四):重构威胁追溯体系
网络·安全·重构
郭二哈3 小时前
git的使用
大数据·网络·git·elasticsearch
码luffyliu4 小时前
MySQL:MVCC机制及其在Java秋招中的高频考点
java·数据库·mysql·事务·并发·mvcc
水涵幽树4 小时前
MySQL 时间筛选避坑指南:为什么格式化字符串比较会出错?
数据库·后端·sql·mysql·database
zcz16071278214 小时前
Linux 网络命令大全
linux·运维·网络
VVVVWeiYee4 小时前
BGP高级特性
运维·服务器·网络
subuq6 小时前
Web3.0 时代的电商系统:区块链如何解决信任与溯源问题?
大数据·网络·学习
安卓开发者6 小时前
Android中使用RxJava实现网络请求与缓存策略
android·网络·rxjava
fake_ss1987 小时前
从零开发Java坦克大战Ⅱ (下)-- 从单机到联机(完整架构功能实现)
java·网络·tcp/ip·游戏程序