Python - 连接 AWS Neptune

7
我在AWS上创建了一个Neptune实例。现在我该如何连接它?
我尝试了文档中提供的示例,从我的笔记本电脑本地连接。请参考文档
from gremlin_python.structure.graph import Graph
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()

g = graph.traversal().withRemote(DriverRemoteConnection('ws://my_endpoint:8182/gremlin','g'))

print(g.V().limit(2).toList())

但是我遇到了超时异常,以下是堆栈跟踪信息。
File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/gremlin_python/driver/driver_remote_connection.py", line 45, in __init__
    password=password)
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/gremlin_python/driver/client.py", line 76, in __init__
    self._fill_pool()
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/gremlin_python/driver/client.py", line 88, in _fill_pool
    conn = self._get_connection()
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/gremlin_python/driver/client.py", line 101, in _get_connection
    self._transport_factory, self._executor, self._pool)
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/gremlin_python/driver/connection.py", line 40, in __init__
    self.connect()
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/gremlin_python/driver/connection.py", line 46, in connect
    self._transport.connect(self._url)
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/gremlin_python/driver/tornado/transport.py", line 33, in connect
    lambda: websocket.websocket_connect(url))
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/tornado/ioloop.py", line 458, in run_sync
    return future_cell[0].result()
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/tornado/concurrent.py", line 238, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 4, in raise_exc_info
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/tornado/stack_context.py", line 316, in wrapped
    ret = fn(*args, **kwargs)
  File "/home/cegprakash/.virtualenvs/cegprakash-6Wq6Rd61/lib/python3.5/site-packages/tornado/simple_httpclient.py", line 307, in _on_timeout
    raise HTTPError(599, error_message)
tornado.httpclient.HTTPError: HTTP 599: Timeout while connecting

我是否缺少任何身份验证才能连接到数据库?


我尝试了本地机器和EC2上的两种方式,但是出现了相同的问题。 - cegprakash
你分享的链接可能只适用于 Tornado。但我在任何地方都没有使用 Tornado 设置。肯定是我在私有网络或其他地方遗漏了一些内容。我不确定具体是什么。 - cegprakash
请确保您的 Neptune 实例与您的 EC2 共享同一个 VPC。 - technomage
1
我确定我的 EC2 和 Neptune 共享同一个 VPC。 - cegprakash
请问您是否可以确认您已经正确设置了 EC2 安全组?请确保在端口 8182 上允许 HTTP 流量。您可以通过使用 HTTP curl 简单地检查连通性。请参阅公共文档:https://docs.aws.amazon.com/neptune/latest/userguide/get-started-prerequisites.html#get-started-vpc-security-group,https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-gremlin-rest.html。 - Ankit Gupta
显示剩余5条评论
3个回答

4

连接问题通常归因于安全组设置的某些问题。这已经在另一个问题[1]中得到了回答。在此发布响应,以防有所帮助。


如果在连接数据库时出现超时,第一步应该是检查是否与端点具有网络连接性。请尝试: telnet endpoint port。如果您有连接性,则会看到类似以下内容的信息:
Trying 172.217.5.110...
Connected to endpoint (172.217.5.110).
Escape character is '^]'

如果这样做有效,那么任何HTTP客户端都应该能够连接到您的数据库。(CURL、POSTMAN等)
如果telnet无法正常工作,则几乎可以确定您没有正确配置EC2安全组。您需要做的主要是:
1. 创建一个安全组(例如“ec2”),并将其附加到您的EC2客户端实例上。默认情况下,此安全组应允许对所有IP的出站连接。如果不是这种情况,请添加它。
2. 创建一个安全组(例如“db”)。在入站规则中,添加一个允许入站TCP连接到您的数据库端口的规则,并将源设置为#1创建的安全组。
3. 现在修改您的Neptune集群,并将“db”附加到其中。
4. 安全组更改传播得非常快,因此您应该能够使用telnet进行测试。
您可能会找到其他答案,说您需要将数据库和EC2实例放在同一安全组中。这并不完全正确,这只是上述步骤的一个特殊情况,您可以使用单个安全组来管理db和客户端实例,而不是创建2个安全组。从安全和设计的角度来看,最好为您的DB和客户端实例分别设置安全组。
希望这可以帮助您。
[1] https://stackoverflow.com/a/51940587/3069919

2

1

确保您的EC2和Neptune在同一个VPC中。

在安全组中允许端口8182的TCP连接。

尝试将URL从('ws://my_endpoint:8182/gremlin','g')更改为('wss://my_endpoint:8182/gremlin','g')

这对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接