Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB highly available deployment

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

规划部署情况如下:

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 db.aj.count();5082

246子节点验证数据量

246:SECONDARY> use testswitched to db test246:SECONDARY> db.getMongo().setSlaveOk();246:SECONDARY> db.aj.count();5082

247重复上述操作

247:PRIMARY> db.aj.count();4918247:SECONDARY> use test;switched to db test247:SECONDARY> db.getMongo().setSlaveOk();247:SECONDARY> db.aj.count();4918

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report