Database Manual / Sharding / Administration

Start a Sharded Cluster with a Config Shard使用配置分片启动分片集群

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步骤

1

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:对于本教程,配置服务器副本集成员与以下主机相关联:

Config Server Replica Set Member配置服务器副本集成员Hostname主机名
Member 0cfg1.example.net
Member 1cfg2.example.net
Member 2cfg3.example.net
  1. Start each member of the config server replica set.启动配置服务器副本集的每个成员。

    When starting each mongod, specify the mongod settings 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)>

    Start the mongod with the --config option set to the configuration file path.--config选项设置为配置文件路径,启动mongod

    mongod --config <path-to-config-file>
    Command Line

    If using the command line options, start the mongod with 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 mongod reference page.有关启动参数的更多信息,请参阅mongod参考页面。

  2. Connect mongosh to one of the config server members.mongosh连接到配置服务器成员之一。

    mongosh --host <hostname> --port <port>
  3. Initiate the replica set.启动副本集。

    From mongosh, run the rs.initiate() method.mongosh运行rs.initiate()方法。

    rs.initiate() can take an optional replica set configuration document. In the replica set configuration document, include:可以采用可选的副本集配置文档。在副本集配置文档中,包括:

    • The _id set to the replica set name specified in either the replication.replSetName or the --replSet option._id设置为replication.replSetName--replSet选项中指定的副本集名称。
    • The configsvr field set to true for the config server replica set.配置服务器副本集的configsvr字段设置为true
    • The members array with a document per each member of the replica set.members数组,每个副本集的成员都有一个文档。

    Important

    Run rs.initiate() on only one mongod instance 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.有关副本集配置文档的更多信息,请参阅自我管理副本集配置

2

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连接到分片集群。

3

Connect to the Sharded Cluster.连接到分片集群。

Connect mongosh to the mongos. Specify the host and port on which the mongos is running:mongoshmongos联系起来。指定运行mongoshostport

mongosh --host <hostname> --port <port>
4

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
} )
5

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

Learn More了解更多