`

mongodb-sharding-replicate sets

阅读更多
3台机器,3个复制集(shard1,shard2,shard3),3个shards ,3个config servers,3个mongos

机器:
192.168.1.158
192.168.1.224
192.168.1.78
-------------------------------------------------------------------------------
先建目录(192.168.1.158)
/data/servers/sharding/shard1-1、/data/servers/sharding/shard2-1、/data/servers/sharding/shard3-1、/data/servers/sharding/config

192.168.1.224
/data/servers/sharding/shard1-2、/data/servers/sharding/shard2-2、/data/servers/sharding/shard3-2、/data/servers/sharding/config

192.168.1.78
/data/servers/sharding/shard1-3、/data/servers/sharding/shard2-3、/data/servers/sharding/shard3-3、/data/servers/sharding/config
----------------------------------------------------------------------------
分别在3台机器上启动复制集shard1:
192.168.1.158
bin/mongod --shardsvr --replSet shard1 --dbpath /data/servers/sharding/shard1-1 --logpath /data/servers/sharding/shard1.log --port 28011  --logappend --oplogSize 100 --fork
192.168.1.224
bin/mongod --shardsvr --replSet shard1 --dbpath /data/servers/sharding/shard1-2 --logpath /data/servers/sharding/shard1.log --port 28011  --logappend --oplogSize 100 --fork
192.168.1.78
bin/mongod --shardsvr --replSet shard1 --dbpath /data/servers/sharding/shard1-3 --logpath /data/servers/sharding/shard1.log --port 28011  --logappend --oplogSize 100 --fork
用 mongo 连接其中一台机器的 28011端口的 mongod,初始化 Replica Sets“shard1”,执行:
bin/mongo  --port 28011
> use admin
>config = {_id:'shard1',members:[ {_id:0,host:'192.168.1.158:28011'}, {_id:1,host:'192.168.1.224:28011'}, {_id:2,host:'192.168.1.78:28011'}]}
>rs.initiate(config)
----------------------------------------------------------
分别在3台机器上启动复制集shard2:
192.168.1.158
bin/mongod --shardsvr --replSet shard2 --dbpath /data/servers/sharding/shard2-1 --logpath /data/servers/sharding/shard2.log --port 28022  --logappend --oplogSize 100 --fork
192.168.1.224
bin/mongod --shardsvr --replSet shard2 --dbpath /data/servers/sharding/shard2-2 --logpath /data/servers/sharding/shard2.log --port 28022  --logappend --oplogSize 100 --fork
192.168.1.78
bin/mongod --shardsvr --replSet shard2 --dbpath /data/servers/sharding/shard2-3 --logpath /data/servers/sharding/shard2.log --port 28022  --logappend --oplogSize 100 --fork
用 mongo 连接其中一台机器的 28022端口的 mongod,初始化 Replica Sets“shard1”,执行:
bin/mongo --port 28022
> use admin
>config = {_id:'shard2',members:[ {_id:0,host:'192.168.1.158:28022'}, {_id:1,host:'192.168.1.224:28022'}, {_id:2,host:'192.168.1.78:28022'}]}
>rs.initiate(config)
------------------------------------------------------------
分别在3台机器上启动复制集shard3:
192.168.1.158
bin/mongod --shardsvr --replSet shard3 --dbpath /data/servers/sharding/shard3-1 --logpath /data/servers/sharding/shard3.log --port 28033  --logappend --oplogSize 100 --fork
192.168.1.224
bin/mongod --shardsvr --replSet shard3 --dbpath /data/servers/sharding/shard3-2 --logpath /data/servers/sharding/shard3.log --port 28033  --logappend --oplogSize 100 --fork
192.168.1.78
bin/mongod --shardsvr --replSet shard3 --dbpath /data/servers/sharding/shard3-3 --logpath /data/servers/sharding/shard3.log --port 28033  --logappend --oplogSize 100 --fork
用 mongo 连接其中一台机器的 28033端口的 mongod,初始化 Replica Sets“shard1”,执行:
bin/mongo --port 28033
> use admin
>config = {_id:'shard3',members:[ {_id:0,host:'192.168.1.158:28033'}, {_id:1,host:'192.168.1.224:28033'}, {_id:2,host:'192.168.1.78:28033'}]}
>rs.initiate(config)

------------------------------------------------------------------------------------------------------------
分别在3台机器上启动config server
bin/mongod --configsvr --dbpath /data/servers/sharding/config --port 20000 --logpath /data/servers/sharding/config.log  --logappend  --fork
(操作的时候误把--configsvr 写成 --shardsvr ,不知道后果会怎么样。。。)
---------------------------------------------------------------------------------------------------------------------
配置 3 台 Route Process, 分别在3台机器上启动mongos
bin/mongos --configdb 192.168.1.158:20000,192.168.1.224:20000,192.168.1.78:20000  --port 30000 --logpath /data/servers/sharding/mongos.log  --logappend  --fork
----------------------------------------------------------------------------------------------------------
配置 shard cluster ,连接到其中一台机器的端口 30000 的 mongos 进程,并切换到 admin 数据库做以下配置 (这样也行:sh.addShard("shard1/192.168.1.158:28011") 版本2.2前是用下面这种方式,要有三个host)
bin/mongo  --port 3000
>use admin
mongos>db.runCommand({addshard:"shard1/192.168.1.158:28011,192.168.1.224:28011,192.168.1.78:28011"});
{ "shardAdded" : "shard1", "ok" : 1 }
mongos>db.runCommand({addshard:"shard2/192.168.1.158:28022,192.168.1.224:28022,192.168.1.78:28022"});
{ "shardAdded" : "shard2", "ok" : 1 }
mongos>db.runCommand({addshard:"shard3/192.168.1.158:28033,192.168.1.224:28033,192.168.1.78:28033"});
{ "shardAdded" : "shard3", "ok" : 1 }

×××××××××××××××测试sharding××××××××××××××××××××
激活数据库及集合的分片
mongos> db.runCommand({enablesharding:"test"})
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"test.users",key:{_id:1}})
{ "collectionsharded" : "test.users", "ok" : 1 }
mongos> use test
switched to db test
插入数据测试
mongos> for (var i=1;i<=200000;i++) db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"});
WriteResult({ "nInserted" : 1 })
mongos> db.users.stats()
{
"sharded" : true,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"ns" : "test.users",
"count" : 199999,
"numExtents" : 14,
"size" : 22399888,
"storageSize" : 33697792,
"totalIndexSize" : 6516272,
"indexSizes" : {
"_id_" : 6516272
},
"avgObjSize" : 112,
"nindexes" : 1,
"nchunks" : 5,
"shards" : {
"shard1" : {
"ns" : "test.users",
"count" : 38932,
"size" : 4360384,
"avgObjSize" : 112,
"numExtents" : 6,
"storageSize" : 11182080,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 1,
"totalIndexSize" : 1267280,
"indexSizes" : {
"_id_" : 1267280
},
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("56d11f4e39d85dcded6d7706")
}
},
"shard2" : {
"ns" : "test.users",
"count" : 161066,
"size" : 18039392,
"avgObjSize" : 112,
"numExtents" : 7,
"storageSize" : 22507520,
"lastExtentSize" : 11325440,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 1,
"totalIndexSize" : 5240816,
"indexSizes" : {
"_id_" : 5240816
},
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("56d1209068bcf815c91b4cdd")
}
},
"shard3" : {
"ns" : "test.users",
"count" : 1,
"size" : 112,
"avgObjSize" : 112,
"numExtents" : 1,
"storageSize" : 8192,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 1,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("56d121247c2081829a8e3044")
}
}
},
"ok" : 1
}












分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics