[Solved] pymongo.errors.OperationFailure: Authentication failed.

Mongodb has different authentication mechanisms. After version 3.0, mongodb-cr is used, while before version, mongodb-cr is used

So, in my version, it’s obvious that ‘scratch-sha-1’ should be used

from pymongo import MongoClient
host = ''
client = MongoClient(host, 27017)
# connect to mydb database, account password authentication
db = client.admin # first connect to the system default database admin
# The following change is the key, I can not believe I tried successfully, I do not know why, first record the pit it stepped on
db.authenticate("root", "123456",mechanism='SCRAM-SHA-1') # let the admin database to authenticate the password login, well, since the success of
my_db = client.mydb # then connect to their own database mydb
collection = my_db.myset # myset collection, as explained above
collection.insert({"name": "zhangsan", "age":18}) # insert a data, if there is no error then the successful connection
for i in collection.find():

Similar Posts: