为什么我无法在我的 MacOS 上连接到 Cassandra?

Why can't I connect to Cassandra on my MacOS?

我正在使用 MAC OS (Monterey 12.2.1) Apple M1, Chip

PostgreSQL 适合我,但 Cassandra 不行。安装并导入 cassandra 后,我尝试连接到 Apache Cassandra 的本地实例,如下所示

import cassandra    
from cassandra.cluster import Cluster
try: 
   cluster = Cluster(['127.0.0.1']) 
   session = cluster.connect()
except Exception as e:
   print(e)

但是我得到了以下错误:

('Unable to connect to any servers', {'127.0.0.1:9042': ConnectionRefusedError(61, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})

我已经安装了 cassandra 4.0.3,而且我已经尝试了很多在线建议,但对我没有用。我什至找不到像 cassandra-env.shcassandra.yaml

这样的文件

使用命令 java -version,我有:

java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)

此消息表明 localhost 上没有 Cassandra 实例 运行 侦听端口 9042:

('Unable to connect to any servers', {'127.0.0.1:9042': \
  ConnectionRefusedError(61, "Tried connecting to [('127.0.0.1', 9042)]. \
  Last error: Connection refused")})

您需要确认 Cassandra 在您的 Mac 上是 运行。最快的方法是使用以下任一命令确定是否有进程在端口 9042 上侦听:

$ netstat -tnlp
$ sudo lsof -nPi -sTCP:LISTEN

Cassandra 默认在 cassandra.yaml 中配置的 CQL 端口 9042 上侦听客户端连接:

native_transport_port: 9042

客户端使用 cassandra.yaml 中的 rpc_address 连接到 Cassandra:

rpc_address: localhost

如果您已将其设置为您的 Mac 的 IP 地址,那么您将需要将其用作应用这一行的联系点,而不是 localhost:

    cluster = Cluster(['127.0.0.1']) 

如果您是 Cassandra 的新手,我建议您看看 datastax.com/dev which has lots of free hands-on interactive learning resources. In particular, the Cassandra Fundamentals 学习系列,让您快速了解基本概念。干杯!

因此, Cassandra 是如何安装在您的 Mac(自制软件等)上的还不是很清楚。但作为实现此目标的另一种可能方法,我将列出有关 I 运行 Apache Cassandra 如何在我的 Mac 上执行的步骤,也许这会有所帮助你.

下载压缩包。对于此示例,我将使用 Apache Cassandra 4.0.3。

https://dlcdn.apache.org/cassandra/4.0.3/apache-cassandra-4.0.3-bin.tar.gz

将压缩包移动到所需位置:

mv ~/Downloads/apache-cassandra-4.0.3-bin.tar.gz ~/local/

解压:

cd ~/local
tar -zxvf apache-cassandra-4.0.3-bin.tar.gz
cd apache-cassandra-4.0.3

配置(如果跳过此步骤,)启动:

atom conf/cassandra.yaml

开始:

bin/cassandra -p cassandra.pid

这 ^ 使用 PID 文件启动 Cassandra。这允许您从输出中 break-away,它仍然会在后台保留 运行ning(不像 bin/cassandrabin/cassandra -f)。