pymongo auth failed in python script

后端 未结 4 2051
不知归路
不知归路 2020-12-31 00:44

I have installed mongodb and enabled auth. and its working find. I can connect it from remote notebook using robomongo application:

Host: SERVER_IP
PORT: 270         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 00:51

    This worked for me ....

    import pymongo
    
    client = pymongo.MongoClient("mongodb://username:12%40password@ip:27017/sample_db",authSource="admin") 
    db = client['sample_db']
    

    Remember if you have special characters in your password (e.g. #,@) you will need to encode them (see %40) in the password. If you do not do authSource="admin" you will receive authentication errors. username - your mongodb username, ip - ip address as this assumes database is hosted on a remote server. sample_db is the database that you would like to access.

提交回复
热议问题