我正在尝试将Python字典插入到Cassandra中。
但是当我尝试连接到数据库时出现NoHostAvailable
错误。
有人可以帮我弄清楚吗?
代码:
# cassandra server started in Terminal on Mac
from cassandra.cluster import Cluster
cluster = Cluster()
session = cluster.connect()
错误:
---------------------------------------------------------------------------
NoHostAvailable Traceback (most recent call last)
<ipython-input-21-dc85f20fd4f5> in <module>()
----> 1 session = cluster.connect()
/Users/a/anaconda/lib/python2.7/site-packages/cassandra/cluster.so in cassandra.cluster.Cluster.connect (cassandra/cluster.c:11030)()
/Users/a/anaconda/lib/python2.7/site-packages/cassandra/cluster.so in cassandra.cluster.Cluster.connect (cassandra/cluster.c:10872)()
/Users/a/anaconda/lib/python2.7/site-packages/cassandra/cluster.so in cassandra.cluster.Cluster.connect (cassandra/cluster.c:10760)()
/Users/a/anaconda/lib/python2.7/site-packages/cassandra/cluster.so in cassandra.cluster.ControlConnection.connect (cassandra/cluster.c:35173)()
/Users/a/anaconda/lib/python2.7/site-packages/cassandra/cluster.so in cassandra.cluster.ControlConnection._reconnect_internal (cassandra/cluster.c:36007)()
NoHostAvailable: ('Unable to connect to any servers', {'127.0.0.1': error(61, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})
警告:
WARNING:cassandra.cluster:[control connection] Error connecting to 127.0.0.1:
Traceback (most recent call last):
File "cassandra/cluster.py", line 2067, in cassandra.cluster.ControlConnection._reconnect_internal (cassandra/cluster.c:35763)
return self._try_connect(host)
File "cassandra/cluster.py", line 2087, in cassandra.cluster.ControlConnection._try_connect (cassandra/cluster.c:36224)
connection = self._cluster.connection_factory(host.address, is_control_connection=True)
File "cassandra/cluster.py", line 789, in cassandra.cluster.Cluster.connection_factory (cassandra/cluster.c:8980)
return self.connection_class.factory(address, self.connect_timeout, *args, **kwargs)
File "cassandra/connection.py", line 306, in cassandra.connection.Connection.factory (cassandra/connection.c:5051)
conn = cls(host, *args, **kwargs)
File "/Users/a/anaconda/lib/python2.7/site-packages/cassandra/io/libevreactor.py", line 264, in __init__
self._connect_socket()
File "cassandra/connection.py", line 340, in cassandra.connection.Connection._connect_socket (cassandra/connection.c:6123)
raise socket.error(sockerr.errno, "Tried connecting to %s. Last error: %s" % ([a[4] for a in addresses], sockerr.strerror or sockerr))
error: [Errno 61] Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused
ERROR:cassandra.cluster:Control connection failed to connect, shutting down Cluster:
Traceback (most recent call last):
File "cassandra/cluster.py", line 844, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:10760)
self.control_connection.connect()
File "cassandra/cluster.py", line 2041, in cassandra.cluster.ControlConnection.connect (cassandra/cluster.c:35173)
self._set_new_connection(self._reconnect_internal())
File "cassandra/cluster.py", line 2076, in cassandra.cluster.ControlConnection._reconnect_internal (cassandra/cluster.c:36007)
raise NoHostAvailable("Unable to connect to any servers", errors)
NoHostAvailable: ('Unable to connect to any servers', {'127.0.0.1': error(61, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})
答案 0 :(得分:0)
在连接到群集之前,您必须提供节点联系点和身份验证提供程序,或者您必须在localhost ip地址上运行cassandra。
试试这个:
ap = PlainTextAuthProvider(username=<username>,password=<password>)
cass_contact_points=[<ip address of your node>,..]
cluster = Cluster(cass_contact_points,auth_provider=ap)
session = cluster.connect(<keyspace name>)
答案 1 :(得分:0)
from cassandra.cluster import Cluster
cluster = Cluster(['127.0.0.1'])
session = cluster.connect('keyspace')