规划部署情况如下:
成都地区优秀IDC服务器托管提供商(创新互联).为客户提供专业的绵阳服务器托管,四川各地服务器托管,绵阳服务器托管、多线服务器托管.托管咨询专线:189820811081、准备基础环境
在网上下载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 --shardsvrreplSet代表集群,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(); 5082246子节点验证数据量
246:SECONDARY> use test switched to db test 246:SECONDARY> db.getMongo().setSlaveOk(); 246:SECONDARY> db.aj.count(); 5082247重复上述操作
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另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:MongoDB高可用部署-创新互联
网页URL:http://scpingwu.com/article/dpiipd.html