美文网首页
使用pymongo连接MongoDB

使用pymongo连接MongoDB

作者: 认真点啊 | 来源:发表于2018-06-03 22:53 被阅读0次

    使用pymongo库里的MongoClient连接MongoDB,
    创建连接对象只设置了IP和port

    import pymongo
    client = MongoClient(host='localhost',port=27017)
    

    然后出现以下的错误
    pymongo.errors.OperationFailure: not authorized on test to execute command
    原因是数据库开启了权限认证,连接对象没有使用认证
    解决如下:

    client = pymongo.MongoClient(host='localhost',port=27017,username='admin',password='12345',authSource='admin',authMechanism='SCRAM-SHA-1')
    

    其中authSource可以省略,默认是admin,是当前用户所在的数据库
    在MongoDB3.0及以上,默认使用SCRAM-SHA-1作为身份验证机制
    具体参考:http://api.mongodb.com/python/current/examples/authentication.html#support-for-special-characters-in-usernames-and-passwords

    相关文章

      网友评论

          本文标题:使用pymongo连接MongoDB

          本文链接:https://www.haomeiwen.com/subject/drpssftx.html