简体   繁体   English

无法从 EC2 实例连接到 RDS 实例

[英]Can't connect to RDS instance from EC2 instance

From EC2 instance i-78a8df00, I'm trying to connect to RDS instance mysql.************.us-east-1.rds.amazonaws.com.从 EC2 实例 i-78a8df00,我正在尝试连接到 RDS 实例 mysql.************.us-east-1.rds.amazonaws.com。 They are both in the US East region.他们都在美国东部地区。 I added the security group of EC2 instance (sg-********) to the RDS security group, but that didn't help.我将EC2实例的安全组(sg-********)添加到RDS安全组,但这没有帮助。 It appears to be a firewall/DNS issue as it is timing out when running this command:这似乎是防火墙/DNS 问题,因为它在运行此命令时超时:

ubuntu@ip-10-195-189-237:~$ mysql -h mysql.************.us-east-1.rds.amazonaws.com

ERROR 2003 (HY000): Can't connect to MySQL server on 'mysql.************.us-east-1.rds.amazonaws.com' (110)错误 2003 (HY000):无法连接到“mysql.************.us-east-1.rds.amazonaws.com”上的 MySQL 服务器 (110)

I can connect to RDS instance fine from my local machine using the same line as above.我可以使用与上面相同的行从本地计算机很好地连接到 RDS 实例。 I tried various forum solutions but those don't help.我尝试了各种论坛解决方案,但没有帮助。

I had similar problem, when I spun a new EC2 instance, but didn't change setting in RDS security group of inbound IP address allowed to connect to port 3306 of my RDS instance.当我创建一个新的 EC2 实例时,我遇到了类似的问题,但没有更改 RDS 安全组中允许连接到我的 RDS 实例的端口 3306 的入站 IP 地址的设置。

The confusing bit was an option in RDS dashboard, called Security Groups.令人困惑的是 RDS 仪表板中的一个选项,称为安全组。 You don't need it to solve the problem.你不需要它来解决问题。

What you really need is:你真正需要的是:

  • Go to list of RDS instances转到 RDS 实例列表
  • Click on the instance you are trying to connect单击您尝试连接的实例
  • Click Security group rules section单击安全组规则部分

在此处输入图像描述

This should open a new browser tab or window with details of security group.这应该会打开一个新的浏览器选项卡或窗口,其中包含安全组的详细信息。 Locate several tabs in bottom part, select Inbound rules tab and click Edit button.在底部找到几个选项卡,选择入站规则选项卡,然后单击编辑按钮。

在此处输入图像描述

Change value to the IP address of your EC2 instance or IPv4 CIDR blocks, eg将值更改为您的 EC2 实例或 IPv4 CIDR 块的 IP 地址,例如

174.33.0.0/16 174.33.0.0/16

To get this value, you can either ssh into your instance and run ip addr or run EC2 Manager in browser and locate value of Private IPs in your instance details.要获取此值,您可以通过 ssh 进入您的实例并运行ip addr或在浏览器中运行 EC2 Manager 并在您的实例详细信息中找到私有 IP的值。

Additional information for people who might run into similar issues trying to connect to RDS or RedShift:对于在尝试连接到 RDS 或 RedShift 时可能遇到类似问题的人的其他信息:

1) Check security groups 1)检查安全组

Verify the security group for the RDS instance allows access from the security group your source server belongs to (or its IP added directly if external to AWS).验证 RDS 实例的安全组是否允许从您的源服务器所属的安全组(或者如果在 AWS 外部,则直接添加其 IP)进行访问。 The security group you should be looking at is the one specified in the RDS instance attributes from the RDS console UI (named "security group").您应该查看的安全组是在 RDS 控制台 UI 的 RDS 实例属性中指定的安全组(名为“安全组”)。

NOTE : Database security groups might be different from AWS EC2 security groups.注意:数据库安全组可能与 AWS EC2 安全组不同。 If your RDS instance is in classic/public EC2, you should check in the "database security group" section of the RDS UI.如果您的 RDS 实例在经典/公共 EC2 中,您应该检查 RDS UI 的“数据库安全组”部分。 For VPC users, the security group will be an normal VPC security group (the name sg-xxx will be listed in the RDS instance's attributes).对于 VPC 用户,安全组将是一个普通的 VPC 安全组(名称 sg-xxx 将列在 RDS 实例的属性中)。

2) Confirm DNS isn't an issue. 2) 确认 DNS 不是问题。

Amazon uses split DNS, so a DNS lookup external to AWS will return the public IP while a lookup internal to AWS will return a private IP. Amazon 使用拆分 DNS,因此 AWS 外部的 DNS 查找将返回公共 IP,而 AWS 内部的查找将返回私有 IP。 If you suspect it is a DNS issue, have you confirmed different IPs are returned from different availability zones?如果您怀疑是 DNS 问题,您是否确认从不同的可用区返回不同的 IP? If different AZs get different IPs, you will need to contact AWS support.如果不同的 AZ 获得不同的 IP,您将需要联系 AWS 支持。

3) Confirm network connectivity by establishing a socket connection. 3) 通过建立套接字连接来确认网络连接。

Tools like tracepath and traceroute likely won't help since RDS currently drops ICMP traffic.由于 RDS 当前会丢弃 ICMP 流量,因此 tracepath 和 traceroute 之类的工具可能无济于事。

Test port connectivity by trying to establish a socket connection to the RDS instance on port 3306 (mysql, or 5432 for postgres).通过尝试在端口 3306(mysql 或 5432 用于 postgres)上建立与 RDS 实例的套接字连接来测试端口连接。 Start by finding the IP of the RDS instance and using either telnet or nc:首先查找 RDS 实例的 IP 并使用 telnet 或 nc:

telnet x.x.x.x 3306
nc -vz x.x.x.x 3306

a) If your connection attempt isn't successful and immediately fails, the port is likely blocked or the remote host isn't running a service on that port. a)如果您的连接尝试不成功并立即失败,则该端口可能被阻塞或远程主机未在该端口上运行服务。 you may need to engage AWS support to troubleshoot further.您可能需要与 AWS 支持联系以进一步排除故障。 If connecting from outside of AWS, try to connect from another instance inside AWS first (as your firewall might be blocking those connections).如果从 AWS 外部连接,请先尝试从 AWS 内部的另一个实例连接(因为您的防火墙可能会阻止这些连接)。

b) If your connection isn't successful and you get a timeout, packets are probably being dropped/ignored by a firewall or packets are returning on a different network path. b)如果您的连接不成功并且超时,则数据包可能被防火墙丢弃/忽略,或者数据包正在返回不同的网络路径。 You can confirm this by running netstat -an | grep SYN您可以通过运行netstat -an | grep SYN来确认这一点。 netstat -an | grep SYN (from a different CLI window/session while running and waiting for the telnet/nc command to timeout). netstat -an | grep SYN (从不同的 CLI 窗口/会话运行并等待 telnet/nc 命令超时)。 Connections in the SYN state mean that you've sent a connection request, but haven't received anything back (SYN_ACK or reject/block).处于 SYN 状态的连接意味着您已发送连接请求,但尚未收到任何回复(SYN_ACK 或拒绝/阻止)。 Usually this means a firewall or security group is ignoring or dropping packets.通常这意味着防火墙或安全组正在忽略或丢弃数据包。

Check to make sure you're not using iptables or a NAT gateway between your host and the RDS instance.检查以确保您没有在主机和 RDS 实例之间使用 iptables 或 NAT 网关。 If you're in a VPC, also make sure you allow egress/outbound traffic from the source host.如果您在 VPC 中,还请确保您允许来自源主机的出口/出站流量。

c) If your socket connection test was successful, but you can't connect with a mysql client (CLI, workbench, app, etc.), take a look at the output of netstat to see what state the connection is in (replace xxxx with the actual IP address of the RDS instance): c)如果你的socket连接测试成功了,但是不能连接mysql客户端(CLI、workbench、app等),看一下netstat的输出,看看连接处于什么状态(替换xxxx使用 RDS 实例的实际 IP 地址):

netstat -an | grep xxxx

If you were getting a connection established when using telnet or NC, but you see the 'SYN' state when using a mysql client, you might be running into an MTU issue.如果您在使用 telnet 或 NC 时建立了连接,但在使用 mysql 客户端时看到“SYN”状态,则您可能遇到了 MTU 问题。

RDS, at the time this is written, may not support ICMP packets used for PMTUD ( https://en.wikipedia.org/wiki/Path_MTU_Discovery#Problems_with_PMTUD ).在编写本文时,RDS 可能不支持用于 PMTUD 的 ICMP 数据包 ( https://en.wikipedia.org/wiki/Path_MTU_Discovery#Problems_with_PMTUD )。 This can be a problem if you're trying to access RDS or RedShift that's in a VPC from a classic ec2 instance via ClassicLink.如果您尝试通过 ClassicLink 从经典 ec2 实例访问 VPC 中的 RDS 或 RedShift,这可能会出现问题。 Try lowering the MTU with the following, then testing again:尝试使用以下方法降低 MTU,然后再次测试:

sudo ip link show
# take note of the current MTU (likely 1500 or 9001)
sudo ip link set dev eth0 mtu 1400

If the lower MTU worked, be sure to follow up with AWS customer support for help and mention that you are seeing an MTU issue while trying to connect to your RDS instance.如果较低的 MTU 有效,请务必跟进 AWS 客户支持以寻求帮助,并提及您在尝试连接到 RDS 实例时遇到了 MTU 问题。 This can happen if TCP packets are wrapped with encapsulation for tunneling, resulting in a lower usable MTU for packet data / payload.如果 TCP 数据包使用隧道封装封装,则会发生这种情况,从而导致数据包数据/有效负载的可用 MTU 较低。 Lowering the MTU on the source server allows the wrapped packets to still fit under the limit.降低源服务器上的 MTU 允许打包的数据包仍然适合限制。

If it didn't work, set your MTU back to it's default and engage AWS support for further troubleshooting.如果它不起作用,请将您的 MTU 设置回默认值并联系 AWS 支持以进行进一步的故障排除。

While Mark's problem seemed to have something to do with multi-AZ routing & EC2 classic, I ran into this exact same problem today.虽然 Mark 的问题似乎与多可用区路由和 EC2 经典有关,但我今天遇到了同样的问题。

To fix it, I modified the Security Group that was created automatically with my RDS instance by adding the two private IP addresses from my EC2 instance.为了解决这个问题,我修改了使用我的 RDS 实例自动创建的安全组,方法是添加我的 EC2 实例中的两个私有 IP 地址。

向我的 RDS 实例添加入站规则

This was a fairly obvious problem, but I'm new to AWS in general, so hopefully this is useful for others like myself.这是一个相当明显的问题,但总的来说我是 AWS 的新手,所以希望这对像我这样的其他人有用。

Apparently, multi-AZ screws everything up.显然,多可用区搞砸了一切。 Since the default multi-AZ config placed my database in region us-east-1d, and my EC2 instance was in region us-east-1a, the DNS was not routing correctly.由于默认的多可用区配置将我的数据库放置在区域 us-east-1d 中,并且我的 EC2 实例位于区域 us-east-1a 中,因此 DNS 路由不正确。 I re-created the RDS instance as non-multi-AZ, and made it live in us-east-1a, and all is happy.我将 RDS 实例重新创建为非多可用区,并将其置于 us-east-1a 中,一切都很开心。

If there are any super geniuses out there in regards to DNS routing on AWS with RDS, ELB, and multi-AZ capabilities, it would be pretty awesome to know how to do this, since this isn't documented anywhere in Amazon Web Service's documentation.如果在 AWS 上具有 RDS、ELB 和多可用区功能的 DNS 路由方面有任何超级天才,知道如何做到这一点会非常棒,因为这在 Amazon Web Service 的文档中没有任何记录.

After struggling for 3 days I finally found why mine was not connecting ...经过3天的努力,我终于找到了为什么我的连接不上...

Add outbound rule on your EC2 instance for port 3306 and inbound rules on your RDS server on port 3306. The inbound value should be the security of the EC2 instance在您的 EC2 实例上为端口 3306 添加出站规则,在您的 RDS 服务器上在端口 3306 上添加入站规则。入站值应该是 EC2 实例的安全性

Example:例子:

  • Your EC2 security group is - sg.ec2您的 EC2 安全组是 - sg.ec2
  • And RDS security group is - sg.rds RDS 安全组是 - sg.rds

So go to edit outbound rules of sg.ec2 and add Custom TCP at port 3306 and destination to 0.0.0.0/0所以去编辑 sg.ec2 的出站规则并在端口3306和目标添加自定义 TCP 到0.0.0.0/0

Then, go to edit inbound rule of sg.rds and add an inbound rule at port 3306 and source as sg.ec2然后,去编辑 sg.rds 的入站规则并在端口 3306 和源作为 sg.ec2 添加一个入站规则

I had the similar problem today when my EC2 instance suddenly lost access to RDS instance and Wordpress stopped working.今天我遇到了类似的问题,当时我的 EC2 实例突然无法访问 RDS 实例并且 Wordpress 停止工作。 The security groups were correct and I could even connect to MySQL from console on EC2 instance but not from PHP.安全组是正确的,我什至可以从 EC2 实例上的控制台连接到 MySQL,但不能从 PHP。 For some reason restarting EC2 server helped me.出于某种原因,重新启动 EC2 服务器帮助了我。

看起来在上次发布和本次发布之间的某个时间,亚马逊修复了 DNS 路由问题,因为现在对于多可用区 rds 一切正常......

Solution : I had to adjust the inbound rule of the RDS instance's security group to allow connections from the IP address of my EC2 instance.解决方案:我不得不调整 RDS 实例的安全组的入站规则以允许来自我的 EC2 实例的 IP 地址的连接。

Longer explanation : When creating an AWS RDS instance I selected the option to be able to connect to it via public IP address.更长的解释:创建 AWS RDS 实例时,我选择了能够通过公共 IP 地址连接到它的选项。 In doing so I ASSUMED that I would then be able to connect to it from ANY IP Address, but that was not the case.这样做时,我假设我可以从任何 IP 地址连接到它,但事实并非如此。 Rather, when the instance got created/configured, it took the public IP address of my laptop and set the inbound rule of the RDS security group to only accept connections from that IP address.相反,当实例被创建/配置时,它使用我笔记本电脑的公共 IP 地址并将 RDS 安全组的入站规则设置为仅接受来自该 IP 地址的连接。 So I had to manually add a rule to allow incoming MYSQL/Aurora connections from IP address of the EC2 instance.所以我不得不手动添加一条规则,以允许来自 EC2 实例 IP 地址的传入 MYSQL/Aurora 连接。

I ran into the same issue, I was not able to connect from EC2 instance to the RDS both resources in the same VPC and multi AZ setup.我遇到了同样的问题,我无法从 EC2 实例连接到同一 VPC 和多可用区设置中的 RDS 两个资源。

The multi-AZ setup was not a problem for me.多可用区设置对我来说不是问题。

I was missing only the OUTBOUND rule 3306 from my EC2 instance Security group, to my RDS Security group.我只缺少从我的 EC2 实例安全组到我的 RDS 安全组的 OUTBOUND 规则 3306。

According to the Amazon doc , we should use:根据亚马逊文档,我们应该使用:

PROMPT> mysql -h <endpoint> -P 3306 -u <mymasteruser> -p

where endpoint and mymasteruser (username) are from your RDS instance.其中端点mymasteruser (用户名)来自您的 RDS 实例。

I solved that problem using IP public adress directly(of the endpoint) instead of the endpoint(****.us-east-1.rds.amazonaws.com).You can get the ip public address using "ping" command(ping ****.us-east-1.rds.amazonaws.com)我直接使用IP公共地址(端点)而不是端点(****.us-east-1.rds.amazonaws.com)解决了这个问题。您可以使用“ping”命令获取IP公共地址( ping ****.us-east-1.rds.amazonaws.com)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM