编程语言
首页 > 编程语言> > 无法使用DataStax Python驱动程序远程连接到Cassandra

无法使用DataStax Python驱动程序远程连接到Cassandra

作者:互联网

我无法远程(从我的笔记本电脑)连接到Cassandra(在EC2节点上运行).当我为Cassandra使用DataStax Python驱动程序时:

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状态给了我:

UN 127.0.0.1 124.18 KB 256 100.0% 4d973e17-ae08-3aa1-81d2-605b92753694 rack1

Cassandra似乎也在侦听端口9042:

netstat -nltp | grep 9042

tcp6       0      0 0.0.0.0:9042            :::*                    LISTEN      -

我可以从我的笔记本电脑telnet到Cassandra:

$telnet 10.X.X.X 9042
Trying 10.X.X.X...
Connected to ip-10-X-X-X.
Escape character is '^]'.

来自我的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,没有任何问题.

看起来我测试我的脚本(PyCharm中的Python控制台)的地方,由于某种原因,我的脚本不起作用的唯一地方.在PyCharm或Python / iPython交互模式下作为脚本运行没有问题.我将留下这个问题,因为它包含一个相当完整的Python-to-Cassandra连接故障排除清单.

解决方法:

这是一个简单的调试方法:
 在另一台安装了cassandra的机器上,使用cassandra cli命令“cqlsh 10.X.X.X”连接远程cassandra服务器.如果失败,则应修改cassandra远程模式的cassandra配置文件.

标签:datastax,python,cassandra,database-connection
来源: https://codeday.me/bug/20191008/1872292.html