规划部署情况如下:
成都创新互联-专业网站定制、快速模板网站建设、高性价比天津网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式天津网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖天津地区。费用合理售后完善,十多年实体公司更值得信赖。
1、准备基础环境
在网上下载MongoDB-linux-x86_64-rhel62-3.4.2.gz放到各个服务器上,解压后放在对应的目录下,然后在根目录下新建data文件夹统一用于存放数据和配置信息。
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 }
查看集群状态
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 }
查看集群状态
这里不在列出,同样的操作在247上执行一次,记得修改集群名字
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表根据_id字段按照hash方式分配数据
mongos> sh.enableSharding("test") { "ok" : 1 } mongos> sh.shardCollection("test.aj", { _id: 'hashed'}) { "collectionsharded" : "test.aj", "ok" : 1 }
5、插入数据
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主节点验证数据量
246:PRIMARY> db.aj.count(); 5082
246子节点验证数据量
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