MongoDB分片

前面学的主从复制,副本集都是解决安全性问题的。现在学习的分片,是解决性能上问题的。在下面三种情况下,我们需要考虑用分片:

1、机器的磁盘空间不足
2、单个的mongoDB服务器已经不能满足大量的插入操作
3、想通过把大数据放到内存中来提高性能

目前就我的理解来看,分片的功能就是把某个或几个数据库数据,拆分的放到多个分片数据库上。它们一个最简单的构成是,1台路由服务器,1台配置服务器,2台分片服务器。我们实际上操作的是路由服务器,路由服务器通过配置服务器来存取数据。下面来演示一下:

配置服务器.conf

dbpath = D:\MongoDB\8888\config
port = 2000
bind_ip = 127.0.0.1

配置服务器.bat

mongod --config 配置服务器.conf


路由服务器.bat

mongos --port 1000 --configdb 127.0.0.1:2000     -------------------------注意,configdb是指向的配置服务器端口

路由服务器客户端.bat

mongo 127.0.0.1:1000/admin


分片1.conf

dbpath = D:\MongoDB\8888\0801
port = 8081
bind_ip = 127.0.0.1

分片1.bat

mongod --config 分片1.conf

分片1client.bat

mongo 127.0.0.1:8081/admin


分片2.conf

dbpath = D:\MongoDB\8888\0802
port = 8082
bind_ip = 127.0.0.1

分片2.bat

mongod --config 分片2.conf

分片2client.bat

mongo 127.0.0.1:8082/admin


1、依次打开配置服务器---->路由服务器------->分片1---------->分片2  (注意,实际应用时conf文件不要用中文打不开)

2、进到路由服务器中,执行   ------------------------操作这句之前,不可操作数据库

connecting to: 127.0.0.1:1000/admin
mongos> db.runCommand({addshard:"127.0.0.1:8081",allowLocal:true})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:"127.0.0.1:8082",allowLocal:true})
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos>
3、开数据分片功能,为数据库foobar打开分片功能,在这之前,我都还没有建foobar数据库,但是没有关系,还是会执行成功

mongos> db.runCommand({"enablesharding":"foobar"})
{ "ok" : 1 }
mongos>

4、建立foobar数据库,并在其内增加一个集合bar

mongos> use foobar
switched to db foobar
mongos> db.bar.insert({name:1})
WriteResult({ "nInserted" : 1 })
mongos>

5、对bar集合进行分片   --------------------这里得回到admin下操作才成功
mongos> use admin
switched to db admin
mongos> db.runCommand({"shardcollection":"foobar.bar","key":{"_id":1}})
{ "collectionsharded" : "foobar.bar", "ok" : 1 }

6、插入大数据(1000000条),路由服务器执行,对数据库的操作都在路由服务器上执行
mongos> function add(){
...  var i = 0;
...  for(;i<200000;i++){
...    db.bar.insert({"age":i+10,"name":"jim"})
...  }
... }
mongos> add()
mongos>

7、在路由服务器上查询插入数据量

mongos> db.bar.find().count()
1000001
mongos>

8、在分片1上查询插入的数据量
> db.bar.find().count()
369808
>

9、在分片2上查询插入的数据量

> db.bar.find().count()
630193
>

分片成功。

10、在配置服务器上查看分片情况

> db.printShardingStatus()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("57820af96aeb53c848d4a707")
}
  shards:
        {  "_id" : "shard0000",  "host" : "127.0.0.1:8081" }
        {  "_id" : "shard0001",  "host" : "127.0.0.1:8082" }
  active mongoses:
        "3.2.7" : 1
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                3 : Success
  databases:
        {  "_id" : "foobar",  "primary" : "shard0000",  "partitioned" : true }
                foobar.bar
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard0000       4
                                shard0001       3
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("
7820e334ed6e8367928b1da") } on : shard0000 Timestamp(4, 1)
                        { "_id" : ObjectId("57820e334ed6e8367928b1da") } -->> {
"_id" : ObjectId("57820e334ed6e8367928b1ee") } on : shard0000 Timestamp(1, 2)
                        { "_id" : ObjectId("57820e334ed6e8367928b1ee") } -->> {
"_id" : ObjectId("57820e994ed6e836792b4eaa") } on : shard0001 Timestamp(3, 1)
                        { "_id" : ObjectId("57820e994ed6e836792b4eaa") } -->> {
"_id" : ObjectId("57820f2e4ed6e836792e5669") } on : shard0001 Timestamp(2, 3)
                        { "_id" : ObjectId("57820f2e4ed6e836792e5669") } -->> {
"_id" : ObjectId("57820fc64ed6e83679310113") } on : shard0000 Timestamp(3, 2)
                        { "_id" : ObjectId("57820fc64ed6e83679310113") } -->> {
"_id" : ObjectId("5782106c4ed6e8367933fae4") } on : shard0000 Timestamp(3, 3)
                        { "_id" : ObjectId("5782106c4ed6e8367933fae4") } -->> {
"_id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(4, 0)

>

10、在配置服务器上查看自动分片机制配置信息
> show dbs
config  0.001GB
local   0.000GB
> use config
switched to db config
> show collections
changelog
chunks
collections
databases
lockpings
locks
mongos
settings
shards
tags
version
> db.shards.find()
{ "_id" : "shard0000", "host" : "127.0.0.1:8081" }
{ "_id" : "shard0001", "host" : "127.0.0.1:8082" }
> db.mongos.find()
{ "_id" : "USER-20160220HU:1000", "ping" : ISODate("2016-07-10T09:28:23.282Z"),
"up" : NumberLong(2616), "waiting" : true, "mongoVersion" : "3.2.7" }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值