MongoDB(六)分片

在单个MongoDB副本集的情况下,客户端是直接连接mongod来操作MongoDB,
如果在MongoDB分片下,则客户端不是直接连接mongod,而是
客户端连接mongos,而mongos再路由到对应的MongoDB副本集上,而路由的关系是保存是配置服务器上.关系以下:

客户端
|
mongos -- 配置服务器(保存路由关系等分片相关信息)
|
副本集

1.建立配置服务器

配置服务器相当于集群的大脑,保存着集群和分片的相关路由数据,如各分片包含哪个数据的信息。
配置服务器并不需要太多的空间,它保存的只是数据的分布表。
由于它包含数据是非常重要的,必须启用日志功能。

启动脚本:
MONGODB_HOME="`pwd`"

${MONGODB_HOME}/bin/mongod \
--configsvr \
--dbpath ${MONGODB_HOME}/data/db \
--port 40000 \
--fork \
--pidfilepath ${MONGODB_HOME}/mongodb.pid \
--logpath ${MONGODB_HOME}/logs/mongod.log --logappend --logRotate rename
其中
--configsvr选项指定mongod为新的配置服务器,使用该选项就比较直白地说明了配置服务器的用途。

按照类似以上配置再建立端口40001,40002的配置服务器,形成40000,40001,40002三台配置服务器

2.建立mongos服务器

mongos需要知道配置服务器的地址,所以必须使用--configdb选项启动mongos。
还需要注意的是,mongos服务器材并不需要指定数据目录,因为它本身并不保存数据,它会在启动时从配置服务器加载集群数据。

启动脚本:
MONGODB_HOME="`pwd`"

${MONGODB_HOME}/bin/mongos \
--configdb 192.168.20.111:40000,192.168.20.111:40001,192.168.20.111:40002 \
--port 50000 \
--fork \
--pidfilepath ${MONGODB_HOME}/mongodb.pid \
--logpath ${MONGODB_HOME}/logs/mongod.log --logappend --logRotate rename

其中,

--configdb 192.168.20.111:40000,192.168.20.111:40001,192.168.20.111:40002指定了mongos保存相关分片集群数据到配置服务器,配置服务器的配置地址。

另外,再建立端口50001,50002,成立三个接收客户端的mongos服务器

3.副本集转换为分片

有两种情况,
一种是已经存在副本集,即在建立分片群集之差,副本集已经处于启动的状态;
另一种是新建立的副本集。

3.1已存在的副本集转换为分片

如现在192.168.20.111:20000, 192.168.20.111:20001, 192.168.20.111:20002上有一个shard2的副本集
使用mongo客户端连接上mongos运行命令
mongos>sh.addShard("shard2/192.168.20.111:20000,192.168.20.111:20001,192.168.20.111:20002")
可以在参数中指定副本集的所有成员,也可以不指定全部成员,因为mongos能够自动检测到没有包含在副本集成员表中的成员,
可使用sh.status(),可发现MongoDB已经找到其他的副本成员。

如有多个已经存在的副本集还没转换为分片,则只要它们跟现已有的分片副本集存在同名的数据库,就可将它们作为新分片全部
添加到集群中;
但如果它们有一个相同的数据库存在,则mongos会拒绝添加这个副本集到集群中。

3.2新建立的副本集转换为分片

先启用副本集脚本:
MONGODB_HOME="`pwd`"

${MONGODB_HOME}/bin/mongod \
--shardsvr \
--replSet shard3 \
--dbpath ${MONGODB_HOME}/data/db \
--port 30000 \
--fork \
--pidfilepath ${MONGODB_HOME}/mongodb.pid \
--logpath ${MONGODB_HOME}/logs/mongod.log --logappend --logRotate rename \
--oplogSize 10

假设副本集shard3有3个成员192.168.20.111:30000, 192.168.20.111:30001, 192.168.20.111:30002
则连接上mongos运行命令sh.addShard()将副本集转换成分片

#查看分片服务器的配置
连接上mongos运行命令db.runCommand( { listshards : 1 } )查看分片的详细列表
mongos> use admin;
switched to db admin
mongos> db.runCommand({listshards:1});
{
    "shards" : [
        {
            "_id" : "shard1",
            "host" : "shard1/192.168.20.109:10000,192.168.20.109:10001"
        },
        {
            "_id" : "shard2",
            "host" : "shard2/192.168.20.109:20000,192.168.20.109:20001"
        },
        {
            "_id" : "shard3",
            "host" : "shard3/192.168.20.109:30000,192.168.20.109:30001"
        }
    ],
    "ok" : 1
}

4.数据分片

默认情况下,MongoDB不会自动对数据进行拆分,如果需要对某个数据库、集合进行拆分,则需要通过命令通知到mongos
#拆分数据库
mongos> db.enableSharding("test");
2016-12-03T06:19:35.901-0800 E QUERY    [thread1] TypeError: db.enableSharding is not a function :
@(shell):1:1
没以上方法,则使用以下命令
mongos> db.runCommand({enablesharding:"test"});
{
    "ok" : 0,
    "errmsg" : "enableSharding may only be run against the admin database.",
    "code" : 13
}
则表明需要切换到admin才可以执行enableSharding命令
mongos> use admin;
switched to db admin
mongos> db.runCommand({enablesharding:"test"});
{ "ok" : 1 }

#指定片键来分片
>db.shardCollection("test.foo", {"name":1});
2016-12-03T06:24:44.049-0800 E QUERY    [thread1] TypeError: db.shardCollection is not a function :
@(shell):1:1
则使用以下命令
>db.runCommand({shardCollection:"test.foo", key:{"name":1}});
{ "collectionsharded" : "test.foo", "ok" : 1 }

>use test;
switched to db test
mongos> db.foo.insert({"name":"abc"});
WriteResult({ "nInserted" : 1 })

在这里,我们说下分片的策略。

4.1分片策略

分片策略有两种:
升序分片(Ranged Sharding)
根据片键的值将集合分成多个块,每个块包含一个连续的范围,这样片键值相近的文档就很可能处于同一块中,
这种情况下,片键创建的是升序索引

>db.createIndex({ "userId":1 })
升序分片

MongoDB(六)分片_第1张图片

如果应用中比较倾向于范围查询的情况下,可以考虑升序分片这种策略,
如将用户id作为升序分片的片键:

>sh.shardCollection( "database.collection", { "userId":1 } );

哈希分片(Hashed Sharding)
将片键进行运算得到哈希值,映射到对应集合块中,
这种情况下,片键创建的是哈希索引
>db.createIndex({ "userId" : "hashed" })
哈希分片

MongoDB(六)分片_第2张图片

这种分片可以查询速度很快,
使用哈希值可以比较平均地将查询平衡到不同的分片中,达到分片的真正作用,
不过,有个缺点就是,在哈希分片情况下,如果需要范围查询,则需要查询各个分片,然后再聚合结果,这对分片的使用不太理想。
哈希分片:

>sh.shardCollection( "database.collection", { "userId" : "hashed" } )

#查看数据在分片上情况

mongos> db.foo.stats();
#使用sh.status()查看分片、数据库和分片集合的摘要信息

mongos> sh.status();
--- Sharding Status ---
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5842d23e32e5f535ca644556")
}
  shards:
    {  "_id" : "shard1",  "host" : "shard1/192.168.20.109:10000,192.168.20.109:10001" }
    {  "_id" : "shard2",  "host" : "shard2/192.168.20.109:20000,192.168.20.109:20001" }
    {  "_id" : "shard3",  "host" : "shard3/192.168.20.109:30000,192.168.20.109:30001" }
  active mongoses:
    "3.2.8" : 3
  balancer:
    Currently enabled:  yes
    Currently running:  no
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours:
        No recent migrations
  databases:
    {  "_id" : "test",  "primary" : "shard1",  "partitioned" : true }
        test.foo
            shard key: { "name" : 1 }
            unique: false
            balancing: true
            chunks:
                shard1    1
            { "name" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)

5.java客户端

https://github.com/jjavaboy/lam-nio/blob/master/lam-mongodb/src/main/java/lam/mongo/demo/ShardDemoTest.java

你可能感兴趣的:(mongodb)