pymongo.errors.OperationFailure: Authentication failed.

原因:mongodb需要用户名、密码登录,认证失败。

mongoDB有不同的认证机制,3.0版本以后采用的是'SCRAM-SHA-1', 之前的版本采用的是'MONGODB-CR'。

根据自己的情况给mechanism赋值。

 

import pymongo

client = MongoClient("mongodb://host:port/")

client.admin.authenticate("username","password",mechanism='SCRAM-SHA-1')(其中admin可以换成你的用户名能登录的库名)

db = client["dbname"]

collection = db["collname"]

 

posted @ 2017-04-26 15:07  那一剑的風情  阅读(6660)  评论(0)    收藏  举报