EuRBamarth
EuRBamarth

Reputation: 974

PyMongo - can't connect to localhost?

I have tried running the following:

import pymongo
from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017/')
db = client.test_database
collection = db.test_collection
collection.find_one()

but I get

ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

Any suggestions on how to fix this? I am running this behind a corporate proxy, but have already set environment variables for that in .bashrc.

EDIT

If I run mongo from the terminal, I get

$ mongo
MongoDB shell version v4.0.13
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2019-10-16T10:30:23.269+0100 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6
exception: connect failed

Upvotes: 0

Views: 4398

Answers (1)

Reznik
Reznik

Reputation: 2806

Its look like you didn't run any mongo instance on your machine

run the command mongod -f <config_file> and then test it again (this may needs a sudo permissions)

Upvotes: 1

Related Questions