部署分片集群

在本页面

Overview

本教程涉及创建一个新的分片集群,该集群由mongos,配置服务器副本集和两个分片副本集组成。

Considerations

Connectivity

分片群集的每个成员都必须能够连接到群集中的“所有”其他成员。这包括所有分片和配置服务器。确保网络和安全系统(包括所有接口和防火墙)允许这些连接。

主机名和配置

Tip

为避免停机,请为每个配置服务器指定一个逻辑 DNS 名称(与服务器的物理或虚拟主机名无关)。如果没有逻辑 DNS 名称,移动或重命名配置服务器需要关闭分片群集中的每个mongodmongos实例。

Localhost Deployments

如果您使用localhost或其 IP 地址作为任何主机标识符的主机名部分,则必须*使用该标识符作为集群中任何其他 MongoDB 组件的主机设置。

例如,sh.addShard()方法采用host参数作为目标分片的主机名。如果将host设置为localhost,则必须使用localhost作为群集中所有其他分片的主机。

Security

本教程包括配置Internal Authentication基于角色的访问控制的必需步骤。

在生产环境中,分片群集至少应采用x.509安全性进行内部身份验证和 Client 端访问。

Procedure

创建配置服务器副本集

以下步骤将部署配置服务器副本集。

对于生产部署,请部署至少具有三个成员的配置服务器副本集。出于测试目的,您可以创建单成员副本集。

Note

配置服务器副本集不得使用与任何分片副本集相同的名称。

对于本教程,配置服务器副本集成员与以下主机相关联:

配置服务器副本集成员Hostname
Member 0cfg1.example.net
Member 1cfg2.example.net
Member 2cfg3.example.net

启动配置服务器副本集的每个成员。

启动* each * mongod时,通过配置文件或命令行指定mongod设置。

Configuration File
Command Line

If using a configuration file, set:

sharding:
  clusterRole: configsvr
replication:
  replSetName: <replica set name>
net:
  bindIp: localhost,<hostname(s)|ip address(es)>
  • sharding.clusterRole to configsvr ,

  • replication.replSetName to the desired name of the config server replica set,

  • net.bindIp option to the hostname/ip address or comma-delimited list of hostnames or ip addresses that remote clients (including the other members of the config server replica set as well as other members of the sharded cluster) can use to connect to the instance.

Warning

Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.

Start the mongod with the --config option set to the configuration file path.

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

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:

Warning

Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.

mongod --configsvr --replSet <replica set name> --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)>

For more information on startup parameters, see the mongod reference page.

连接到其中一台配置服务器。

mongo Shell 连接到配置服务器成员之一。

mongo --host <hostname> --port <port>

启动副本集。

mongo shell 中,运行rs.initiate()方法。

rs.initiate()可以选填副本集配置文档。在副本集配置文档中,包括:

  • _id设置为在replication.replSetName--replSet选项中指定的副本集名称。

  • 配置服务器副本集的configsvr字段设置为true

  • members数组,每个副本集中每个成员都有一个文档。

Important

在副本集的仅一个且仅一个 mongod实例上运行rs.initiate()

rs.initiate(
  {
    _id: "<replSetName>",
    configsvr: true,
    members: [
      { _id : 0, host : "cfg1.example.net:27019" },
      { _id : 1, host : "cfg2.example.net:27019" },
      { _id : 2, host : "cfg3.example.net:27019" }
    ]
  }
)

有关副本集配置文档的更多信息,请参见副本集配置

启动并启动配置服务器副本集(CSRS)后,continue 创建分片副本集。

创建分片副本集

对于生产部署,请使用至少具有三个成员的副本集。出于测试目的,您可以创建单成员副本集。

Note

分片副本集不得使用与配置服务器副本集相同的名称。

对于每个分片,使用以下步骤创建分片副本集:

启动分片副本集的每个成员。

启动* each * mongod时,通过配置文件或命令行指定mongod设置。

Configuration File
Command Line

If using a configuration file, set:

sharding:
    clusterRole: shardsvr
replication:
    replSetName: <replSetName>
net:
    bindIp: localhost,<ip address>
  • replication.replSetName to the desired name of the replica set,

  • sharding.clusterRole option to shardsvr ,

  • net.bindIp option to the ip or a comma-delimited list of ips that remote clients (including the other members of the config server replica set as well as other members of the sharded cluster) can use to connect to the instance.

Warning

Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.

Start the mongod with the --config option set to the configuration file path.

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

If using the command line option, start the mongod with the --replSet , and --shardsvr , --bind_ip options, and other options as appropriate to your deployment. For example:

mongod --shardsvr --replSet <replSetname>  --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)>

For more information on startup parameters, see the mongod reference page.

连接到分片副本集的一个成员。

mongo shell 连接到副本集成员之一。

mongo --host <hostname> --port <port>

启动副本集。

mongo shell 中,运行rs.initiate()方法。

rs.initiate()可以选填副本集配置文档。在副本集配置文档中,包括:

下面的示例启动一个三成员副本集。

Important

在副本集的仅一个且仅一个 mongod实例上运行rs.initiate()

rs.initiate(
  {
    _id : <replicaSetName>,
    members: [
      { _id : 0, host : "s1-mongo1.example.net:27018" },
      { _id : 1, host : "s1-mongo2.example.net:27018" },
      { _id : 2, host : "s1-mongo3.example.net:27018" }
    ]
  }
)

为分片群集启动 mongos

使用配置文件或命令行参数启动mongos以指定配置服务器。

Configuration File
Command Line

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.

Warning

Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. 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 <path-to-config>

For more information on the configuration file, see configuration options.

If using command line parameters start the mongos and specify the --configdb , --bind_ip , and other options as appropriate to your deployment. For example:

Warning

Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. 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.

此时,分片群集由mongos和配置服务器组成。现在,您可以使用mongo shell 连接到分片群集。

连接到分片群集

mongoShell 连接到mongos。指定运行mongoshostport

mongo --host <hostname> --port <port>

mongoShell 连接到mongos后,continue 执行下一个过程,将分片添加到群集。

将碎片添加到群集

在连接到mongosmongo shell 中,使用sh.addShard()方法将每个分片添加到群集。

以下操作将单个分片副本集添加到集群:

sh.addShard( "<replSetName>/s1-mongo1.example.net:27018,s1-mongo2.example.net:27018,s1-mongo3.example.net:27018")

重复这些步骤,直到群集包含所有所需的碎片。

启用数据库分片

在分片集合之前,必须为集合的数据库启用分片。对数据库启用分片并不会重新分发数据,但可以在该数据库中分片集合。

在连接到mongosmongo shell 中,使用sh.enableSharding()方法在目标数据库上启用分片。在数据库上启用分片可以在数据库中分片集合。

sh.enableSharding("<database>")

为数据库启用分片后,MongoDB 将为该数据库分配primary shard,其中 MongoDB 将所有数据存储在该数据库中。

分片集合

Important

在分片集合之前,必须首先对集合所在的数据库进行enable sharding

要分片集合,请从mongoShell 连接到mongos并使用sh.shardCollection()方法。

分片和索引

如果集合已经包含数据,则在分片集合之前,您必须创建一个索引支持shard key。如果集合为空,则 MongoDB 将创建索引作为sh.shardCollection()的一部分。

MongoDB 提供了两种分片收集策略:

sh.shardCollection("<database>.<collection>", { <shard key field> : "hashed" } )
  • Range-based sharding可以将多个字段用作分片键,并将数据划分为由分片键值确定的连续范围。
sh.shardCollection("<database>.<collection>", { <shard key field> : 1, ... } )

分片键注意事项

您对分片密钥的选择会影响分片的效率,以及您利用某些分片功能(例如zones)的能力。要了解如何选择有效的分片键,请参阅选择分片键

从 4.0 版开始,mongo shell 提供了convertShardKeyToHashed()方法。此方法使用与哈希索引相同的哈希函数,可用于查看键的哈希值。

See also