以前的写法是这样的,

from pymongo import MongoClient
client = MongoClient(host='127.0.0.1')
db = client['dbname']
db.authenticate(user, password)

不过上面的已经废弃

warning:: Starting in MongoDB 3.6, calling :meth:authenticate
invalidates all existing cursors. It may also leave logical sessions
open on the server for up to 30 minutes until they time out.

from pymongo import MongoClient
client = MongoClient('127.0.0.1', username='user' password='password', authSource='dbname')

或者使用Mongodb的链接

uri = "mongodb://user:password@127.0.0.1:27017/?authSource=dbname&authMechanism=SCRAM-SHA-256"
client = MongoClient(uri)

&authMechanism=SCRAM-SHA-256 默认可以不加

参考: http://api.mongodb.com/python/current/examples/authentication.html

标签: mongodb

相关文章

添加新评论