简体   繁体   English

如何重命名 mongodb 副本集?

[英]How do I rename a mongodb replica set?

I started up a few MongoDB replica sets with the default configuration and added them to Cloud Manager , but since the default replica set name is "rs0", they're hard to tell apart in the UI.我使用默认配置启动了一些 MongoDB 副本集并将它们添加到Cloud Manager ,但由于默认副本集名称是“rs0”,因此很难在 UI 中区分它们。 Is there a way to change the name of a replica set, preferably without having to drop and re-import all of the data in the cluster?有没有办法更改副本集的名称,最好不必删除并重新导入集群中的所有数据?

I've tried using rs.reconfig ( link ) but it doesn't allow you to change the replica set name.我试过使用rs.reconfig ( link ) 但它不允许您更改副本集名称。

Yes!是的! The process is pretty simple:这个过程非常简单:

  1. Start all of the nodes in the non-replicated mode以非复制模式启动所有节点
    • Stop mongod on each server在每台服务器上停止mongod
    • Start mongod back up.启动mongod备份。 If you use /etc/mongod.conf , remove the replication section.如果您使用/etc/mongod.conf ,请删除replication部分。 If you don't, omit the --replSet option to mongod如果不这样做,请省略mongod--replSet选项
  2. Flush the local database where the replication set configuration is cached刷新缓存复制集配置的local数据库

    • On each server, open a mongo shell as the admin user and run use local; db.dropDatabase()在每台服务器上,以 admin 用户身份打开一个 mongo shell 并运行use local; db.dropDatabase() use local; db.dropDatabase() (Make sure that admin user / root user has dbAdmin role on local db) use local; db.dropDatabase() (确保管理员用户/root 用户在local数据库上具有dbAdmin角色)
  3. Start all of the nodes again in replicated mode以复制模式再次启动所有节点

    • Stop mongod on each server在每台服务器上停止mongod
    • If you use /etc/mongod.conf , add the replication section back in with the new name and start mongod .如果您使用/etc/mongod.conf ,请使用名称重新添加replication部分并启动mongod If not, start mongod with --replSet <new-name>如果没有,请使用--replSet <new-name>启动mongod
  4. Initialize the replica set初始化副本集
    • Open a mongo shell as the admin user on one of the nodes.在其中一个节点上以管理员用户身份打开 mongo shell。 (It will become the new primary) (它将成为新的小学)
    • Run rs.initiate() .运行rs.initiate() DO NOT pass any arguments to rs.initiate() .不要将任何参数传递给rs.initiate() (It'll fail with an error) Any other config you want to set can be changed using rs.reconfig() later. (它会因错误而失败rs.reconfig()稍后可以使用rs.reconfig()更改您要设置的任何其他配置。
    • On the same node where your ran rs.initiate() , for each secondary, run rs.add('[secondary.host.name]') to add it to the replica set.在您运行rs.initiate()的同一节点上,对于每个辅助节点,运行rs.add('[secondary.host.name]')以将其添加到副本集。
    • Wait for the secondaries to come in sync等待辅助节点同步

This doesn't require you to dump and re-import your data, and it can be done with minimal downtime (and a period of degraded performance as the secondaries sync) if you automate it.这不需要您转储和重新导入您的数据,如果您将其自动化,则可以在最短的停机时间(以及辅助同步时性能下降的一段时间)内完成。

Here's an ansible playbook that does the whole thing (assuming you're using /etc/mongod.conf and managing mongod via System V/Upstart/things that speak service .)这是一个完整的剧本(假设您正在使用/etc/mongod.conf并通过 System V/Upstart/things that speak service管理mongod 。)

You could remove the existing replica set by following below steps and then can start the mongods with new replica set您可以按照以下步骤删除现有的副本集,然后可以使用新的副本集启动 mongods

  1. start all mongods without --replSet在没有 --replSet 的情况下启动所有 mongod

    mongod --dbpath data/rs3 --port 27019 --oplogSize 64 mongod --dbpath 数据/rs3 --port 27019 --oplogSize 64
    Note: - Above command is for windows machine注意: - 以上命令适用于 windows 机器

  2. start mongo for all the respective mongods remove the existing replica set use local为所有相应的 mongod 启动 mongo 删除现有的副本集使用本地
    db.system.replset.remove({_id:'OldReplicaSetName'}) db.system.replset.remove({_id:'OldReplicaSetName'})

  3. start each mongod with the new replica set with --replSet ex - mongod --dbpath data/rs3 --port 27019 --oplogSize 64 --replSet mySet Note: - Above command is for windows machine使用 --replSet ex - mongod --dbpath data/rs3 --port 27019 --oplogSize 64 --replSet mySet 用新的副本集启动每个 mongod 注意:- 以上命令适用于 Windows 机器

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

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