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:本教程使用以下服务器:
| Hostname | ||
|---|---|---|
mongodb0.example.net | 27017 | |
mongodb1.example.net | 27017 | |
mongodb2.example.net | 27017 |
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过程
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"
}
)Configure Replica Set Members配置副本集成员
Update the configuration file on each server and to set the 更新每台服务器上的配置文件并设置replSetName settingreplSetName设置.
replication:
replSetName: "rs0"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证书进行内部成员身份验证。
net.tls.mode | --tlsMode | requireTLS.requireTLS。 |
net.tls.certificateKeyFile | --tlsCertificateKeyFile | .pem file that contains the TLS certificate for client connections..pem文件的路径。 |
net.tls.CAFile | --tlsCAFile | |
net.tls.clusterFile | --tlsClusterFile | .pem file that contains the TLS certificate for cluster member connections..pem文件的路径。 |
security.clusterAuthMode | --clusterAuthMode | x509.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.pemKeyfile Authentication
Configure the replica set to use keyfiles for internal member authentication. To authenticate, each member must have a copy of the same keyfile.
| Setting | ||
|---|---|---|
security.keyFile | --keyFile | Sets the path to the replica set keyfile. |
For example:例如:
replication:
replSetName: "rs0"
security:
keyFile: /etc/mongodb/keyfileNo 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.您应该只将此配置用于无法通过网络访问的内部副本集。
| Setting | ||
|---|---|---|
net.bindIp | --bind_ip | localhost.localhost。 |
For example:例如:
replication:
replSetName: "rs0"
net:
bindIp: localhostStart MongoDB启动MongoDB
Start 为每个成员启动mongod for each member.mongod。
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()。
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")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()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:27017mongodb1.example.net:27017mongodb2.example.net:27017
mongodb://mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017
Then, restart your applications.然后,重新启动应用程序。