【问题标题】:Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s,Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection denied, Timeout: 30s,
【发布时间】:2021-09-16 00:01:40
【问题描述】:

我正在尝试本地连接到我的数据库。我已经在 MongoDB Compass 上建立了与数据库的连接,但是当我运行我的简单代码时,我收到了这个错误:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111]连接被拒绝,超时:30s,拓扑描述: ]> root@LAPTOP-8OKVP35I:/portfolio/myProjects/webDevelopment/shopify_db#

这是我正在运行的代码:

import pymongo
from pymongo import MongoClient

client = pymongo.MongoClient()
db = client["Shopper_Info"]
my_collection = db["Names"]
shopper_data = {'name': 'Yoni', 'email': 'test@gmail.com'}

my_collection.insert_one(shopper_data)

results= collection.find({"name": 'Yoni'})

for result in results:
print(result)

【问题讨论】:

  • 您用于通过 Compass 进行连接的连接字符串是什么?
  • mongodb://localhost:27017
  • 这能回答你的问题吗? Pymongo keeps refusing the connection at 27017
  • 不,因为当我运行那些涉及 /var/lib/mongodb.lock 的命令时,它说没有这样的文件或目录。当尝试使用他们的命令重新启动服务时,它说没有找到服务
  • 该解决方案是平台相关的,但通用过程是相同的:清理锁定文件,重新启动 mongod。您在哪个平台上运行它,您通常如何启动 mongod?

标签: python mongodb localhost pymongo


【解决方案1】:

这个问题也发生在我的堆栈中,我将连接字符串放在一个 env 文件中以连接到 mongo atlas。

MONGO_URI=mongodb://<username>:<password>@cluster-details

但正确的做法是

MONGO_URI="mongodb://<username>:<password>@cluster-details"

【讨论】:

    【解决方案2】:

    这也发生在我身上。 确保您已经启动了 mongod 服务器。这对我有用。

    sudo service mongod start.
    

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 2019-10-06
      • 2020-10-18
      • 2020-05-16
      • 2018-03-30
      • 2021-12-16
      • 2013-01-03
      • 1970-01-01
      • 2018-03-05
      相关资源
      最近更新 更多