pymongo升级4.0后的认证问题

  最近pip install pymono后,连接时认证出现问题,报了TypeError: 'Collection' object is not callable. If you meant to call the 'authenticate' method on a 'Database' object it is failing because no such method exists.的错

 

Traceback (most recent call last):
  File "D:/work/py/test.py", line 37, in <module>
    admin.authenticate("root", "123456")
  File "C:\python3\lib\site-packages\pymongo\collection.py", line 2583, in __call__
    self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the 'authenticate' method on a 'Database' object it is failing because no such method exists.

 

 

  之前一直这样连接是没有问题的,于是再想是不是版本问题,查找一通文档后,确实是pymongo升级到4.0后的认证方式改变

  官方文档中解释如下:

     

  

  之前的认证方式:

  

client = pymongo.MongoClient("127.0.0.1", 43127)
admin = client.get_database("admin")   # 连接所需数据库
admin.authenticate("root", "123456")

  解决办法:

  1、pymongo降到4.0以下

    pip uninstall pymongo

    pip install pymongo==3.xx.xx

 

  2、更改为新的认证方式:

    client = pymongo.MongoClient("127.0.0.1", 43127, username="root", password="123456")

  参考:

    https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html#database-authenticate-and-database-logout-are-removed

 

 

 

 
posted @ 2022-01-10 09:42  emunshe  阅读(2514)  评论(0编辑  收藏  举报