1

I had followed the steps given in https://docs.datastax.com/en/developer/python-driver/3.25/getting_started/ to connect to cassandra database using python code, but still after running the code snippet I am getting

NoHostAvailable: ('Unable to connect to any servers', {'hosts"port': OperationTimedOut('errors=None, last_host=None'),
  • Python version 2.7 and 3 (classpath is set for both the python versions)
  • Java 1.8 (class path has been set)
  • Apache cassandra 3.11.6 (apache home classpath has been set)
Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
Dolly
  • 97
  • 3
  • 18

2 Answers2

2

I tend to use a very simple app to test connectivity to a Cassandra cluster:

from cassandra.cluster import Cluster

cluster = Cluster(['10.1.2.3'], port=45678)
session = cluster.connect()

row = session.execute("SELECT release_version FROM system.local").one()
if row:
    print(row[0])

Then run it:

$ python HelloCassandra.py
4.0.6

In your comment you mentioned that you're getting OperationTimedOut which indicates that the driver never got a response back from the node within the client timeout period. This usually means (a) you're connecting to the wrong IP, (b) you're connecting to the wrong CQL port, or (c) there's a network connectivity issue between your app and the cluster.

Make sure that you're using the IP address that you've set in rpc_address of cassandra.yaml. Also make sure that the node is listening for CQL clients on the right port. You can easily verify this by checking the output of either of these Linux utilities like netstat or lsof, for example:

$ sudo lsof -nPi -sTCP:LISTEN

Cheers!

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
1

So that error message suggests that the host/port combination either does not have Cassandra running on it or is under heavy load and unable to respond.

Can you edit your question to include the Cassandra connection portion of your code, as well as maybe how you're calling it? I have a test script which I use (and you're welcome to check it out), and here is the connection portion:

protocol=4

hostname=sys.argv[1]
username=sys.argv[2]
password=sys.argv[3]

nodes = []
nodes.append(hostname)

auth_provider = PlainTextAuthProvider(username=username, password=password)
cluster = Cluster(nodes,auth_provider=auth_provider, protocol_version=protocol)
session = cluster.connect()

I call it like this:

$ python3 testCassandra.py 127.0.0.1 aaron notReallyMyPassword
local

One thing you might try too, would be to run a nodetool status on the cluster just to make sure it's running ok.

Edit

local variable 'session' referenced before assignment

So this sounds to me like you're attempting a session.execute before session = cluster.connect(). Have a look at my Git repo (linked above) to see the correct order for instantiating session.

I am not using default port

In that case, make sure the port is being set in the cluster definition. Ex:

port = 19099
cluster = Cluster(nodes,auth_provider=auth_provider, port=port)
Aaron
  • 55,518
  • 11
  • 116
  • 132
  • Hey @Aaron, thanks for replying .. I have used the same code snippet as yours though in my code NODES and PROTOCOL were missing. But despite of adding those 2 values, getting the same error as in below comment (I am not using default port ) – Dolly Sep 08 '22 at 09:50
  • [ ERROR ] Control connection failed to connect, shutting down Cluster: Traceback (most recent call last): File "cassandra\cluster.py", line 1690, in cassandra.cluster.Cluster.connect File "cassandra\cluster.py", line 3488, in cassandra.cluster.ControlConnection.connect File "cassandra\cluster.py", line 3533, in cassandra.cluster.ControlConnection._reconnect_internal cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'host:port': OperationTimedOut('errors=None, last_host=None')}) | FAIL | UnboundLocalError: local variable 'session' referenced before assignment – Dolly Sep 08 '22 at 09:52
  • @Dolly edit made. Also, is the cluster you are trying to connect to running on your local machine? – Aaron Sep 08 '22 at 13:07
  • no cluster is not on my local machine .. Also I have written separate func for connection where I am doing 'session = cluster.connect(key_space)'.. Also the port is not default one and for this i had given 'cluster = Cluster(nodes, auth_provider=auth_provider,protocol_version=protocol, port=port)' – Dolly Sep 12 '22 at 07:08