mongodb複製集和分片混合集羣搭建(原創)

主機             ip                                服務及端口
ServerA       192.168.100.127        mongodb shard1:22001
                                                       mongodb shard2:22002
                                                       mongodb shard3:22003
                                                       mongodb config:21000
                                                       mongos:20000
ServerB       192.168.100.152        mongodb shard1:22001
                                                       mongodb shard2:22002
                                                       mongodb shard3:22003
                                                       mongodb config:21000
                                                       mongos:20000
ServerC       192.168.100.154        mongodb shard1:22001
                                                       mongodb shard2:22002
                                                       mongodb shard3:22003
                                                       mongodb config:21000
                                                       mongos:20000  

1、配置shard所用到的Replica Sets,在每臺服務器上執行以下命令:
[root@localhost ~]# cd /data/mongodbsrs/mongodb/bin/
[root@localhost bin]# ./mongod --shardsvr --replSet shard1 --port 22001 --dbpath /data/mongodbsrs/data/shard1/ --logpath /data/mongodbsrs/log/shard1/shard1.log --logappend --fork
[root@localhost bin]# ./mongod --shardsvr --replSet shard2 --port 22002 --dbpath /data/mongodbsrs/data/shard2/ --logpath /data/mongodbsrs/log/shard2/shard2.log --logappend --fork
[root@localhost bin]# ./mongod --shardsvr --replSet shard3 --port 22003 --dbpath /data/mongodbsrs/data/shard3/ --logpath /data/mongodbsrs/log/shard3/shard3.log --logappend --fork
2、用mongo連接其中任意一臺機器,初始化Replica Sets,分別連接每一個shard的端口號進行Replica Sets的初始化,命令如下:
[root@localhost ~]# cd /data/mongodbsrs/mongodb/bin/
[root@localhost bin]# ./mongo --port 22001
> config = {_id:"shard1",members:[{_id:0,host:"192.168.100.127:22001"},{_id:1,host:"192.168.100.152:22001"},{_id:2,host:"192.168.100.154:22001"}]};
> rs.initiate(config);
[root@localhost ~]# cd /data/mongodbsrs/mongodb/bin/
[root@localhost bin]# ./mongo --port 22002
> config = {_id:"shard2",members:[{_id:0,host:"192.168.100.127:22002"},{_id:1,host:"192.168.100.152:22002"},{_id:2,host:"192.168.100.154:22002"}]};
> rs.initiate(config);
[root@localhost ~]# cd /data/mongodbsrs/mongodb/bin/
[root@localhost bin]# ./mongo --port 22003
> config = {_id:"shard3",members:[{_id:0,host:"192.168.100.127:22003"},{_id:1,host:"192.168.100.152:22003"},{_id:2,host:"192.168.100.154:22003"}]};
> rs.initiate(config);
3、配置每臺Config Server,在每臺服務器上執行以下命令:
[root@localhost bin]# ./mongod --configsvr --dbpath /data/mongodbsrs/data/config/ --port 21000 --logpath /data/mongodbsrs/log/config/config.log --logappend --fork
4、配置每臺Route Process,在每臺服務器上執行以下命令:
[root@localhost bin]# ./mongos --configdb 192.168.100.127:21000,192.168.100.152:21000,192.168.100.154:21000 --port 20000 --chunkSize 1 --logpath /data/mongodbsrs/log/mongos/mongos.log --logappend --fork
5、配置Shard Cluster,連接到其中任意一臺機器的20000端口的mongos進程,並切換到admin數據庫做以下配置:
[root@localhost ~]# cd /data/mongodbsrs/mongodb/bin/
[root@localhost bin]# ./mongo --port 20000
mongos> use admin;
mongos> db.runCommand({addshard:"shard1/192.168.100.127:22001,192.168.100.152:22001,192.168.100.154:22001"});
mongos> db.runCommand({addshard:"shard2/192.168.100.127:22002,192.168.100.152:22002,192.168.100.154:22002"});
mongos> db.runCommand({addshard:"shard3/192.168.100.127:22003,192.168.100.152:22003,192.168.100.154:22003"});
激活數據庫及集合的分片:
mongos> db.runCommand({enablesharding:"testdb"});
mongos> db.runCommand({shardcollection:"testdb.users",key:{_id:1}});
6、驗證Sharding正常工作,連接到其中任意一臺機器的20000端口的mongos進程,並切換到testdb數據庫,以便添加測試數據
[root@localhost ~]# cd /data/mongodbsrs/mongodb/bin/
[root@localhost bin]# ./mongo --port 20000
mongos> use testdb;
mongos> for(var i = 1; i <= 200000; i++)db.users.insert({id:i,addr1:"beijing",addr2:"shanghai"});
mongos> db.users.stats();
{
"sharded" : true,
"capped" : false,
"ns" : "testdb.users",
"count" : 200000,
"size" : 14600000,
"storageSize" : 4055040,
"totalIndexSize" : 1740800,
"indexSizes" : {
"_id_" : 1740800
},
"avgObjSize" : 73,
"nindexes" : 1,
"nchunks" : 27,
"shards" : {
"shard1" : {
"ns" : "testdb.users",
"count" : 73580,
"size" : 5371340,
"avgObjSize" : 73,
"storageSize" : 1490944,
"capped" : false,
......
"nindexes" : 1,
"totalIndexSize" : 638976,
"indexSizes" : {
"_id_" : 638976
},
"ok" : 1
},
"shard2" : {
"ns" : "testdb.users",
"count" : 62580,
"size" : 4568340,
"avgObjSize" : 73,
"storageSize" : 1269760,
"capped" : false,
......
"nindexes" : 1,
"totalIndexSize" : 544768,
"indexSizes" : {
"_id_" : 544768
},
"ok" : 1
},
"shard3" : {
"ns" : "testdb.users",
"count" : 63840,
"size" : 4660320,
"avgObjSize" : 73,
"storageSize" : 1294336,
"capped" : false,
......
"nindexes" : 1,
"totalIndexSize" : 557056,
"indexSizes" : {
"_id_" : 557056
},
"ok" : 1
}
},
"ok" : 1
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章