zoukankan      html  css  js  c++  java
  • mongo-分片

    分片

    分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程。

    路由器mongos:分片之前要运行一个路由进程,该进程名为mongos

    这个路由器知道所有数据的存放位置,所以应用可以连接它来正常发送请求。

    对应用来说,它仅知道连接了一个普通的mongod

    路由器知道数据和片的对应关系,能够转发请求道正确的片上。

    如果请求有了回应,路由器将其收集起来回送给应用。

    片键(shard key):设置分片时,需要从集合里面选一个键,用该键的值作为数据拆分的依据。

    1、创建三个目录,分别存放两个mongod服务的数据文件和config服务的数据文件

    2、开启config服务器 。mongos要把mongod之间的配置放到config服务器里面,所以首先开启它,这里就使用2222端口。 命令为:

    mongod --dbpath D:mongodbshardingconfig_node --port 2222

    3、开启mongos服务器 。这里要注意的是我们开启的是mongos,端口3333,同时指定下config服务器。命令为:

    mongos --port 3333 --configdb=127.0.0.1:2222

    4、启动mongod服务器 。对分片来说,也就是要添加片了,这里开启两个mongod服务,端口分别为:44445555。命令为:

    mongod --dbpath D:mongodbshardingmongod_node1 --port 4444

    mongod --dbpath D:mongodbshardingmongod_node2 --port 5555 

    5、服务配置 。client直接跟mongos打交道,也就说明我们要连接mongos服务器,然后将44445555mongod交给mongos,添加分片也就是addshard()

    C:Usersking>mongo 127.0.0.1:3333/admin //admin下添加

    MongoDB shell version: 2.4.3

    connecting to: 127.0.0.1:3333/adminmongos> db.runCommand({"addshard":"127.0.0.1:4444",allowLocal:true})

    { "ok" : 0, "errmsg" : "host already used" }

    mongos> db.runCommand({"addshard":"127.0.0.1:5555",allowLocal:true})

    { "shardAdded" : "shard0001", "ok" : 1 }

    6、开启数据库分片功能,命令很简单 enablesharding(),这里就开启test数据库。 

    mongos> db.runCommand({"enablesharding":"test"})

    { "ok" : 1 }

    7、指定集合中分片的片键,这里就指定为person.name键。 

    mongos> db.runCommand({"shardcollection":"test.person","key":{"name":1}})

    { "collectionsharded" : "test.person", "ok" : 1 }

    mongos>

    8、通过mongos插入10w记录,然后通过printShardingStatus命令查看mongodb的数据分片情况。 

    mongos> use test

    switched to db test

    mongos> for(var i=0;i<10000;i++){

    ... db.person.insert({"name":"jack"+i,"age":i})

    }

    mongos> db.printShardingStatus()

    --- Sharding Status ---

      sharding version: {

            "_id" : 1,

            "version" : 3,

            "minCompatibleVersion" : 3,

            "currentVersion" : 4,

            "clusterId" : ObjectId("51b09d556a9ca490d75d1c3a")

    }

      shards:

            {  "_id" : "shard0000",  "host" : "127.0.0.1:4444" }

            {  "_id" : "shard0001",  "host" : "127.0.0.1:5555" }

      databases:

            {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

            {  "_id" : "test",  "partitioned" : true,  "primary" : "shard0001" }

                    test.person

                            shard key: { "name" : 1 }

                            chunks:

                                    shard0000       1  //分片比例

                                    shard0001       1

                            { "name" : { "$minKey" : 1 } } -->> { "name" : "jack0" }

     on : shard0000 { "t" : 2, "i" : 0 }

                            { "name" : "jack0" } -->> { "name" : { "$maxKey" : 1 } }

     on : shard0001 { "t" : 2, "i" : 1 }

    mongos>

  • 相关阅读:
    14.6 将运算分组为事务
    Android 取得 ListView中每个Item项目的值
    【编程题目】n 个骰子的点数
    【编程题目】扑克牌的顺子
    【编程题目】颠倒栈☆
    【编程题目】输出 1 到最大的 N 位数
    【编程题目】寻找丑数
    【编程题目】在字符串中删除特定的字符
    【编程题目】复杂链表的复制☆
    【编程题目】找出数组中两个只出现一次的数字 ★★(自己没做出来)
  • 原文地址:https://www.cnblogs.com/kuyuyingzi/p/4266374.html
Copyright © 2011-2022 走看看