【问题标题】:pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]No connection could be made because the target machine actively refused itpymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]No connection could be made because the target machine主动拒绝它
【发布时间】:2020-07-05 10:41:45
【问题描述】:

我正在学习 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"

有人知道出了什么问题吗?

【问题讨论】:

  • 您的 mongod 服务是否正在运行,您可以访问 mongo shell 吗?
  • import pymongo 只加载库 - 它不检查 mongo 是否正在运行。
  • @Belly Buster 这些是本章的第一步。因为没有收到错误信息,所以知道mongoDB已经安装成功了。 w3schools.com/python/python_mongodb_query.asp
  • @DaveStSomeWhere 我已经安装了社区版。 MongoDB shell 仅包含在 enterprice 版本中。
  • 我现在在我的问题中添加了更多信息

标签: python database mongodb runtime-error pymongo


【解决方案1】:

您的代码没有任何问题。

如果您禁用了防火墙,最可能的原因是 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
>

【讨论】:

    【解决方案2】:

    尝试在 CLI 中运行此命令:

    mongod
    

    如果您发现任何错误,请观看此视频
    https://www.youtube.com/watch?v=FwMwO8pXfq0
    该视频名为“如何在 Windows 10 上安装 MongoDB”
    这个视频很有帮助

    【讨论】:

      【解决方案3】:

      我试图从 Django 连接。我正在使用 Python 3.7,之前尝试使用为 Python 3.6+ 提供的 URI 字符串,但它在 pyCharm 或 Jupyter 中不起作用。但后来我为 Python 3.4+ 版本选择了 URI 字符串,它在 Jupyter Notebook 中工作,但从 django 项目连接时出现问题。所以为了解决这个问题,我从https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2012plus-4.2.11-signed.msi 安装了 mongodb 应用程序,它在我的机器上启动了 mongoDB 服务。在不更改任何以前的代码的情况下,我可以连接。下面是我从 Django 应用程序连接到 MongoDB 的代码:

      DATABASES = {
          'default': {
              'ENGINE': 'djongo',
              'NAME': <DBname>,
              'HOST': "mongodb://<username>:<password>@cluster0-shard-00-00.pifkd.mongodb.net:27017,cluster0-shard-00-01.pifkd.mongodb.net:27017,cluster0-shard-00-02.pifkd.mongodb.net:27017/<DBname>?ssl=true&replicaSet=atlas-79xyw7-shard-0&authSource=admin&retryWrites=true&w=majority",
              'USER': <username>,
              'PASSWORD': <password>,
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-12-07
        • 2016-07-25
        • 1970-01-01
        • 2018-01-22
        • 2022-01-23
        • 2019-02-01
        • 2018-12-16
        • 2018-11-02
        • 2020-12-11
        相关资源
        最近更新 更多