然后打下mongo:
E:\mongodb\bin>mongo 回車 (有時(shí)加端口會(huì)造成下面的addshard命令出問題) > use admin switched to db admin > db.runCommand( { addshard : "10.0.4.85:27020", allowLocal : 1, maxSize:2 , minKey:1, maxKey:10} ) --添加sharding,maxsize單位是M,此處設(shè)置比較小的數(shù)值只為演示sharding效果 { "shardAdded" : "shard0000", "ok" : 1 } > db.runCommand( { addshard : "10.0.4.85:27021", allowLocal : 1, minKey:1000} ) { "shardAdded" : "shard0001", "ok" : 1 } 注:如果要移除sharding,可用下面寫法 db.runCommand( { removeshard : "localhost:10000" } ); > db.runCommand({listshards:1}); 查看shard節(jié)點(diǎn)列表 { "shards" : [ { "_id" : "shard0000", "host" : "10.0.4.85:27020" }, { "_id" : "shard0001", "host" : "10.0.4.85:27021" } ], "ok" : 1 }
接下來創(chuàng)建相應(yīng)數(shù)據(jù)庫并設(shè)置其"可以sharding",新建自動(dòng)切片的庫user001:
> config = connect("10.0.4.85:27022") > config = config.getSisterDB("config") > dnt_mongodb=db.getSisterDB("dnt_mongodb"); dnt_mongodb > db.runCommand({enablesharding:"dnt_mongodb"}) { "ok" : 1 }
注:一旦enable了個(gè)數(shù)據(jù)庫,mongos將會(huì)把數(shù)據(jù)庫里的不同數(shù)據(jù)集放在不同的分片上。除非數(shù)據(jù)集被分片(下面會(huì)設(shè)置),否則一個(gè)數(shù)據(jù)集的所有數(shù)據(jù)將放在一個(gè)分片上。
> db.printShardingStatus(); --- Sharding Status --- sharding version: { "_id" : 1, "version" : 3 } shards: { "_id" : "shard0000", "host" : "10.0.4.85:27020" } { "_id" : "shard0001", "host" : "10.0.4.85:27021" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "dnt_mongodb", "partitioned" : true, "primary" : "shard0000" } > db.runCommand( { shardcollection : "dnt_mongodb.posts1", key : {_id : 1}, unique: true } ) { "collectionsharded" : "dnt_mongodb.posts1", "ok" : 1 }
--使用shardcollection 命令分隔數(shù)據(jù)集,key自動(dòng)生成。
出處:CSDN
責(zé)任編輯:bluehearts
上一頁 基于Mongodb進(jìn)行分布式數(shù)據(jù)存儲(chǔ) [1] 下一頁 基于Mongodb進(jìn)行分布式數(shù)據(jù)存儲(chǔ) [3]
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|