部署分片集群

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

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

 
 
配置服务器副本集成员
主机名
会员 0
cfg1.example.net
成员 1
cfg2.example.net
成员 2
cfg3.example.net
1启动配置服务器副本集的每个成员
启动每个 mongod时, mongod通过配置文件或命令行指定设置。
sharding:
  clusterRole: configsvr
replication:
  replSetName: <replica set name>
net:
  bindIp: localhost,<hostname(s)|ip address(es)>  #包括配置服务器副本集的其他成员以及分片集群的其他成员

mongod通过将--config选项设置为配置文件路径来启动。

mongod --config <path-to-config-file>
2连接到其中一个配置服务器。
连接mongosh到其中一个配置服务器成员。
mongosh --host <hostname> --port <port>
3启动副本集。
mongosh运行该rs.initiate()方法。
rs.initiate()可以采用可选的副本集配置文件。在 副本集配置文件中,包括:
  • 设置为或选项_id中指定的副本集名称replication.replSetName--replSet

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

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

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" }
    ]
  }
)
仅在副本集的一个且仅在一个mongod实例上运行rs.initiate()。

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

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

启动每个 mongod, mongod通过配置文件或命令行指定设置。

sharding:
    clusterRole: shardsvr
replication:
    replSetName: <replSetName>
net:
    bindIp: localhost,<ip address>
  • 根据您的部署添加其他设置,例如 storage.dbPathnet.port。有关配置文件的更多信息,请参阅配置选项

mongod --config <path-to-config-file>
2连接到分片副本集的一个成员。
mongosh --host <hostname> --port <port>
3启动副本集。
mongosh运行该rs.initiate()方法。

rs.initiate()可以采用可选的副本集配置文档。在复制副本集配置文档中,包括:
_id字段设置为replication.replSetName或--replSet选项中指定的副本集名称。
成员数组,其中副本集的每个成员都有一个文档。

以下示例启动一个由三名成员组成的副本集。

仅在副本集的一个且仅在一个mongod实例上运行rs.initiate()。
rs.initiate(
  {
    _id : "myReplSet",
    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使用配置文件或命令行参数指定配置服务器。
如果使用配置文件,请将sharding.configDB设置为配置服务器副本集名称和至少一个副本集成员,格式为<replSetName>/<host:port>。
sharding:
  configDB: <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019
net:
  bindIp: localhost,<hostname(s)|ip address(es)>

mongos开始指定--config 选项和配置文件的路径。

mongos --config <path-to-config>
mongosh --host <hostname> --port <port>

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

sh.addShard( "<replSetName>/s1-mongo1.example.net:27018,s1-mongo2.example.net:27018,s1-mongo3.example.net:27018")
碎片和索引
如果集合已经包含数据,则在对集合进行分片之前,必须创建一个支持分片键的索引。如果集合为空,MongoDB将创建索引作为sh.shardCollection()的一部分。

MongoDB 提供了两种对集合进行分片的策略:

  • 哈希分片使用 单个字段的 哈希索引作为分片键来跨分片集群对数据进行分区。

sh.shardCollection("<database>.<collection>", { <shard key field> : "hashed" } )

基于范围的分片可以使用多个字段作为分片键,并根据分片键值将数据划分为连续的范围。

sh.shardCollection("<database>.<collection>", { <shard key field> : 1, ... } )

 

总结:

  1. 设置配置文件,启动配置服务器副本集(3个)mongod  27019
  2. 连接其中一个,执行初始化配置服务器
  3. 设置配置i文件,启动分片副本集(3个)mongod    27018
  4. 连接其中一个,执行初始化分片复制集服务器
  5. 设置mongos配置文件(文件里需要配置服务器信息),启动路由服务器mongos   27017
  6. 连接到路由服务器,添加分片副本集
  7. 对集合进行分片

 

 

 

 

 
posted @ 2024-07-08 06:29  wongchaofan  阅读(2)  评论(0编辑  收藏  举报