您的位置:首页 > 数据库 > Mongodb

mongodb_分布式存储_切片

2013-10-13 16:35 447 查看
我选用了ip为10.120.20.183这台机器,

然后再D盘准备了3个mongodb数据库:



后边紧跟的是端口号,其中27022是配置(server config)

然后我又准备了3个db的存储空间:



进行存储的配置:
D:/mongodb_27020/bin/mongod --dbpath D:/27020_db/ --port 27020
D:/mongodb_27021/bin/mongod --dbpath D:/27021_db/ --port 27021
D:/mongodb_27022_conf/bin/mongod --configsvr --dbpath D:/27022_db/ --port 27022



 启动mongos时,默认开启了27017端口

     D:/mongodb_27022_conf/bin/mongos --configdb 10.120.20.183:27022

然后打下mongo:

      D:/mongodb_27022_conf/bin>mongo   回车  (有时加端口会造成下面的addshard命令出问题)
     > use admin

          switched to db admin

      > db.runCommand( { addshard : "10.120.20.183:27020", allowLocal : 1, maxSize:2 , minKey:1, maxKey:10} ) 

         --添加sharding,maxsize单位是M,此处设置比较小的数值只为演示sharding效果

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

      > db.runCommand( { addshard : "10.120.20.183:27021", allowLocal : 1, minKey:1000} )
         { "shardAdded" : "shard0001", "ok" : 1 }     

          注:如果要移除sharding,可用下面写法

          db.runCommand( { removeshard : "localhost:10000" } );

      > db.runCommand({listshards:1});   查看shard节点列表    

      {

        "shards" : [

                {

                        "_id" : "shard0000",

                        "host" : "10.0.4.85:27020"

                },

                {

                        "_id" : "shard0001",

                        "host" : "10.0.4.85:27021"

                }

        ],

        "ok" : 1

      }



接下来创建相应数据库并设置其"可以sharding",新建自动切片的库user001:
     >config = connect("10.120.20.183:27022/config")
     > dnt_mongodb=db.getSisterDB("dnt_mongodb")
     >db.runCommand({enablesharding:"dnt_mongodb"})



 注:一旦enable了个数据库,mongos将会把数据库里的不同数据集放在不同的分片上。除非数据集被分片(下面会设置),否则一个数据集的所有数据将放在一个分片上。

>db.printShardingStatus();



>db.runCommand( { shardcollection : "dnt_mongodb.posts1", key : {_id : 1}, unique: true } ) 
--使用shardcollection 命令分隔数据集,key自动生成。 



              如果要进行GridFS sharding,则需进行如下设置:
            db.runCommand( { shardcollection : "test.fs.chunks", key : { _id : 1 } } )

            {"ok" : 1} ,更多内容参见http://eshilin.blog.163.com/blog/static/13288033020106215227346/

>db.printShardingStatus()



然后向里边插入测试数据(测试数据的程序我已经放到csdn:http://download.csdn.net/detail/dulei294948/6392945)

>db.posts1.stats()

{

        "sharded" : true,

        "ns" : "dnt_mongodb.posts1",

        "count" : 623920,

        "numExtents" : 17,

        "size" : 58696172,

        "storageSize" : 103546880,

        "totalIndexSize" : 20292832,

        "indexSizes" : {

                "_id_" : 20292832

        },

        "avgObjSize" : 94.07643928708809,

        "nindexes" : 1,

        "nchunks" : 6,

        "shards" : {

                "shard0000" : {

                        "ns" : "dnt_mongodb.posts1",

                        "count" : 406761,

                        "size" : 37897560,

                        "avgObjSize" : 93.16910913288147,

                        "storageSize" : 65748992,

                        "numExtents" : 9,

                        "nindexes" : 1,

                        "lastExtentSize" : 23224320,

                        "paddingFactor" : 1,

                        "systemFlags" : 1,

                        "userFlags" : 0,

                        "totalIndexSize" : 13228768,

                        "indexSizes" : {

                                "_id_" : 13228768

                        },

                        "ok" : 1

                },

                "shard0001" : {

                        "ns" : "dnt_mongodb.posts1",

                        "count" : 217159,

                        "size" : 20798612,

                        "avgObjSize" : 95.77596139234386,

                        "storageSize" : 37797888,

                        "numExtents" : 8,

                        "nindexes" : 1,

                        "lastExtentSize" : 15290368,

                        "paddingFactor" : 1,

                        "systemFlags" : 1,

                        "userFlags" : 0,

                        "totalIndexSize" : 7064064,

                        "indexSizes" : {

                                "_id_" : 7064064

                        },

                        "ok" : 1

                }

        },

        "ok" : 1

}

上边的count就是分开的记录数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mongodb 分布式 切片