我正在尝试本地连接到我的数据库。我已经在 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/