This tutorial describes the process for converting a standalone本教程描述了将独立mongod
instance into a replica set. mongod
实例转换为副本集的过程。Use standalone instances for testing and development, but always use replica sets in production.使用独立实例进行测试和开发,但在生产中始终使用副本集。
The procedure is specific to instances that are not part of a sharded cluster. 该过程特定于不属于分片集群的实例。To convert a shard standalone instance to a shard replica set, see Convert a Shard Standalone to a Shard Replica Set instead.要将分片独立实例转换为分片副本集,请参阅将分片独立转换为分片复制集。
To deploy a replica set without using a pre-existing 要在不使用预先存在的mongod
instance, see Deploy a Replica Set instead.mongod
实例的情况下部署副本集,请参阅部署副本集。
To install a standalone instance, see the installation tutorials.要安装独立实例,请参阅安装教程。
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 为了避免由于IP地址更改而导致的配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置副本集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. 使用主机名而不是IP地址来跨拆分网络范围配置群集。Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅使用IP地址配置的节点将无法通过启动验证,无法启动。
mongod
instance.mongod
实例。Restart the instance. 重新启动实例。Use the 使用--replSet
option to specify the name of the new replica set.--replSet
选项指定新副本集的名称。
For example, the following command starts a standalone instance as a member of a new replica set named 例如,以下命令作为名为rs0
. rs0
的新副本集的成员启动一个独立实例。The command uses the standalone's existing database path of 该命令使用单机版的现有数据库路径/srv/mongodb/db0
:/srv/mongodb/db0
:
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. 在绑定到非本地主机(例如,可公开访问的)IP地址之前,请确保已保护集群免受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和加强网络基础设施。
mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<hostname(s)|ip address(es)>
If your application connects to more than one replica set, each set must have a distinct name. 如果应用程序连接到多个副本集,则每个副本集必须具有不同的名称。Some drivers group replica set connections by replica set name.某些驱动程序按副本集名称对副本集连接进行分组。
For more information on configuration options, see Configuration File Options and the 有关配置选项的更多信息,请参阅配置文件选项和mongod
manual page.mongod
手册页面。
mongosh
to the mongod
instance.mongosh
连接到mongod
实例。Use 使用rs.initiate()
to initiate the new replica set:rs.initiate()
启动新副本集:
rs.initiate()
The replica set is now operational. 复制集现在可以运行。To view the replica set configuration, use 要查看副本集配置,请使用rs.conf()
. rs.conf()
。To check the status of the replica set, use 要检查副本集的状态,请使用rs.status()
.rs.status()
。
To add members to this replica set, use the 要将成员添加到此副本集,请使用rs.add()
method. rs.add()
方法。For more information on adding members to a replica set, see Add Members to a Replica Set.有关向副本集添加成员的详细信息,请参阅将成员添加到副本集。