DigitalOcean Droplet中Cassandra连接问题求助
Cassandra安装与连接问题排查
问题1:cqlsh连接报错ModuleNotFoundError: No module named 'six.moves'
在DigitalOcean Droplet上按官方指南安装Cassandra后,执行cqlsh时出现如下错误:
root@ubuntu-s-cass-history-chat:~# cqlsh Traceback (most recent call last): File "/usr/bin/cqlsh.py", line 134, in <module> from cassandra.cluster import Cluster File "/usr/share/cassandra/lib/cassandra-driver-internal-only-3.25.0.zip/cassandra-driver-3.25.0/cassandra/cluster.py", line 33, in <module> ModuleNotFoundError: No module named 'six.moves'
已尝试apt-get install python3-six但报错仍存在。
原因与解决方法
- 核心原因:系统通过apt安装的python3-six未被cqlsh关联的Python环境识别,cqlsh可能绑定了特定Python版本或使用内部驱动包,导致依赖无法加载。
- 解决步骤:
- 使用pip3直接安装six库(确保对应Python3环境):
pip3 install six - 若无效,检查cqlsh使用的Python路径:
输出类似head -n1 /usr/bin/cqlsh.py#!/usr/bin/python3,验证该环境下是否能导入six:
若报错,直接给该Python环境安装six:/usr/bin/python3 -c "import six.moves"/usr/bin/python3 -m pip install six - 若仍有问题,尝试升级cassandra-driver兼容依赖:
pip3 install --upgrade cassandra-driver
- 使用pip3直接安装six库(确保对应Python3环境):
问题2:本地Python远程连接Cassandra被拒绝(ConnectionRefusedError 10061)
本地使用cassandra-driver连接Droplet上的Cassandra时,出现如下错误:
NoHostAvailable: ('Unable to connect to any servers', {'host:port': ConnectionRefusedError(10061, "Tried connecting to [('host', port)]. Last error: Unable to establish a connection since the computer destination expressly denied said connection")})
连接代码:
from cassandra.cluster import Cluster cluster = Cluster([host], port=port) session = cluster.connect()
原因与解决方法
- 核心原因:防火墙限制、Cassandra监听地址配置错误、端口未对外开放三者之一或组合导致。
- 解决步骤:
- 开放Droplet防火墙端口:在DigitalOcean防火墙规则中添加TCP 9042端口的入站允许规则,允许本地IP或所有IP访问(生产环境建议限制特定IP)。
- 修改Cassandra配置文件
/etc/cassandra/cassandra.yaml:- 将
listen_address改为Droplet私有IP或0.0.0.0(生产环境不建议用0.0.0.0,需指定特定IP)。 - 将
rpc_address改为Droplet私有IP或0.0.0.0(远程连接依赖RPC端口9042)。 - 设置
broadcast_rpc_address为Droplet的公网IP,确保客户端能正确识别连接地址。
- 将
- 重启Cassandra服务:
sudo systemctl restart cassandra - 验证端口监听状态:
输出需显示netstat -tulpn | grep 90420.0.0.0:9042或指定IP绑定9042端口。 - 本地测试连通性:用telnet或nc命令测试Droplet公网IP与9042端口:
若不通,重新检查防火墙规则与Cassandra配置。telnet <Droplet公网IP> 9042
内容的提问来源于stack exchange,提问作者Esteban Ávila Rincón
相关产品推荐
相关产品推荐

