小编给大家分享一下MongoDB搭建高可用集群的案例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一、规划好端口ip
架构图如下,任意抽取每个副本集中的一个分片(非仲裁节点)可以组成一份完整的数据。
1. 第一个副本集rs1
share110.0.0.7:30011:/data/share_rs/share_rs1/share1/data/ share210.0.0.7:40011:/data/share_rs/share_rs1/share2/data/ share310.0.0.7:50011:/data/share_rs/share_rs1/share3/data/
2. 第二个副本集rs2
share110.0.0.7:30012:/data/share_rs/share_rs2/share1/data/ share210.0.0.7:40012:/data/share_rs/share_rs2/share2/data/ share310.0.0.7:50012:/data/share_rs/share_rs2/share3/data/
3. 第三个副本集rs3
share110.0.0.7:30013:/data/share_rs/share_rs3/share1/data/ share210.0.0.7:40013:/data/share_rs/share_rs3/share2/data/ share310.0.0.7:50013:/data/share_rs/share_rs3/share3/data/
4.config server
config110.0.0.7:30002:/data/share_rs/config/config1/data/ config210.0.0.7:30002:/data/share_rs/config/config2/data/ config310.0.0.7:30002:/data/share_rs/config/config3/data/
5. mongos
mongos110.0.0.7:30001:/data/share_rs/mongos/mongos1/data/ mongos210.0.0.7:30001:/data/share_rs/mongos/mongos2/data/ mongos310.0.0.7:30001:/data/share_rs/mongos/mongos3/data/
二、创建相应的目录
mkdir-p/data/share_rs/{share_rs1,share_rs2,share_rs3}/{share1,share2,share3}/{data,log} mkdir-p/data/share_rs/mongos/{mongos1,mongos2,mongos3}/{data,log} mkdir-p/data/share_rs/config/{config1,config2,config3}/{data,log}
三、配置mongs和config的配置文件(其他副本参考修改端口以及ip)
[mongo@mongoconfig1]$catmongo.conf dbpath=/data/share_rs/config/config1/data/ logpath=/data/share_rs/config/config1/log/mongo.log logappend=true port=30002 fork=true rest=true httpinterface=true configsvr=true [mongo@mongomongs1]$catmongo.conf logpath=/data/share_rs/mongos/mongos1/log/mongo.log logappend=true port=30001 fork=true configdb=10.0.0.7:30002,10.0.0.7:40002,10.0.0.7:50002 chunkSize=1
四、依次启动三个副本上的config服务器以及mongs服务器
mongod-f/data/share_rs/config/config1/mongo.conf mongod-f/data/share_rs/config/config2/mongo.conf mongod-f/data/share_rs/config/config3/mongo.conf mongos-f/data/share_rs/mongos/mongos1/mongo.conf mongos-f/data/share_rs/mongos/mongos2/mongo.conf mongos-f/data/share_rs/mongos/mongos3/mongo.conf
五、配置mong分片的的配置文件(其他副本参考修改端口以及ip),同一个分片的副本集名称一样,即replSet。
第一个副本集的一个分片 [mongo@mongoshare_rs1]$catshare1/mongo.conf dbpath=/data/share_rs/share_rs1/share1/data logpath=/data/share_rs/share_rs1/share1/log/mongo.log logappend=true port=30011 fork=true rest=true httpinterface=true replSet=rs1 shardsvr=true 第二个副本集的一个分片 [mongo@mongoshare_rs2]$catshare1/mongo.conf dbpath=/data/share_rs/share_rs2/share1/data logpath=/data/share_rs/share_rs2/share1/log/mongo.log logappend=true port=30012 fork=true rest=true httpinterface=true replSet=rs2 shardsvr=true 第三个副本集的一个分片 [mongo@mongoshare_rs1]$catshare1/mongo.conf dbpath=/data/share_rs/share_rs3/share1/data logpath=/data/share_rs/share_rs3/share1/log/mongo.log logappend=true port=30013 fork=true rest=true httpinterface=true replSet=rs3 shardsvr=true
六、启动各个分片以及相应的副本
mongod-f/data/share_rs/share_rs1/share1/mongo.conf mongod-f/data/share_rs/share_rs1/share2/mongo.conf mongod-f/data/share_rs/share_rs1/share3/mongo.conf mongod-f/data/share_rs/share_rs2/share1/mongo.conf mongod-f/data/share_rs/share_rs2/share2/mongo.conf mongod-f/data/share_rs/share_rs2/share3/mongo.conf mongod-f/data/share_rs/share_rs3/share1/mongo.conf mongod-f/data/share_rs/share_rs3/share2/mongo.conf mongod-f/data/share_rs/share_rs3/share3/mongo.conf [mongo@mongoshare_rs]$ps-ef|grepmongo|grepshare|grep-vgrep mongo24801012:50?00:00:03mongod-f/data/share_rs/share_rs1/share1/mongo.conf mongo25061012:50?00:00:03mongod-f/data/share_rs/share_rs1/share2/mongo.conf mongo25321012:50?00:00:02mongod-f/data/share_rs/share_rs1/share3/mongo.conf mongo25581012:50?00:00:03mongod-f/data/share_rs/share_rs2/share1/mongo.conf mongo25841012:50?00:00:03mongod-f/data/share_rs/share_rs2/share2/mongo.conf mongo26101012:50?00:00:02mongod-f/data/share_rs/share_rs2/share3/mongo.conf mongo26361012:50?00:00:01mongod-f/data/share_rs/share_rs3/share1/mongo.conf mongo26621012:50?00:00:01mongod-f/data/share_rs/share_rs3/share2/mongo.conf mongo26881012:50?00:00:01mongod-f/data/share_rs/share_rs3/share3/mongo.conf mongo34691013:17?00:00:00mongod-f/data/share_rs/config/config1/mongo.conf mongo34851013:17?00:00:00mongod-f/data/share_rs/config/config2/mongo.conf mongo35131013:17?00:00:00mongod-f/data/share_rs/config/config3/mongo.conf mongo35351013:18?00:00:00mongos-f/data/share_rs/mongos/mongos1/mongo.conf mongo36291013:22?00:00:00mongos-f/data/share_rs/mongos/mongos2/mongo.conf mongo36781013:22?00:00:00mongos-f/data/share_rs/mongos/mongos3/mongo.conf
七、设置副本集
1.登录第一个副本的一个分片,为其设置副本集 mongo127.0.0.1:30011/admin config={_id:"rs1",members:[ {_id:0,host:"10.0.0.7:30011"}, {_id:1,host:"10.0.0.7:40011"}, {_id:2,host:"10.0.0.7:50011",arbiterOnly:true} ] } -->;注意:这里idrs1需要与副本集中的名称一样即replSet的值 rs.initiate(config) {"ok":1}-->;提示这个说明初始化成功 2.登录第二个副本的一个分片,为其设置副本集 mongo127.0.0.1:30012/admin config={_id:"rs2",members:[ {_id:0,host:"10.0.0.7:30012"}, {_id:1,host:"10.0.0.7:40012"}, {_id:2,host:"10.0.0.7:50012",arbiterOnly:true} ] } rs.initiate(config) {"ok":1}-->;提示这个说明初始化成功 3.登录第三个副本的一个分片,为其设置副本集 mongo127.0.0.1:30013/admin config={_id:"rs3",members:[ {_id:0,host:"10.0.0.7:30013"}, {_id:1,host:"10.0.0.7:40013"}, {_id:2,host:"10.0.0.7:50013",arbiterOnly:true} ] } rs.initiate(config) {"ok":1}-->;提示这个说明初始化成功
八、目前前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接mongos 路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
连接到第一个mongos上 mongo10.0.0.7:30001/admin db.runCommand({addshard:"rs1/10.0.0.7:30011,10.0.0.7:40011,10.0.0.7:50011",allowLocal:true}); db.runCommand({addshard:"rs2/10.0.0.7:30012,10.0.0.7:40012,10.0.0.7:50012"}); db.runCommand({addshard:"rs3/10.0.0.7:30013,10.0.0.7:40013,10.0.0.7:50013"}); -->;将第一个分片的所有副本全部加入 -->;如里shard是单台服务器,用db.runCommand({addshard:"[:]"})这样的命令加入 -->;如果shard是副本集,用db.runCommand({addshard:"replicaSetName/[:port][,serverhostname2[:port],…]"});这样的格式表示. mongos>;sh.status() ---ShardingStatus--- shardingversion:{ "_id":1, "minCompatibleVersion":5, "currentVersion":6, "clusterId":ObjectId("57f33f4d35d9c494714adfa7") } shards: {"_id":"rs1","host":"rs1/10.0.0.7:30011,10.0.0.7:40011"} {"_id":"rs2","host":"rs2/10.0.0.7:30012,10.0.0.7:40012"} {"_id":"rs3","host":"rs3/10.0.0.7:30013,10.0.0.7:40013"} activemongoses: "3.2.7":3 balancer: Currentlyenabled:yes Currentlyrunning:no Failedbalancerroundsinlast5attempts:0 MigrationResultsforthelast24hours: Norecentmigrations databases:
九、将集合进行分片。
db.runCommand({enablesharding:"testcol"}); -->;指定testdb分片生效 db.runCommand({shardcollection:"testcol.testdoc",key:{id:1}}) -->;指定数据库里需要分片的集合和片键 -->;插入测试数据 for(vari=1;i<;=100000;i++){db.testdoc.save({id:i,"name":"harvey"})};-->;查看该集合的状态 db.testcol.stats();
以上是“MongoDB搭建高可用集群的案例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恒创行业资讯频道!