无法使用 DataStax Python 驱动程序远程连接到 Cassandra [英] Unable to connect to Cassandra remotely using DataStax Python driver
问题描述
我无法远程(从我的笔记本电脑)连接到 Cassandra(在 EC2 节点上运行).当我为 Cassandra 使用 DataStax Python 驱动程序时:
I'm having trouble connecting to Cassandra (running on an EC2 node) remotely (from my laptop). When I use the DataStax Python driver for Cassandra:
from cassandra.cluster import Cluster
cluster = Cluster(['10.X.X.X'], port=9042)
cluster.connect()
我明白了:
Traceback (most recent call last):
File "/Users/user/virtualenvs/test/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3035, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-23-dc85f20fd4f5>", line 1, in <module>
session = cluster.connect()
File "/Users/user/virtualenvs/test/lib/python2.7/site-packages/cassandra/cluster.py", line 755, in connect
self.control_connection.connect()
File "/Users/user/virtualenvs/test/lib/python2.7/site-packages/cassandra/cluster.py", line 1868, in connect
self._set_new_connection(self._reconnect_internal())
File "/Users/user/virtualenvs/test/lib/python2.7/site-packages/cassandra/cluster.py", line 1903, in _reconnect_internal
raise NoHostAvailable("Unable to connect to any servers", errors)
NoHostAvailable: ('Unable to connect to any servers', {'10.X.X.X': OperationTimedOut('errors=None, last_host=None',)})
但是 Cassandra 正在运行,因为 nodetool status
给了我:
But Cassandra is running because nodetool status
gives me:
UN 127.0.0.1 124.18 KB 256 100.0% 4d973e17-ae08-3aa1-81d2-605b92753694 rack1
Cassandra 似乎也在监听 9042 端口:
Cassandra also seems to be listening on port 9042:
netstat -nltp | grep 9042
tcp6 0 0 0.0.0.0:9042 :::* LISTEN -
而且我可以从我的笔记本电脑 telnet 到 Cassandra:
And I can telnet from my laptop to Cassandra:
$ telnet 10.X.X.X 9042
Trying 10.X.X.X...
Connected to ip-10-X-X-X.
Escape character is '^]'.
来自我的 cassandra.yaml 的相关亮点:
Relevant highlights from my cassandra.yaml:
start_native_transport: true
rpc_address: 0.0.0.0
rpc_port: 9160
broadcast_rpc_address: 10.X.X.X
/var/log/cassandra/system.log
显示没有任何用处.
编辑:我还可以使用服务器上的 cqlsh 127.0.0.1 9042
和笔记本电脑上的 cqlsh 10.X.X.X 9042
连接到 Cassandra,没有任何问题.
edit:
I can also connect to Cassandra using cqlsh 127.0.0.1 9042
on the server, and cqlsh 10.X.X.X 9042
from my laptop without any problems.
解决方案
由于某种原因,看起来我测试脚本的地方(PyCharm 中的 Python 控制台)是我的脚本唯一无法运行的地方.在 PyCharm 或 Python/iPython 交互模式下作为脚本运行没有问题.我会留下这个问题,因为它包含一个相当完整的 Python 到 Cassandra 连接故障排除清单.
Looks like the one place where I tested my script (a Python console inside PyCharm) is, for some reason, the only place where my script does not work. No problems running as a script in PyCharm or in Python/iPython interactive mode. I will leave the question, as it contains a fairly complete checklist for Python-to-Cassandra connection troubleshooting.
推荐答案
如果 控制连接超时 已超过.尝试增加它或将其设置为 None
以完全禁用此超时.例如:
Such exception can be thrown if a control connection timeout was exceeded. Try to increase it or set it to None
to completely disable this timeout. For example:
from cassandra.cluster import Cluster
cluster = Cluster(['127.0.0.1'], control_connection_timeout=10, port=9042)
cluster.connect()
这篇关于无法使用 DataStax Python 驱动程序远程连接到 Cassandra的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!