简体   繁体   English

尝试启动pymongo中的副本集时出现身份验证错误

[英]authentication error trying to initiate replica set in pymongo

Trying to initiate a mongodb replica set from a python script, and running into an error with authentication. 尝试从python脚本启动mongodb副本集,并在身份验证时遇到错误。

from pymongo import MongoClient

ip = 'zzz.zz.zzz.zz'
port = 27017
replica_set = 'yomama'

config = {
  '_id': replica_set,
  'members': [
    {'host': 'xxx.xx.xxx.xx',
     '_id': 0,
     'arbiterOnly': False},
    {'host': 'yyy.yy.yyy.yy',
     '_id': 1,
     'arbiterOnly': False},
    {'host': 'zzz.zz.zzz.zz',
     '_id': 2,
     'arbiterOnly': False},
  ],
}

connection = MongoClient(ip, port)
connection.admin.command('replSetInitiate', config)

Running this script results in: 运行此脚本将导致:

pymongo.errors.OperationFailure: command SON([('replSetInitiate', {'_id': 'yomama', 'members': [{'host': 'xxx.xx.xxx.xx', '_id': 0, 'arbiterOnly': False}, {'host': 'yyy.yy.yyy.yy', '_id': 1, 'arbiterOnly': False}, {'host': 'zzz.zz.zzz.zz', '_id': 2, 'arbiterOnly': False}]})])
failed: unauthorized

There is no authentication set up on the database, and running the same thing from a mongo shell works fine: 在数据库上没有设置身份验证,并且从mongo shell运行相同的东西可以正常工作:

db.runCommand({replSetInitiate:{'_id': 'yomama', 'members': [{'host': 'xxx.xx.xxx.xx', '_id': 0, 'arbiterOnly': false}, {'host': 'yyy.yy.yyy.yy', '_id': 1, 'arbiterOnly': false}, {'host': 'zzz.zz.zzz.zz', '_id': 2, 'arbiterOnly': false}]}})
{
    "info" : "Config now saved locally.  Should come online in about a minute.",
    "ok" : 1
}

Any thoughts on why this error when doing the same thing with pymongo? 有什么想法为什么在使用pymongo做相同的事情时会出现此错误?

Figured it out. 弄清楚了。 This was happening because by specifying the ip and port while initializing MongoClient ( connection = MongoClient(ip, port) ), the client did not bypass authentication using the localhost exception . 发生这种情况的原因是,通过在初始化MongoClient时指定ip和端口( connection = MongoClient(ip, port) ),客户端未使用localhost异常绕过身份验证。 Initializing using localhost does the trick, eg connection = MongoClient() . 使用localhost进行初始化很connection = MongoClient() ,例如, connection = MongoClient()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM