Database Manual / Self-Managed Deployments / Deploy and Manage Self-Managed Replica Sets / Deploy

Convert a Standalone Self-Managed 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.如果将开发服务器转换为副本集以供生产使用,请在将集群暴露给互联网之前参考安全检查表

You can easily migrate from a standalone server to a MongoDB Atlas cluster. MongoDB Atlas is the fully managed service for MongoDB deployments in the cloud. 您可以轻松地从单机版服务器迁移到MongoDB Atlas集群。MongoDB Atlas是云中MongoDB部署的完全托管服务。To learn more, see Migrate or Import Data in the MongoDB Atlas documentation.要了解更多信息,请参阅MongoDB Atlas文档中的迁移或导入数据

About This Task关于此任务

Server Architecture服务器架构

This tutorial uses the following servers:本教程使用以下服务器:

HostnamePort端口Description描述
mongodb0.example.net27017A running standalone MongoDB Server with data.一个正在运行的单机版MongoDB服务器。
mongodb1.example.net27017A new MongoDB Server to join the replica set.一个新的MongoDB服务器加入副本集。
mongodb2.example.net27017A new MongoDB Server to join the replica set.一个新的MongoDB服务器加入副本集。

Before You Begin开始之前

Cluster Type群集类型

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.分片通常托管在多个服务器上,并允许横向扩展。

Authorization授权

To use authorization with a replica set, you must also configure replica set members to use X.509 certificates or keyfiles to perform internal authentication.要对副本集使用授权,您还必须将副本集成员配置为使用X.509证书或键文件来执行内部身份验证。

For more information, see:有关更多信息,请参阅:

Procedure过程

1

Shut down the standalone instance.关闭单机版实例。

Use mongosh to connect to your existing mongod instance:使用mongosh连接到现有的mongod实例:

mongosh "mongodb://mongodb0.example.net:27017"

Switch to the admin database and run shutdown.切换到admin数据库并运行shutdown(关机)。

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

Configure Replica Set Members配置副本集成员

Update the configuration file on each server and to set the replSetName setting更新每台服务器上的配置文件并设置replSetName设置.

replication:
replSetName: "rs0"
3

Configure Member Authentication配置成员身份验证

Configure member authentication for each server in the replica set.为副本集中的每台服务器配置成员身份验证。

X.509 Authentication

Configure the replica set to use X.509 certificates for internal member authentication.将副本集配置为使用X.509证书进行内部成员身份验证。

Setting设置Option选项Description描述
net.tls.mode--tlsModeSets the TLS mode to use in authentication. To configure the server to require X.509 certificate authentication, set this option to requireTLS.设置在身份验证中使用的TLS模式。要将服务器配置为要求X.509证书身份验证,请将此选项设置为requireTLS
net.tls.certificateKeyFile--tlsCertificateKeyFileSets the path to the .pem file that contains the TLS certificate for client connections.设置包含客户端连接TLS证书的.pem文件的路径。
net.tls.CAFile--tlsCAFileSets the path to the file that contains the root certificate chain for the Certificate Authority (CA).设置包含证书颁发机构(CA)根证书链的文件的路径。
net.tls.clusterFile--tlsClusterFileSets the path to the .pem file that contains the TLS certificate for cluster member connections.设置包含群集成员连接的TLS证书的.pem文件的路径。
security.clusterAuthMode--clusterAuthModeSets the mode used to authenticate cluster members. To use X.509 authentication, set this option to x509.设置用于对群集成员进行身份验证的模式。要使用X.509身份验证,请将此选项设置为x509

For example:例如:

replication:
replSetName: "rs0"
security:
clusterAuthMode: x509
net:
tls:
mode: requireTLS
certificateKeyFile: /etc/mongodb/client.pem
CAFile: /etc/mongodb/ca.pem
clusterFile: /etc/mongodb/member.pem
Keyfile Authentication

Configure the replica set to use keyfiles for internal member authentication. To authenticate, each member must have a copy of the same keyfile.

SettingOption选项Description描述
security.keyFile--keyFileSets the path to the replica set keyfile.

For example:例如:

replication:
replSetName: "rs0"
security:
keyFile: /etc/mongodb/keyfile
No Authentication

Configures a replica set without authorization.

Warning

You should only use this configuration for internal replica sets that are not accessible through the network.您应该只将此配置用于无法通过网络访问的内部副本集。

SettingOption选项Description描述
net.bindIp--bind_ipSets the hostnames or IP addresses that MongoDB listens on for client connections. To block network access to the server, set this option to localhost.设置MongoDB监听客户端连接的主机名或IP地址。要阻止对服务器的网络访问,请将此选项设置为localhost

For example:例如:

replication:
replSetName: "rs0"
net:
bindIp: localhost
4

Start MongoDB启动MongoDB

Start mongod for each member.为每个成员启动mongod

5

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

To initialize the replica set, use mongosh to reconnect to your server instance.要初始化副本集,请使用mongosh重新连接到服务器实例。

mongosh "mongodb://mongodb0.example.net:27017"

Then, run the rs.initiate() method:然后,运行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()

6

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:新的副本集只有一个主节点。下一步是将新节点添加到副本集中。在添加其他节点之前,请查看有关集群的文档:

To add nodes, run the rs.add() method:要添加节点,请运行rs.add()方法:

rs.add("mongodb1.example.net:27017")
rs.add("mongodb2.example.net:27017")
7

Check replication status.检查复制状态。

To check that the replica set is correctly configured, run the rs.status() method and check that the new members are listed in the members field:要检查副本集是否配置正确,请运行rs.status()方法,并检查新成员是否列在members字段中:

rs.status()
8

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

After you convert the standalone server to a replica set, update the connection string used by your applications to the connection string for your replica set:将独立服务器转换为副本集后,将应用程序使用的连接字符串更新为副本集的连接字符串

  • mongodb0.example.net:27017
  • mongodb1.example.net:27017
  • mongodb2.example.net:27017
mongodb://mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017

Then, restart your applications.然后,重新启动应用程序。

Learn More了解更多