MongoDB搭建高可用集群的案例
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,小编给大家分享一下MongoDB搭建高可用集群的案例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、规划好端口ip架构图
千家信息网最后更新 2025年02月01日MongoDB搭建高可用集群的案例
小编给大家分享一下MongoDB搭建高可用集群的案例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一、规划好端口ip
架构图如下,任意抽取每个副本集中的一个分片(非仲裁节点)可以组成一份完整的数据。
1. 第一个副本集rs1
share1 10.0.0.7:30011:/data/share_rs/share_rs1/share1/data/share2 10.0.0.7:40011:/data/share_rs/share_rs1/share2/data/share3 10.0.0.7:50011:/data/share_rs/share_rs1/share3/data/
2. 第二个副本集rs2
share1 10.0.0.7:30012:/data/share_rs/share_rs2/share1/data/share2 10.0.0.7:40012:/data/share_rs/share_rs2/share2/data/share3 10.0.0.7:50012:/data/share_rs/share_rs2/share3/data/
3. 第三个副本集rs3
share1 10.0.0.7:30013:/data/share_rs/share_rs3/share1/data/share2 10.0.0.7:40013:/data/share_rs/share_rs3/share2/data/share3 10.0.0.7:50013:/data/share_rs/share_rs3/share3/data/
4.config server
config1 10.0.0.7:30002:/data/share_rs/config/config1/data/config2 10.0.0.7:30002:/data/share_rs/config/config2/data/config3 10.0.0.7:30002:/data/share_rs/config/config3/data/
5. mongos
mongos1 10.0.0.7:30001:/data/share_rs/mongos/mongos1/data/mongos2 10.0.0.7:30001:/data/share_rs/mongos/mongos2/data/mongos3 10.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@mongo config1]$ cat mongo.confdbpath=/data/share_rs/config/config1/data/logpath=/data/share_rs/config/config1/log/mongo.loglogappend=trueport=30002fork=truerest=truehttpinterface=trueconfigsvr=true[mongo@mongo mongs1]$ cat mongo.conf logpath=/data/share_rs/mongos/mongos1/log/mongo.loglogappend=trueport=30001fork=trueconfigdb=10.0.0.7:30002,10.0.0.7:40002,10.0.0.7:50002chunkSize=1
四、依次启动三个副本上的config服务器以及mongs服务器
mongod -f /data/share_rs/config/config1/mongo.confmongod -f /data/share_rs/config/config2/mongo.confmongod -f /data/share_rs/config/config3/mongo.confmongos -f /data/share_rs/mongos/mongos1/mongo.confmongos -f /data/share_rs/mongos/mongos2/mongo.confmongos -f /data/share_rs/mongos/mongos3/mongo.conf
五、配置mong分片的的配置文件(其他副本参考修改端口以及ip),同一个分片的副本集名称一样,即replSet。
第一个副本集的一个分片[mongo@mongo share_rs1]$ cat share1/mongo.confdbpath=/data/share_rs/share_rs1/share1/datalogpath=/data/share_rs/share_rs1/share1/log/mongo.loglogappend=trueport=30011fork=truerest=truehttpinterface=truereplSet=rs1shardsvr=true第二个副本集的一个分片[mongo@mongo share_rs2]$ cat share1/mongo.confdbpath=/data/share_rs/share_rs2/share1/datalogpath=/data/share_rs/share_rs2/share1/log/mongo.loglogappend=trueport=30012fork=truerest=truehttpinterface=truereplSet=rs2shardsvr=true第三个副本集的一个分片[mongo@mongo share_rs1]$ cat share1/mongo.confdbpath=/data/share_rs/share_rs3/share1/datalogpath=/data/share_rs/share_rs3/share1/log/mongo.loglogappend=trueport=30013fork=truerest=truehttpinterface=truereplSet=rs3shardsvr=true
六、启动各个分片以及相应的副本
mongod -f /data/share_rs/share_rs1/share1/mongo.confmongod -f /data/share_rs/share_rs1/share2/mongo.confmongod -f /data/share_rs/share_rs1/share3/mongo.confmongod -f /data/share_rs/share_rs2/share1/mongo.confmongod -f /data/share_rs/share_rs2/share2/mongo.confmongod -f /data/share_rs/share_rs2/share3/mongo.confmongod -f /data/share_rs/share_rs3/share1/mongo.confmongod -f /data/share_rs/share_rs3/share2/mongo.confmongod -f /data/share_rs/share_rs3/share3/mongo.conf[mongo@mongo share_rs]$ ps -ef | grep mongo | grep share | grep -v grepmongo 2480 1 0 12:50 ? 00:00:03 mongod -f /data/share_rs/share_rs1/share1/mongo.confmongo 2506 1 0 12:50 ? 00:00:03 mongod -f /data/share_rs/share_rs1/share2/mongo.confmongo 2532 1 0 12:50 ? 00:00:02 mongod -f /data/share_rs/share_rs1/share3/mongo.confmongo 2558 1 0 12:50 ? 00:00:03 mongod -f /data/share_rs/share_rs2/share1/mongo.confmongo 2584 1 0 12:50 ? 00:00:03 mongod -f /data/share_rs/share_rs2/share2/mongo.confmongo 2610 1 0 12:50 ? 00:00:02 mongod -f /data/share_rs/share_rs2/share3/mongo.confmongo 2636 1 0 12:50 ? 00:00:01 mongod -f /data/share_rs/share_rs3/share1/mongo.confmongo 2662 1 0 12:50 ? 00:00:01 mongod -f /data/share_rs/share_rs3/share2/mongo.confmongo 2688 1 0 12:50 ? 00:00:01 mongod -f /data/share_rs/share_rs3/share3/mongo.confmongo 3469 1 0 13:17 ? 00:00:00 mongod -f /data/share_rs/config/config1/mongo.confmongo 3485 1 0 13:17 ? 00:00:00 mongod -f /data/share_rs/config/config2/mongo.confmongo 3513 1 0 13:17 ? 00:00:00 mongod -f /data/share_rs/config/config3/mongo.confmongo 3535 1 0 13:18 ? 00:00:00 mongos -f /data/share_rs/mongos/mongos1/mongo.confmongo 3629 1 0 13:22 ? 00:00:00 mongos -f /data/share_rs/mongos/mongos2/mongo.confmongo 3678 1 0 13:22 ? 00:00:00 mongos -f /data/share_rs/mongos/mongos3/mongo.conf
七、设置副本集
1.登录第一个副本的一个分片,为其设置副本集mongo 127.0.0.1:30011/adminconfig = { _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} ] }-- >; 注意:这里id rs1 需要与副本集中的名称一样即replSet的值rs.initiate(config){ "ok" : 1 } -- >; 提示这个说明初始化成功 2.登录第二个副本的一个分片,为其设置副本集mongo 127.0.0.1:30012/adminconfig = { _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.登录第三个副本的一个分片,为其设置副本集mongo 127.0.0.1:30013/adminconfig = { _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上mongo 10.0.0.7:30001/admindb.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()--- Sharding Status --- sharding version: { "_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" } active mongoses: "3.2.7" : 3 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases:
九、将集合进行分片。
db.runCommand({enablesharding:"testcol"});-- >; 指定testdb分片生效db.runCommand({shardcollection: "testcol.testdoc",key : {id: 1} } )-- >; 指定数据库里需要分片的集合和片键-->; 插入测试数据for (var i = 1; i <;= 100000; i++){ db.testdoc.save({id:i,"name":"harvey"})}; -- >; 查看该集合的状态db.testcol.stats();
以上是"MongoDB搭建高可用集群的案例"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
副本
服务器
服务
配置
三个
成功
数据
端口
篇文章
参考
提示
登录
案例
集群
建高
内容
名称
文件
程序
路由
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
jdbc连接数据库四个步骤
我的世界粘土服务器粘土大逃杀
世界互联网公司领先科技成果
搭建一个服务器要几天
泉州诺亚软件开发
服务器硬盘寿命监控
换流站网络安全防护的要求
放舟生存进化手游怎么开服务器
怎么提取个人大数据库
迪普企业网络安全
无法链接服务器1-10087
细胞凋亡基因数据库
闵行区项目数据库服务商收费标准
网络安全002912
淘宝服务器能不能出错
完全自主代码数据库
枣庄市网络安全领导小组
金山区网络软件开发厂家价格
文献信息最佳数据库
医院网络技术与管理
招聘c软件开发
上海图书馆 数据库
数据库怎么截断字
软件开发者模式怎么关
mongo数据库连接
领扣 数据库题目 答案
枣庄市网络安全领导小组
移动硬盘数据恢复服务器
揭阳分布式服务器价格
网络安全的市场需求大吗