2
votes

I'm having trouble connecting to Cassandra (running on an EC2 node) remotely (from my laptop). When I use the DataStax Python driver for Cassandra:

from cassandra.cluster import Cluster
cluster = Cluster(['10.X.X.X'], port=9042)
cluster.connect()

I get:

Traceback (most recent call last):
  File "/Users/user/virtualenvs/test/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-23-dc85f20fd4f5>", line 1, in <module>
    session = cluster.connect()
  File "/Users/user/virtualenvs/test/lib/python2.7/site-packages/cassandra/cluster.py", line 755, in connect
    self.control_connection.connect()
  File "/Users/user/virtualenvs/test/lib/python2.7/site-packages/cassandra/cluster.py", line 1868, in connect
    self._set_new_connection(self._reconnect_internal())
  File "/Users/user/virtualenvs/test/lib/python2.7/site-packages/cassandra/cluster.py", line 1903, in _reconnect_internal
    raise NoHostAvailable("Unable to connect to any servers", errors)
NoHostAvailable: ('Unable to connect to any servers', {'10.X.X.X': OperationTimedOut('errors=None, last_host=None',)})

But Cassandra is running because nodetool status gives me:

UN 127.0.0.1 124.18 KB 256 100.0% 4d973e17-ae08-3aa1-81d2-605b92753694 rack1

Cassandra also seems to be listening on port 9042:

netstat -nltp | grep 9042

tcp6       0      0 0.0.0.0:9042            :::*                    LISTEN      -

And I can telnet from my laptop to Cassandra:

$ telnet 10.X.X.X 9042
Trying 10.X.X.X...
Connected to ip-10-X-X-X.
Escape character is '^]'.

Relevant highlights from my cassandra.yaml:

start_native_transport: true
rpc_address: 0.0.0.0
rpc_port: 9160
broadcast_rpc_address: 10.X.X.X

/var/log/cassandra/system.log shows nothing useful.

edit: I can also connect to Cassandra using cqlsh 127.0.0.1 9042 on the server, and cqlsh 10.X.X.X 9042 from my laptop without any problems.


Solution

Looks like the one place where I tested my script (a Python console inside PyCharm) is, for some reason, the only place where my script does not work. No problems running as a script in PyCharm or in Python/iPython interactive mode. I will leave the question, as it contains a fairly complete checklist for Python-to-Cassandra connection troubleshooting.

2
Does 'nodetool status' show UN?phact
@phact: Yes, it does: UN 127.0.0.1 124.18 KB 256 100.0% 4d973e17-ae08-3aa1-81d2-605b92753694 rack1Def_Os
Hm can you turn on logging at the driverphact
This might be the cause of the problem in PyCharm: datastax-oss.atlassian.net/browse/PYTHON-289Tyler Hobbs
I put the machine ip instead of localhost in rcp_address; and then noticed the yaml snippet here;Alex Punnen

2 Answers

2
votes

Such exception can be thrown if a control connection timeout was exceeded. Try to increase it or set it to None to completely disable this timeout. For example:

from cassandra.cluster import Cluster
cluster = Cluster(['127.0.0.1'], control_connection_timeout=10,  port=9042)
cluster.connect()
1
votes

Here is a simple debug method for you: On another machine which has cassandra installed, using cassandra cli command "cqlsh 10.X.X.X" to connect the remote cassandra server. If failed , then you should modify cassandra configuration file for the cassandra remote mode.