'Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s,
I am trying to locally connect to my db. I've established a connection to the database on MongoDB Compass, but when I run my simple code, I get this error:
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#
This is the code I am running:
import pymongo
from pymongo import MongoClient
client = pymongo.MongoClient()
db = client["Shopper_Info"]
my_collection = db["Names"]
shopper_data = {'name': 'Yoni', 'email': '[email protected]'}
my_collection.insert_one(shopper_data)
results= collection.find({"name": 'Yoni'})
for result in results:
print(result)
Solution 1:[1]
This problem was also happening in my stack, I had my connection string in an env file to connect to mongo atlas.
MONGO_URI=mongodb://<username>:<password>@cluster-details
But the right way to do this is
MONGO_URI="mongodb://<username>:<password>@cluster-details"
Solution 2:[2]
This happend to me as well. make sure that you have started the mongod server.that worked for me.
sudo service mongod start.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ARITRA GHOSH |
| Solution 2 | Doddi girish |
