Mongodb 部署一个分片集群

根据下面的任务顺序来部署一个分片集群:

警告

分片和“localhost”地址

如果你使用“localhost”或者是127.0.0.1 作为任一节点标识的主机名部分,例如addShard 方法的host 参数或者是运行时操作 --configdb的值,那么你必须为集群中的所有MongoDB的所有节点设置都使用“localhost”或者是127.0.0.1。如果你把本地地址和远程地址混合使用的话,MongoDB将会出现错误。

启动Config Server的数据库实例

Config Server进程是用来存放集群元数据的mongod实例。你可以通过 --configsvr 操作指定一个mongod 实例作为configure server。每个config server都保存了一份集群元数据的备份。

在生产环境中,你必须部署恰好三个config server实例,并且每个实例都要运行在不同的服务器上,来保证正常运行时间和数据安全。在测试环境中,你可以让三个实例在一台机器上运行。

重要

一个分片集群中的所有成员必须能够连接到这个分片集群中的其他所有成员,包括所有的分片和config servers。要确保包括防火墙在内的安全系统和网络允许这些连接。

  1. 为每个config server创建数据目录。在默认情况下,一个config server将它的数据文件存储再/data/configdb 目录下。你可以选择一个其他的位置。要创建一个数据目录,执行类似下面的命令:

    mkdir /data/configdb
    
  2. 启动三个config server 实例。启动每个实例使用满足下面语法的命令:

    mongod --configsvr --dbpath <path> --port <port>
    

    Config Servers 默认的端口号是27019。 你可以制定其它的端口号。下面的示例启动了一个使用默认端口号和默认数据路径的config server:

    mongod --configsvr --dbpath /data/configdb --port 27019
    

    有关更多的命令选项, 查看 mongod 或者 Configuration File Options.

    提示

    在你第一次初始化一个分片集群时,所有的config servers 都必须处于运行状态,并且要能被够访问到。

启动Mongos实例

Mongos 实例是轻量级的并且不需要数据目录。你可以在运行着集群其他组件的系统上运行一个mongos实例,例如一个应用服务器(注:运行这客户端程序的服务器)或者一台运行着mongod进程的服务器。默认情况下,mongos的端口号是 27017。

当你启动 mongos 实例时,要指定三个config servers的hostnames,要么通过配置文件指定,要么通过命令行参数指定。

贴士

为了避免停机时间,给每个config server设置一个逻辑上的DNS名称(与服务器的物理或者虚拟节点名称无关)。如果没有逻辑DNS名称,移动或者重命名一个config server需要关闭集群中所有 mongod 和 mongos 实例。

要启动一个 mongos 实例, 执行使用下面语法的命令:

mongos --configdb <config server hostnames>

例如:要启动一个连接在以下节点上运行在默认端口的config server实例的 mongos 实例:

  • cfg0.example.net
  • cfg1.example.net
  • cfg2.example.net

你需要发出先面的命令:

mongos --configdb cfg0.example.net:27019,cfg1.example.net:27019,cfg2.example.net:27019

集群中每一个 mongos 都必须使用相同的 configDB 字符串, 通过相同的顺序列出的相同的主机名称(with identical host names listed in identical order)。

如果你启动一个 mongos 实例使用的字符串不能正确匹配集群中其他的 mongos 实例使用的字符串,这个 mongos 将会报 Config Database String Error 错误,并且拒绝启动。

添加分片到集群中

一个分片可以是一个单独的mongod实例也可以是一个复制集。在生产环境中,所有的分片都应该是一个复制集。

  1. 通过 mongo shell, 连接上 mongos 实例。发出使用下面语法的命令:

    mongo --host <hostname of machine running mongos> --port <port mongos listens on>
    

    例如:如果一个 mongos 可以在 mongos0.example.net 的 27017端口上被访问到, 发出下面的命令:

    mongo --host mongos0.example.net --port 27017
    
  2. 添加每个分片到集群都要使用 sh.addShard() 方法,向下面的例子展示的那样。如果这个分片是一个复制集,那么指定这个数据集的名称和集合中的一个成员。在生产环境中,所有的分片都必须是复制集。

    可选

    你可以是哟你数据库命令 addShard 来指定集群的名称和最大数目。如果你不指定这些,MongoDB会自动指定。要使用数据库命令,查看 addShard.

    下面是通过 sh.addShard() 添加到集群的示例:

    • 要给一个名字是 rs1 并且运行在端口为27017 的mongodb0.example.net上的复制集添加一个分片,发出下面的命令:

      sh.addShard( "rs1/mongodb0.example.net:27017" )
      

      版本2.0.3中的变更

      对于早于2.0.3版本的MongoDB,你必须指定复制集中所有的成员。例如:

      sh.addShard( "rs1/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017" )
      
    • 要给运行在 mongodb0.example.net 端口号为27017的单独的mongod实例添加分片,发出如下命令:

      sh.addShard( "mongodb0.example.net:27017" )
      

    提示

    让 chunks 迁移到新的分片上可能会花费些时间。

对数据库启用分片

你必须对数据库的集合启用分片才能分片一个集合。(Before you can shard a collection, you must enable sharding for the collection’s database)。启用数据库的分片不会对数据进行重新分配,而是让数据库中的集合能够被分片。

一旦你为数据库启用分片,MongoDB会再分片之前为存放所有数据的数据库指定一个 primary shard 。

  1. 通过 mongo shell连接到 mongos 实例。发出使用下面语法的命令:

    mongo --host <hostname of machine running mongos> --port <port mongos listens on>
    
  2. 执行 sh.enableSharding() 方法,指定已启用分片的数据库的名称。使用下面的语法:

    sh.enableSharding("<database>")
    

可选项,你可以使用 enableSharding 命令为数据库启用分片:

db.runCommand( { enableSharding: <database> } )

对集合启用分片

在每一个集合的基础上启用分片。

  1. 指定你要使用的 shard key。你对shard key的选择会影响到分片的效率。

  2. 如果集合已经包含数据,你必须再shard key上使用ensureIndex()方法来创建索引。如果集合是空的,MongoDB会在sh.shardCollection() 的步骤中创建索引。

  3. 通过再mongo shell 中执行sh.shardCollection() 方法来为集合启用分片。使用如下语法:

    sh.shardCollection("<database>.<collection>", shard-key-pattern)
    

    Replace the <database>.<collection> string with the full namespace of your database, which consists of the name of your database, a dot (e.g. .), and the full name of the collection. Theshard-key-pattern represents your shard key, which you specify in the same form as you would an index key pattern.

    EXAMPLE

    The following sequence of commands shards four collections:

    sh.shardCollection("records.people", { "zipcode": 1, "name": 1 } )
    sh.shardCollection("people.addresses", { "state": 1, "_id": 1 } )
    sh.shardCollection("assets.chairs", { "type": 1, "_id": 1 } )
    sh.shardCollection("events.alerts", { "_id": "hashed" } )
    

    以下操作按顺序分片:

    1. The people collection in the records database using the shard key { "zipcode": 1,"name": 1 }.

      This shard key distributes documents by the value of the zipcode field. If a number of documents have the same value for this field, then that chunk will be splittable by the values of the name field.

    2. The addresses collection in the people database using the shard key { "state": 1,"_id": 1 }.

      This shard key distributes documents by the value of the state field. If a number of documents have the same value for this field, then that chunk will be splittable by the values of the _id field.

    3. The chairs collection in the assets database using the shard key { "type": 1,"_id": 1 }.

      This shard key distributes documents by the value of the type field. If a number of documents have the same value for this field, then that chunk will be splittable by the values of the _idfield.

    4. The alerts collection in the events database using the shard key { "_id": "hashed"}.

      New in version 2.4.

      这个shard key通过_id字段的hash值来对文档进行分配。MongoDB computes the hash of the _id field for the hashed index, which should provide an even distribution of documents across a cluster.

Mongodb 部署一个分片集群,布布扣,bubuko.com

Mongodb 部署一个分片集群

上一篇:PHP在线支付


下一篇:用php描述顺序查找