Starting in MongoDB 8.0, you can configure a config server to store your application data in addition to the usual sharded cluster metadata. 从MongoDB 8.0开始,您可以配置配置服务器来存储应用程序数据以及通常的分片集群元数据。A 同时提供配置服务器和分片服务器功能的mongod node that provides both config server and shard server functionality is called a config shard. mongod节点称为配置分片。A 没有分片服务器功能的mongod node that runs as a standalone --configsvr without shard server functionality is called a dedicated config server.mongod节点作为独立的--configsvr运行,称为专用配置服务器。
About this Task关于此任务
You can consider using a config shard if your cluster has three or fewer shards.如果集群有三个或更少的分片,你可以考虑使用配置分片。
For details, see Config Shard Use Cases.有关详细信息,请参阅配置分片用例。
Compatibility兼容性
You can perform this task on deployments hosted in the following environments:您可以在以下环境中托管的部署上执行此任务:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This task is not available on the MongoDB Atlas Free or Flex Tiers.此任务在MongoDB Atlas Free或Flex层上不可用。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Steps步骤
Create the Config Server Replica Set.创建配置服务器副本集。
For a production deployment, deploy a config server replica set with at least three members.对于生产部署,部署一个至少有三个成员的配置服务器副本集。
Note
The config server replica set must not use the same name as any of the shard replica sets.配置服务器副本集不得使用与任何分片副本集相同的名称。
For this tutorial, the config server replica set members are associated with the following hosts:对于本教程,配置服务器副本集成员与以下主机相关联:
| Member 0 | cfg1.example.net |
| Member 1 | cfg2.example.net |
| Member 2 | cfg3.example.net |
Start each member of the config server replica set.启动配置服务器副本集的每个成员。When starting each启动每个mongod, specify themongodsettings using either a configuration file or the command line.mongod时,使用配置文件或命令行指定mongod设置。Configuration File
If using a configuration file, set:如果使用配置文件,请设置:sharding:
clusterRole: configsvr
replication:
replSetName: <replica set name>
net:
bindIp: localhost,<hostname(s)|ip address(es)>sharding.clusterRoleto为configsvr.configsvr。replication.replSetNameto the desired name of the config server replica set.到配置服务器副本集的所需名称。net.bindIpoption to one of:net.bindIp选项为以下选项之一:The hostname/ip address.主机名/ip地址。A comma-delimited list of hostnames or ip addresses that remote clients can use to connect to the instance (including the other members of the config server replica set as well as other members of the sharded cluster).一个逗号分隔的主机名或ip地址列表,远程客户端可以使用它连接到实例(包括配置服务器副本集的其他成员以及分片集群的其他成员)。
Warning
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist for Self-Managed Deployments.在将实例绑定到可公开访问的IP地址之前,必须保护集群免受未经授权的访问。有关安全建议的完整列表,请参阅自我管理部署的安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证并加强网络基础设施。Additional settings as appropriate to your deployment, such as根据部署情况进行其他设置,如storage.dbPathandnet.port.storage.dbPath和net.port。For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
Start the将mongodwith the--configoption set to the configuration file path.--config选项设置为配置文件路径,启动mongod。mongod --config <path-to-config-file>Command Line
If using the command line options, start the如果使用命令行选项,请使用mongodwith the--configsvr,--replSet,--bind_ip, and other options as appropriate to your deployment. For example:--configsvr、--replSet、--bind_ip和其他适合您部署的选项启动mongod。例如:mongod --configsvr --replSet <replica set name> --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)>Warning
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist for Self-Managed Deployments.在将实例绑定到可公开访问的IP地址之前,必须保护集群免受未经授权的访问。有关安全建议的完整列表,请参阅自我管理部署的安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证并加强网络基础设施。For more information on startup parameters, see the有关启动参数的更多信息,请参阅mongodreference page.mongod参考页面。Connect mongosh to one of the config server members.将mongosh连接到配置服务器成员之一。mongosh --host <hostname> --port <port>Initiate the replica set.启动副本集。From从mongosh, run thers.initiate()method.mongosh运行rs.initiate()方法。rs.initiate()can take an optional replica set configuration document. In the replica set configuration document, include:可以采用可选的副本集配置文档。在副本集配置文档中,包括:The_idset to the replica set name specified in either thereplication.replSetNameor the--replSetoption._id设置为replication.replSetName或--replSet选项中指定的副本集名称。The配置服务器副本集的configsvrfield set totruefor the config server replica set.configsvr字段设置为true。Themembersarray with a document per each member of the replica set.members数组,每个副本集的成员都有一个文档。
Important
Run仅对副本集的一个rs.initiate()on only onemongodinstance for the replica set.mongod实例运行rs.initiate()。rs.initiate(
{
_id: "myReplSet",
configsvr: true,
members: [
{ _id : 0, host : "cfg1.example.net:27019" },
{ _id : 1, host : "cfg2.example.net:27019" },
{ _id : 2, host : "cfg3.example.net:27019" }
]
}
)See Self-Managed Replica Set Configuration for more information on replica set configuration documents.有关副本集配置文档的更多信息,请参阅自我管理副本集配置。
Start a mongos for the Sharded Cluster.为分片集群启动一个mongos。
Start a 使用配置文件或命令行参数启动mongos using either a configuration file or a command line parameter to specify the config servers.mongos以指定配置服务器。
Configuration File
If using a configuration file, set the 如果使用配置文件,请将sharding.configDB to the config server replica set name and at least one member of the replica set in <replSetName>/<host:port> format.sharding.configDB设置为配置服务器副本集名称,并以<replSetName>/<host:port>格式设置副本集的至少一个成员。
Warning
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护集群免受未经授权的访问。For a complete list of security recommendations, see Security Checklist for Self-Managed Deployments. 有关安全建议的完整列表,请参阅自我管理部署的安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证并加强网络基础设施。
sharding:
configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019
net:
bindIp: localhost,<hostname(s)|ip address(es)>
Start the 启动mongos specifying the --config option and the path to the configuration file.mongos,指定--config选项和配置文件的路径。
mongos --config <path-to-config>
For more information on the configuration file, see configuration options.有关配置文件的更多信息,请参阅配置选项。
Command Line
If using command line parameters start the 如果使用命令行参数,请启动mongos and specify the --configdb, --bind_ip, and other options as appropriate to your deployment. For example:mongos,并根据部署情况指定--configdb、--bind_ip和其他选项。例如:
Warning
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护集群免受未经授权的访问。For a complete list of security recommendations, see Security Checklist for Self-Managed Deployments. 有关安全建议的完整列表,请参阅自我管理部署的安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证并加强网络基础设施。
mongos --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019,cfg3.example.net:27019 --bind_ip localhost,<hostname(s)|ip address(es)>
Include any other options as appropriate for your deployment.根据部署情况,包括任何其他选项。
At this point, your sharded cluster consists of the 此时,分片集群由mongos and the config servers. mongos和配置服务器组成。You can now connect to the sharded cluster using 现在,您可以使用mongosh.mongosh连接到分片集群。
Connect to the Sharded Cluster.连接到分片集群。
Connect 把mongosh to the mongos. Specify the host and port on which the mongos is running:mongosh和mongos联系起来。指定运行mongos的host和port:
mongosh --host <hostname> --port <port>Transition from a Dedicated Config Server to a Config Shard.从专用配置服务器到配置分片的转换。
This example configures a dedicated config server to run as a config shard:此示例配置了一个专用配置服务器作为配置分片运行:
db.adminCommand( {
transitionFromDedicatedConfigServer: 1
} )Confirm Cluster uses a Config Shard确认群集使用配置分片
To confirm that the sharded cluster now uses the config shard, run the 要确认分片集群现在使用配置分片,请运行serverStatus command to check that the configServerInShardCache status returns true:serverStatus命令检查configServerInShardCache状态是否返回true:
db.adminCommand( {
serverStatus: 1,
} ).shardingStatistics.configServerInShardCache
true