Bhautik Patel
Bhautik Patel

Reputation: 31

mongodb refusing connection in python

I am using windows8, for writing code I use IDLE. I tried to connect python to mongodb. But when trying to get collection name than it gives an error.

ServerSelectionTimeoutError: localhost:20101: [Errno 10061] No connection could be made because the target machine actively refused it

This is code for which i am getting an error.

from pymongo import MongoClient
connection = MongoClient('localhost',20101)
db = connection['Bhautik']
collection = db['Student']
db.collection_names(include_system_collections=True)

Upvotes: 2

Views: 14667

Answers (5)

Gihan Gamage
Gihan Gamage

Reputation: 3364

In order to run MongoDB,

Upvotes: 0

Karam Qusai
Karam Qusai

Reputation: 799

Some times it's gives this error when you forgot to run the local server (if it's run with local server).

To run it you need to write on your terminal:

mongod

or, if MongoDB not in PATH, you can find it via this link in your computer:

C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe

Upvotes: 0

Peko Chan
Peko Chan

Reputation: 364

You can try run mongo like that:

"C:\\Program Files\\MongoDB\\Server\\3.6\\bin\\mongod.exe" --dbpath E:\\data\\db --port 27017 --bind_ip 127.0.0.1

E:\data\db should be your location path then in you code it will lok like

client = MongoClient("127.0.0.1", 27017)
db = client['addsome']

datas = db.follow_up

and if you want to access from a distant machine make sure you open the port "27017" in the firewall

Upvotes: 0

HakSoo Kim
HakSoo Kim

Reputation: 21

If you are starting to use mongoDB after installation, make C:/data/db because it is a default database directory which mongoDB uses.

To change the database directory, do type below:

C:\Program Files\MongoDB\Server\3.x\bin> mongod --dbpath "c:\custom_folder"

Upvotes: 2

chicao
chicao

Reputation: 86

By the output message you probably didn't set your mongo bind_ip or didn't set the dbpath. Try this:

mongod --dbpath <database_path> --bind_ip 127.0.0.1 --port 20101

It would be more helpful to put alongside with your code some information regarding the mongodb configuration, like the server port, if you are using authentication or not, which dbpath you are using and so on.

So put in your question your mongodb.conf (if you are using one) or the command you are using to start the mongo server.

Upvotes: 2

Related Questions