rahul bajaj
rahul bajaj

Reputation: 11

cassandra.cluster.NoHostAvailable "Unable to connect to any servers" for cassandra-driver database

When I use this command:

session = cluster.connect('demo')

...it gives me an error saying:

>>> session = cluster.connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cassandra/cluster.py", line 1152, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:17598)
  File "cassandra/cluster.py", line 1185, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:17419)
  File "cassandra/cluster.py", line 1172, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:17122)
  File "cassandra/cluster.py", line 2618, in cassandra.cluster.ControlConnection.connect (cassandra/cluster.c:47646)
  File "cassandra/cluster.py", line 2655, in cassandra.cluster.ControlConnection._reconnect_internal (cassandra/cluster.c:48543)
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'127.0.0.1': ConnectionRefusedError(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})

I am on Fedora.

Upvotes: 1

Views: 6897

Answers (1)

Jibin
Jibin

Reputation: 7

Here is the standard process to connect to a Cassandra instance using the DataStaX python driver (cassandra-driver)

from cassandra.cluster import Cluster
from cassandra.policies import DCAwareRoundRobinPolicy
from cassandra.auth import PlainTextAuthProvider

def cassandra_conn():

   auth_provider = PlainTextAuthProvider(username='cassandra', password='password')
   cluster = Cluster(['127.0.0.1'], load_balancing_policy=DCAwareRoundRobinPolicy(local_dc='US-WEST'), port=9042, auth_provider=auth_provider)

   session = cluster.connect()

   return session, cluster

Note: I have included connection credentials based on my local cassandra cluster. However, these credentials would be different when connecting to a different instance.

Upvotes: 1

Related Questions