我正在尝试使用教程命令连接到Neo4j服务器但是我收到以下错误:
from py2neo import Graph
g = Graph(host='localhost', port=7687)
tx = g.begin()
a = Node("Person", name="Alice")
tx.create(a)
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
tx.create(ab)
tx.commit()
print(g.exists(ab))
块引用
---------------------------------------------------------------------------
ConnectionRefusedError Traceback (most recent call last)
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connection.py in _new_conn(self)
140 conn = connection.create_connection(
--> 141 (self.host, self.port), self.timeout, **extra_kw)
142
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\util\connection.py in create_connection(address, timeout, source_address, socket_options)
82 if err is not None:
---> 83 raise err
84
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\util\connection.py in create_connection(address, timeout, source_address, socket_options)
72 sock.bind(source_address)
---> 73 sock.connect(sa)
74 return sock
ConnectionRefusedError:[WinError 10061]无法建立连接 因为目标机器主动拒绝它
在处理上述异常期间,发生了另一个异常:
NewConnectionError Traceback (most recent call last)
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
600 body=body, headers=headers,
--> 601 chunked=chunked)
602
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
356 else:
--> 357 conn.request(method, url, **httplib_request_kw)
358
~\Anaconda3\envs\wavelength\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
1238 """Send a complete request to the server."""
-> 1239 self._send_request(method, url, body, headers, encode_chunked)
1240
~\Anaconda3\envs\wavelength\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
1284 body = _encode(body, 'body')
-> 1285 self.endheaders(body, encode_chunked=encode_chunked)
1286
~\Anaconda3\envs\wavelength\lib\http\client.py in endheaders(self, message_body, encode_chunked)
1233 raise CannotSendHeader()
-> 1234 self._send_output(message_body, encode_chunked=encode_chunked)
1235
~\Anaconda3\envs\wavelength\lib\http\client.py in _send_output(self, message_body, encode_chunked)
1025 del self._buffer[:]
-> 1026 self.send(msg)
1027
~\Anaconda3\envs\wavelength\lib\http\client.py in send(self, data)
963 if self.auto_open:
--> 964 self.connect()
965 else:
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connection.py in connect(self)
165 def connect(self):
--> 166 conn = self._new_conn()
167 self._prepare_conn(conn)
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connection.py in _new_conn(self)
149 raise NewConnectionError(
--> 150 self, "Failed to establish a new connection: %s" % e)
151
NewConnectionError ::无法建立新连接:[WinError 10061]无法建立连接,因为目标机器是主动的 拒绝了
在处理上述异常期间,发生了另一个异常:
MaxRetryError Traceback (most recent call last)
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\http.py in request(self, method, url, fields, headers, **urlopen_kw)
188 try:
--> 189 return self._http.request(method, url, fields, headers, **urlopen_kw)
190 except MaxRetryError:
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\request.py in request(self, method, url, fields, headers, **urlopen_kw)
69 headers=headers,
---> 70 **urlopen_kw)
71
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\request.py in request_encode_body(self, method, url, fields, headers, encode_multipart, multipart_boundary, **urlopen_kw)
147
--> 148 return self.urlopen(method, url, **extra_kw)
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
667 release_conn=release_conn, body_pos=body_pos,
--> 668 **response_kw)
669
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
667 release_conn=release_conn, body_pos=body_pos,
--> 668 **response_kw)
669
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
667 release_conn=release_conn, body_pos=body_pos,
--> 668 **response_kw)
669
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
638 retries = retries.increment(method, url, error=e, _pool=self,
--> 639 _stacktrace=sys.exc_info()[2])
640 retries.sleep()
~\Anaconda3\envs\wavelength\lib\site-packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
387 if new_retry.is_exhausted():
--> 388 raise MaxRetryError(_pool, url, error or ResponseError(cause))
389
MaxRetryError:HTTPConnectionPool(host ='localhost:7687',port = None): 使用url:/ db / data / transaction超出最大重试次数(由 NewConnectionError(':无法建立新连接:[WinError 10061]无法建立连接,因为目标机器是主动的 拒绝了,'))
在处理上述异常期间,发生了另一个异常:
ServiceUnavailable Traceback (most recent call last)
<ipython-input-20-4018ad364426> in <module>()
1 tx = g.begin()
2 a = Node("Person", name="Alice")
----> 3 tx.create(a)
4 b = Node("Person", name="Bob")
5 ab = Relationship(a, "KNOWS", b)
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\graph.py in create(self, subgraph)
933 raise TypeError("No method defined to create object %r" % subgraph)
934 else:
--> 935 create(self)
936
937 def degree(self, subgraph):
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\types.py in __db_create__(self, tx)
331 statement = "\n".join(reads + writes + ["RETURN %s LIMIT 1" % ", ".join(returns)])
332 tx.entities.append(returns)
--> 333 list(tx.run(statement, parameters))
334
335 def __db_degree__(self, tx):
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\graph.py in __iter__(self)
1110
1111 def __iter__(self):
-> 1112 while self.forward():
1113 yield self._current
1114
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\graph.py in forward(self, amount)
1169 fetch = self._result.fetch
1170 while moved != amount:
-> 1171 new_current = fetch()
1172 if new_current is None:
1173 break
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\graph.py in fetch(self)
778 """
779 try:
--> 780 return next(self.result_iterator)
781 except StopIteration:
782 return None
~\Anaconda3\envs\wavelength\lib\site-packages\neo4j\v1\api.py in records(self)
435 hydrate = self.value_system.hydrate
436 zipper = self.zipper
--> 437 keys = self.keys()
438 records = self._records
439 while records:
~\Anaconda3\envs\wavelength\lib\site-packages\neo4j\v1\api.py in keys(self)
425 """
426 while self._keys is None and self.online():
--> 427 self.fetch()
428 return self._keys
429
~\Anaconda3\envs\wavelength\lib\site-packages\neo4j\v1\api.py in fetch(self)
407 """
408 if self.online():
--> 409 return self._session.fetch()
410 else:
411 return 0
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\http.py in fetch(self)
295
296 def fetch(self):
--> 297 return self.sync()
298
299 def sync(self):
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\http.py in sync(self)
306 count = 0
307 try:
--> 308 response = self.post(ref, {"statements": self._statements}, expected=(OK, CREATED))
309 if response.status == 201:
310 location_path = urlsplit(response.headers["Location"]).path
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\http.py in post(self, ref, json, expected)
210 headers["Content-Type"] = "application/json"
211 json = json_dumps(json).encode('utf-8')
--> 212 rs = self.request("POST", self.path + ref, headers=self._headers, body=json)
213 if rs.status not in expected:
214 raise_error(self.uri, rs.status, rs.data)
~\Anaconda3\envs\wavelength\lib\site-packages\py2neo\http.py in request(self, method, url, fields, headers, **urlopen_kw)
189 return self._http.request(method, url, fields, headers, **urlopen_kw)
190 except MaxRetryError:
--> 191 raise ServiceUnavailable("Cannot send %r request to %r" % (method, url))
192
193 def get_json(self, ref):
ServiceUnavailable:无法发送“POST”请求 '/分贝/数据/交易'
答案 0 :(得分:2)
解决了这个问题,我的一个非常小的错误 - 我必须在运行上述命令之前初始化服务器。为“狼来了”道歉
答案 1 :(得分:0)
也许您没有正确进行身份验证。您可以尝试以下方法:
from neo4j.v1 import GraphDatabase
uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "your password"))
with driver.session() as session:
with session.begin_transaction() as tx:
for record in tx.run("your cypher query"):
{process the response}
答案 2 :(得分:0)
我正在将Neo4j Community 4.0与最新的官方Neo4j Bolt驱动程序配合使用,并且遇到相同的错误。
为了摆脱它,我不得不在neo4j.conf
中取消注释以下设置,然后重新启动Neo4j服务:
# To accept non-local connections, uncomment this line:
dbms.default_listen_address=0.0.0.0
不确定Neo4J服务器为什么将来自本地驱动程序会话的连接视为远程连接,但是对我有用。