简体   繁体   中英

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)

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.

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"

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

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

In order to run MongoDB,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM