gpt4 book ai didi

python - pymongo.errors.ServerSelectionTimeoutError :localhost:27017:[WinError 10061]No connection could be made because the target machine actively refused it

转载 作者:行者123 更新时间:2023-12-04 11:47:27 27 4
gpt4 key购买 nike

我正在关注 w3schools 的 python 教程。我刚刚开始了 mongoDB 章节。我安装了 mongoDB 并检查了它:

import pymongo 

没有得到错误。

但是一旦我输入以下代码:
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway 37" }

x = mycol.insert_one(mydict)

print(x.inserted_id)

我在 cmd 底部收到这些消息和错误消息:

C:\Users\xxx\myname

C:\Users\xxx\myname>python index.py
Traceback (most recent call last):
File "index.py", line 8, in <module>
x = mycol.insert_one(mydict)
File "C:\Users\path...\pymongo\collection.py", line 695, in insert_one
self._insert(document,
File "C:\Users\path...\pymongo\collection.py", line 610, in _insert
return self._insert_one(
File "C:\Users\path...\pymongo\collection.py", line 599, in _insert_one
self.__database.client._retryable_write(
File "C:\Users\path...\pymongo\mongo_client.py", line 1490, in _retryable_write
with self._tmp_session(session) as s:
File "C:\Program Files\WindowsApps\path...\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "C:\Users\path...\pymongo\mongo_client.py", line 1823, in _tmp_session
s = self._ensure_session(session)
File "C:\Users\path...\pymongo\mongo_client.py", line 1810, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "C:\Users\path...\pymongo\mongo_client.py", line 1763, in __start_session
server_session = self._get_server_session()
File "C:\Users\path...\pymongo\mongo_client.py", line 1796, in _get_server_session
return self._topology.get_server_session()
File "C:\Users\path...\pymongo\topology.py", line 482, in get_server_session
self._select_servers_loop(
File "C:\Users\path...\pymongo\topology.py", line 208, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost: 27017: [WinError 10061] Could not connect because target computer actively refused connection

C:\Users\xxx\myname


我也尝试过暂时禁用防火墙,但错误不断出现。

我用了:
"python 3.8.2
, mongoDB 4.2.5.0
, pymongo 3.10.1
, windows 10 home"

有谁知道出了什么问题?

最佳答案

您的代码没有任何问题。

如果您已禁用防火墙,最可能的原因是未安装或运行 MongoDB 服务。在 Windows 上按 windows 键并输入 services打开服务应用程序。查看服务MongoDB Server已上市并有 Running地位。

您可以通过打开您最喜欢的 Windows 终端或 powershell 并输入 mongo 来测试本地连接。 .如果它正在工作,您应该看到:

PS> mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1b5499b8-166a-4de6-a8c9-643499f04e66") }
MongoDB server version: 4.2.3
>

关于python - pymongo.errors.ServerSelectionTimeoutError :localhost:27017:[WinError 10061]No connection could be made because the target machine actively refused it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60839470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com