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

Time:03-09

I am using MAC OS (Monterey 12.2.1) Apple M1, Chip

PostgreSQL works fine for me but Cassandra doesn't. After installing and importing cassandra, I tried connecting to the local instance of Apache Cassandra as done below

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

However I got the following error:

('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")})

I have cassandra 4.0.3 installed and and I have tried many of the suggestions online but didn't work for me. I cannot even find files like cassandra-env.sh, cassandra.yaml

With the command java -version, I have:

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)

CodePudding user response:

So it's not abundantly clear how Cassandra was installed on your Mac (homebrew, etc). But as another possible method of achieving this, I'll list out the steps about how I run Apache Cassandra on my Mac, and maybe that'll help you.

Download the tarball. For this example, I'll go with Apache Cassandra 4.0.3.

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

Move the tarball to the desired location:

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

Untar:

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

Configure (if you skip this step, it will) start:

atom conf/cassandra.yaml

Start:

bin/cassandra -p cassandra.pid

This ^ starts Cassandra with a PID file. That allows you to break-away from the output, and it will still keep running in the background (unlike bin/cassandra or bin/cassandra -f).

CodePudding user response:

This message indicates that there is no Cassandra instance running on localhost listening on port 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")})

You need to confirm that Cassandra is running on your Mac. The quickest way is to determine if there is a process listening on port 9042 with either of these commands:

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

Cassandra by default listens for client connections on CQL port 9042 configured in cassandra.yaml with:

native_transport_port: 9042

Clients connect to Cassandra using the rpc_address in cassandra.yaml:

rpc_address: localhost

If you've set it to your Mac's IP address then you will need to use it as the contact point on this line of your app instead of localhost:

    cluster = Cluster(['127.0.0.1']) 

If you're new to Cassandra, I recommend having a look at datastax.com/dev which has lots of free hands-on interactive learning resources. In particular, the Cassandra Fundamentals learning series lets you learn the basic concepts quickly. Cheers!

  • Related