python - Mongodb pymongo.errors.ServerSelectionTimeoutError : localhost:27017: [Errno 111] Connection refused, 超时: 30s,

标签 python mongodb localhost pymongo

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

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 60e2b31fab1da2bb146bb38c, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]> 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': '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c08190f083c1b111d1510521f1311" rel="noreferrer noopener nofollow">[email protected]</a>'}

my_collection.insert_one(shopper_data)

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

for result in results:
print(result)

最佳答案

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

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

但是正确的方法是

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

关于python - Mongodb pymongo.errors.ServerSelectionTimeoutError : localhost:27017: [Errno 111] Connection refused, 超时: 30s,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68252175/

相关文章:

python - 改变方程的 ast.NodeTransformer 示例

python - 根据值对列表进行编号的更优雅的方法

mongodb - Mongo _id 作为字符串索引键。是好是坏?

reactjs - 当 ngrok 尝试连接到 React 开发服务器时,主机 header 无效

c# - 无法从 UWP 应用程序中看到本地主机

java - 无法访问使用本地 ip 创建的 cookie

python - 无法使用 buildout 安装 geopy 0.96.1

python - 使用 SCons 的 VariantDir 和 Repository 来构建使用自定义生成器

node.js - 如何在node js mongodb中创建动态引用

mongodb - 可选在 .Find() MongoDB 查询中查找