Docs HomeMongoDB Manual

Convert a Standalone mongod to a Replica Set将独立mongod转换为副本集

A standalone mongod instance is useful for testing and development. 一个独立的mongod实例对于测试和开发非常有用。A standalone instance isn't a good choice for a production deployment because it can be a single point of failure. 对于生产部署来说,独立实例不是一个好的选择,因为它可能是一个单点故障。A replica set, also known as a cluster, provides redundancy and availability. Always use a replica set in production.复制副本集(也称为群集)提供冗余和可用性。在生产中始终使用复制副本集。

If you have a standalone server with data that you want to use in production, convert the standalone server to a replica set first.如果您有一个包含要在生产中使用的数据的独立服务器,请先将该独立服务器转换为副本集。

Important

If you convert a development server to a replica set for production use, consult the security checklist before you expose your cluster to the internet.如果将开发服务器转换为用于生产的副本集,请在将集群暴露在internet之前查阅安全检查表

Before You Begin开始之前

Before you convert your standalone instance, consider whether a replica set or a sharded cluster is more appropriate for your workload.在转换独立实例之前,请考虑副本集还是分片集群更适合您的工作负载。

A sharded cluster is a special kind of cluster. A sharded cluster provides redundancy and availability; it also distributes data across shards. Shards are usually hosted on multiple servers and allow for horizontal scaling.分片集群是一种特殊的集群。分片集群提供了冗余和可用性;它还跨分片分发数据。分片通常托管在多个服务器上,并允许水平扩展。

Procedure过程

1

Shut down the standalone instance.关闭独立实例。

Use mongosh to connect to your mongod instance.使用mongosh连接到您的mongod实例。

mongosh

Switch to the admin database and run shutdown.切换到admin数据库并运行shutdown

use admin
db.adminCommand(
{
shutdown: 1,
comment: "Convert to cluster"
}
)
2

Name the replica set.命名复制副本集。

If you configure your mongod instance from the command line, use the --replSet option to set a name for your replica set.如果从命令行配置mongod实例,请使用--replSet选项为副本集设置名称。

A typical command line invocation might include:典型的命令行调用可能包括:

Purpose意图Option选项
Cluster name群集名称--replSet
Network details网络详细信息--port
Data path数据路径--dbpath
Authentication details身份验证详细信息--authenticationDatabase, --username, --password

Update the example code with the settings for your deployment.使用部署的设置更新示例代码。

mongod --replSet rs0 \
--port 27017 \
--dbpath /path/to/your/mongodb/dataDirectory \
--authenticationDatabase "admin" \
--username "adminUserName" \
--password

If you use a configuration file to start mongodb, add a replication section to your configuration file. Edit the replSetName value to set the name of your replica set.如果使用配置文件启动mongodb,请在配置文件中添加一个replication部分。编辑replSetName值以设置复制副本集的名称。

replication:
replSetName: rs0
3

Initialize the replica set.初始化复制副本集。

To initialize the replica set, use mongosh to reconnect to your server instance. 要初始化副本集,请使用mongosh重新连接到服务器实例。Then, run rs.initiate().然后,运行rs.initiate()

rs.initiate()

You only have to initiate the replica set once.您只需要启动复制副本集一次。

To view the replica set configuration, use rs.conf().要查看副本集配置,请使用rs.conf()

To check the status of the replica set, use rs.status().要检查复制副本集的状态,请使用rs.status()

4

Add nodes to the replica set.将节点添加到复制副本集中。

The new replica set has a single, primary node. The next step is to add new nodes to the replica set. Review the documentation on clusters before you add additional nodes:新的复制副本集只有一个主节点。下一步是将新节点添加到复制副本集中。在添加其他节点之前,请查看有关群集的文档:

When you are ready to add nodes, use rs.add().当您准备好添加节点时,请使用rs.add()

5

Update Your Application Connection String.更新您的应用程序连接字符串。

After you convert the mongod to a replica set, update the connection string used by your applications to the connection string for your replica set. Then, restart your applications.mongod转换为副本集后,将应用程序使用的连接字符串更新为副本集的连接字符串。然后,重新启动应用程序。

Learn More了解更多信息