您好,登錄后才能下訂單哦!
規(guī)劃部署情況如下:
1、準備基礎環(huán)境
在網(wǎng)上下載mongodb-linux-x86_64-rhel62-3.4.2.gz放到各個服務器上,解壓后放在對應的目錄下,然后在根目錄下新建data文件夾統(tǒng)一用于存放數(shù)據(jù)和配置信息。
2、搭建配置集群243-244-245
在mongo的根目錄下新建start腳本,內容如下
bin/./mongod --dbpath data/ --logpath data/mongo.log --configsvr --fork --port 20243 --replSet config
其中 configsvr表示配置服務,fork表示后臺啟動,replSet表示集群,config表示集群名字
同樣在其他配置服務上也建立對應的start啟動文件,記得修改端口哦。
啟動-3臺都啟動
./start
隨便進入一臺機器,進入mongo,運行如下命令
>config = { _id:"config", configsvr:true, members:[ {_id:0,host:'172.16.13.243:20243'}, {_id:1,host:'172.16.13.244:20244'}, {_id:2,host:'172.16.13.245:20245'} ] } { "_id" : "config", "configsvr" : true, "members" : [ { "_id" : 0, "host" : "172.16.13.243:20243" }, { "_id" : 1, "host" : "172.16.13.244:20244" }, { "_id" : 2, "host" : "172.16.13.245:20245" } ] } > rs.initiate(config) { "ok" : 1 }
查看集群狀態(tài)
config:PRIMARY> rs.status(); { "set" : "config", "date" : ISODate("2017-04-12T09:29:51.889Z"), "myState" : 1, "term" : NumberLong(1), "configsvr" : true, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "172.16.13.243:20243", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 621, "optime" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2017-04-12T09:29:42Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1491989360, 1), "electionDate" : ISODate("2017-04-12T09:29:20Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "172.16.13.244:20244", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 42, "optime" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2017-04-12T09:29:42Z"), "optimeDurableDate" : ISODate("2017-04-12T09:29:42Z"), "lastHeartbeat" : ISODate("2017-04-12T09:29:50.238Z"), "lastHeartbeatRecv" : ISODate("2017-04-12T09:29:51.467Z"), "pingMs" : NumberLong(0), "syncingTo" : "172.16.13.243:20243", "configVersion" : 1 }, { "_id" : 2, "name" : "172.16.13.245:20245", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 42, "optime" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1491989382, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2017-04-12T09:29:42Z"), "optimeDurableDate" : ISODate("2017-04-12T09:29:42Z"), "lastHeartbeat" : ISODate("2017-04-12T09:29:50.237Z"), "lastHeartbeatRecv" : ISODate("2017-04-12T09:29:51.537Z"), "pingMs" : NumberLong(0), "syncingTo" : "172.16.13.243:20243", "configVersion" : 1 } ], "ok" : 1 }
3、搭建分片集群246-247
在246機器的mongo的根目錄下新建start腳本,內容如下
[root@localhost mongodb-32246]# cat start bin/./mongod --dbpath data/ --logpath data/mongo.log --fork --port 32246 --replSet 246 --shardsvr
replSet代表集群,shardsvr代表分片,246代表集群的名字,在246的其他mongo目錄下也建立對應的start腳本,記得修改端口,啟動腳本。
運行如下命令
config = { ... _id:'246', ... members:[ ... {_id:0,host:'172.16.13.246:30246'}, ... {_id:1,host:'172.16.13.246:31246'}, ... {_id:2,host:'172.16.13.246:32246'} ... ] ... } { "_id" : "246", "members" : [ { "_id" : 0, "host" : "172.16.13.246:30246" }, { "_id" : 1, "host" : "172.16.13.246:31246" }, { "_id" : 2, "host" : "172.16.13.246:32246" } ] } > rs.initiate(config) { "ok" : 1 }
查看集群狀態(tài)
這里不在列出,同樣的操作在247上執(zhí)行一次,記得修改集群名字
4、搭建mongos
在241的mongos根目錄下建立start腳本
[root@localhost mongodb-3.4.2]# cat start bin/./mongos --logpath data/mongo.log --fork --configdb config/172.16.13.243:20243,172.16.13.244:20244,172.16.13.245:20245
進入mongos的命令界面,
添加分片
mongos> sh.addShard("246/172.16.13.246:30246,172.16.13.246:31246,172.16.13.246:32246") { "shardAdded" : "246", "ok" : 1 } mongos> sh.addShard("247/172.16.13.247:30247,172.16.13.247:31247,172.16.13.247:32247") { "shardAdded" : "247", "ok" : 1 }
在242上也運行上述命令。
設置test.aj表根據(jù)_id字段按照hash方式分配數(shù)據(jù)
mongos> sh.enableSharding("test") { "ok" : 1 } mongos> sh.shardCollection("test.aj", { _id: 'hashed'}) { "collectionsharded" : "test.aj", "ok" : 1 }
5、插入數(shù)據(jù)
for (var id = 1; id <= 10000; id++) db.aj.save({ "_class" : "java.util.HashMap", "CAh" : "123", "CCbrXm" : "wangchy", "valid" : 0, "fydm" : "2400", "CAjmc" : "ptest", "CCbr" : "ptest", "WCreateTime" : ISODate("2015-10-13T18:29:06.649Z") });
6、驗證結果
246主節(jié)點驗證數(shù)據(jù)量
246:PRIMARY> db.aj.count(); 5082
246子節(jié)點驗證數(shù)據(jù)量
246:SECONDARY> use test switched to db test 246:SECONDARY> db.getMongo().setSlaveOk(); 246:SECONDARY> db.aj.count(); 5082
247重復上述操作
247:PRIMARY> db.aj.count(); 4918 247:SECONDARY> use test; switched to db test 247:SECONDARY> db.getMongo().setSlaveOk(); 247:SECONDARY> db.aj.count(); 4918
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。