簡體   English   中英

Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection denied, Timeout: 30s,

[英]Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s,

我正在嘗試本地連接到我的數據庫。 我已經在 MongoDB Compass 上建立了與數據庫的連接,但是當我運行我的簡單代碼時,我收到了這個錯誤:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection denied, Timeout: 30s, Topology Description: <TopologyDescription id: 60e2b31fab1da2bb146bb38c, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt:無,error=AutoReconnect('localhost:27017: [Errno 111] Connection denied')>]> 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)

這個問題也發生在我的堆棧中,我將連接字符串放在一個 env 文件中以連接到 mongo atlas。

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

這也發生在我身上。 確保您已經啟動了對我有用的 mongod 服務器。

sudo service mongod start.

只需將上述代碼添加到 django 中的 settings.py 中:-

DATABASE = {
'default': {
'ENGINE': 'djongo',
 "CLIENT": {
           "name": <your_database_name>,
           "host": <your_connection_string>,
           "username": <your_database_username>,
           "password": <your_database_password>,
           "authMechanism": "SCRAM-SHA-1",
        },
    }
}

但在主機中,您可能會遇到此問題:-“pymongo.errors.ServerSelectionTimeoutError:”[SSL: CERTIFICATE_VERIFY_FAILED] 證書驗證失敗:無法獲取本地頒發者證書(_ssl.c:997)所以為此您添加如下:-“mongodb +srv://sampleUser:samplePassword@cluster0-gbdot.mongodb.net/sampleDB??ssl=true&ssl_cert_reqs=CERT_NONE&retryWrites=true&w=majority"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM