翻译或纠错本页面

对复制集节点进行维护

On this page

概述

Replica sets 的存在,使得MongoDB在维护计划的时候也可以保持可用。

本文是对复制集中各个成员的维护过程进的概述。此外,可以通过节点维护的顺序来减少 primary 主节点不可用的时间和控制受影响的时间。

Use these steps as the basis for common replica set operations, particularly for procedures such as upgrading to the latest version of MongoDB and changing the size of the oplog.

流程

如果要维护复制集中每个节点,我们应该先从从节点开始,然后依次维护完所有从节点,最后在对主节点进行维护:

  • Restart the mongod instance as a standalone.
  • 在单节点实例上执行任务。

  • Restart the mongod instance as a member of the replica set.
1

Stop a secondary.

In the mongo shell, shut down the mongod instance:

db.shutdownServer()
2

Restart the secondary as a standalone on a different port.

At the operating system shell prompt, restart mongod as a standalone instance running on a different port and without the --replSet parameter:

mongod --port 37017 --dbpath /srv/mongodb

Always start mongod with the same user, even when restarting a replica set member as a standalone instance.

3

Perform maintenance operations on the secondary.

While the member is a standalone, use the mongo shell to perform maintenance:

mongo --port 37017
4

Restart mongod as a member of the replica set.

After performing all maintenance tasks, use the following procedure to restart the mongod as a member of the replica set on its usual port.

From the mongo shell, shut down the standalone server after completing the maintenance:

db.shutdownServer()

Restart the mongod instance as a member of the replica set using its normal command-line arguments or configuration file.

The secondary takes time to catch up to the primary. From the mongo shell, use the following command to verify that the member has caught up from the RECOVERING state to the SECONDARY state.

rs.status()
5

Perform maintenance on the primary last.

To perform maintenance on the primary after completing maintenance tasks on all secondaries, use rs.stepDown() in the mongo shell to step down the primary and allow one of the secondaries to be elected the new primary. Specify a 300 second waiting period to prevent the member from being elected primary again for five minutes:

rs.stepDown(300)

After the primary steps down, the replica set will elect a new primary. See 复制集选举 for more information about replica set elections.