2

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)
5
  • The default mongodb server port is 27017. Are you sure that it is really running on 20101? Commented Jun 10, 2015 at 11:32
  • using what command did you start your mongod connection Commented Jun 10, 2015 at 11:56
  • I tried with port no. 27017. But same error got. Commented Jun 10, 2015 at 12:22
  • i started connection with "net start mongod" but its also giving error like service name is invalid. Commented Jun 10, 2015 at 12:25
  • I get the same error message and I used default port 27017. from pymongo import MongoClient client = MongoClient('localhost:27017') db = client[db_name] Commented Apr 20, 2020 at 0:31

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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"

1 Comment

Thanks, I had the same problem and your solution worked with me :)
0

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

Comments

0

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

Comments

0

In order to run MongoDB,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.