简体   繁体   English

MongoDB:无法启动副本集; '已有数据,无法启动设置'

[英]MongoDB: Can't initiate replica set; 'has data already, cannot initiate set'

I've got a MongoDB instance, set up using a config file and a key file . 我有一个MongoDB实例, 使用配置文件和密钥文件进行设置

I'd like to initiate a replica set using pymongo . 我想用pymongo启动一个副本集 When I attempt to initiate the replcia set, by executing a python script against the server which will become the replica set primary, as such: 当我尝试启动replcia集时,通过对将成为副本集主服务器的服务器执行python脚本,如下:

from pymongo import MongoClient

uri = "mongodb://correctWorkingUsername:password@localhost:27017"
c = MongoClient(uri)

config = {'_id': 'RelicaSetName', 'members': [
{'_id': 0, 'host': 'FirstServer:27017'},
{'_id': 1, 'host': 'SecondServer:27017'},
{'_id': 2, 'host': 'ThirdServer:27017'}]}

c.admin.command("replSetInitiate", config)

I get an error message, the following: 我收到一条错误消息,如下:

'SecondSErver:27017' has data already, cannot initiate set

However, if I authenticate to the database using 但是,如果我使用数据库进行身份验证

mongo admin -u correctWorkingUsername -p password

I can initiate the replication, and successfully add members: 我可以启动复制,并成功添加成员:

rs.initiate()
rs.add('SecondServer:27017')

I was unsure if this was related to the keyfile authentication, or the fact that the users were ALREADY created on the other servers by a script. 我不确定这是否与密钥文件身份验证有关,或者是否由用户脚本在其他服务器上创建了ALREADY。 Each server has also been started with a config file, mongod.conf, which contains a replica set name. 每个服务器也都使用配置文件mongod.conf启动,该文件包含副本集名称。

Why is this failing? 为什么这会失败? The rs.initiate() and rs.add() work perfectly, but the python script does not work though it can infact connect to the database. rs.initiate()和rs.add()完美地工作,但python脚本虽然可以实际连接到数据库但不起作用。

When you initialize the full replica set at once, every node should not have data. 一次初始化完整副本集时,每个节点都不应该有数据。

When you turn an existing single node into a replica set, its data becomes the replica set data, and then adding additional nodes will replicate data to them wiping out their existing data. 将现有单个节点转换为副本集时,其数据将成为副本集数据,然后添加其他节点将向其复制数据,从而消除其现有数据。

What you should be doing is starting up the nodes, initializing the replica set and then creating users (only on the primary). 您应该做的是启动节点,初始化副本集, 然后创建用户(仅在主节点上)。

the users were ALREADY created on the other servers by a script 用户通过脚本在其他服务器上创建了ALREADY

This is the issue at the core - the only way this could have been done on an uninitialized member of a replica set is if it was first brought up as non-replica set node, users were added and then it was restarted with the replSet option. 这是核心问题 - 在副本集的未初始化成员上完成此操作的唯一方法是,如果它首先作为非副本集节点启动,则添加用户然后使用replSet选项重新启动它。 That's not the correct sequence to follow. 这不是正确的顺序。 For correct list of steps check the docs . 有关正确的步骤列表, 请查看文档

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

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