Error in Python connection to mongodb pymongo.errors.OperationFailure : Authentication failed.

python connected mongo, appear pymongo. Errors. OperationFailure: Authentication failed. Error because authentication rights were not granted.
the initial join script is:


import pymongo
myclient = pymongo.MongoClient("mongodb//root:[email protected] :10000/")  xx.xx.xx.xx 是数据库服务器  root是数据库的用户名,123456是数据库的密码
mydb = myclient["test-db"]       #test-db是数据库名称 
mycol = mydb["cust_refund"]    #cust_refund是数据库的中一个数据表
for x in mycol.find({"Id":"80","testId":"1375"}):
  print(x)  

at first glance, as if that’s right, but using the statement above, at runtime, an error:
pymongo. Errors. OperationFailure: Authentication failed.

is viewed because authentication rights have not been granted.

At this point, add authenticate() function to authenticate as follows:

import pymongo
myclient = pymongo.MongoClient("mongodb://xx.xx.xx.xx :10000/")  xx.xx.xx.xx  是数据库服务器
mydb = myclient["test-db"]       #test-db是数据库名称 
mydb.authenticate('root','123456')
mycol = mydb["cust_refund"]    #cust_refund是数据库的中一个数据表
for x in mycol.find({"Id":"80","testId":"1375"}):
  print(x)  

so, once again, you can open the database.

more test communication, you can add friends

Read More: