MongoDB高可用集群搭建
mongoDB副本集搭建
准备三台服务器:
10.175.120.131(主节点)
10.175.121.134(副本节点)
10.175.121.136(副本节点)
1.分别在每台服务器上安装MongoDB,并通过配置文件启动服务,配置文件如下:
dbpath=/data/db #指定数据库路径logpath=/data/mongodb.log #指定日志文件路径logappend=true #在日志文件持续添加port=27017 #端口号bind_ip=0.0.0.0 #允许远程访问fork=true #后台运行replSet=admin #设置副本集名称
2.进入MongoDB后台管理 Shell
./momgo
3.使用admin数据库
> use admin
4. 定义副本集配置变量,这里的 _id:”admin” 和配置文件中的“replSet=admin” 要保持一致
> config = { _id:"admin", members:[ {_id:0,host:"10.175.120.131:27017"}, {_id:1,host:"10.175.121.134:27017"}, {_id:2,host:"10.175.121.136:27017"} ] }
5.初始化副本集配置
> rs.initiate(config)
6.查看集群节点的状态
> rs.status()
输出:
{ "set" : "admin", "date" : ISODate("2018-09-21T08:52:24.064Z"), "myState" : 1, "term" : NumberLong(3), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) }, "appliedOpTime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) }, "durableOpTime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) } }, "members" : [ { "_id" : 1, "name" : "10.175.120.131:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 5874, "optime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2018-09-21T08:52:23Z"), "electionTime" : Timestamp(1537514082, 1), "electionDate" : ISODate("2018-09-21T07:14:42Z"), "configVersion" : 1, "self" : true }, { "_id" : 2, "name" : "10.175.121.134:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 5861, "optime" : { "ts" : Timestamp(1537519933, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1537519933, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2018-09-21T08:52:13Z"), "optimeDurableDate" : ISODate("2018-09-21T08:52:13Z"), "lastHeartbeat" : ISODate("2018-09-21T08:52:23.012Z"), "lastHeartbeatRecv" : ISODate("2018-09-21T08:52:23.584Z"), "pingMs" : NumberLong(0), "syncingTo" : "10.175.120.131:27017", "configVersion" : 1 }, { "_id" : 3, "name" : "10.175.121.136:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 5866, "optime" : { "ts" : Timestamp(1537519933, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1537519933, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2018-09-21T08:52:13Z"), "optimeDurableDate" : ISODate("2018-09-21T08:52:13Z"), "lastHeartbeat" : ISODate("2018-09-21T08:52:23.013Z"), "lastHeartbeatRecv" : ISODate("2018-09-21T08:52:23.558Z"), "pingMs" : NumberLong(0), "syncingTo" : "10.175.120.131:27017", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1537519943, 1), "$clusterTime" : { "clusterTime" : Timestamp(1537519943, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
输出部分参数说明:
"health" : 1, #代表机器正常 "stateStr" : "PRIMARY", #代表是主节点,可读写,其中有以下几下状态1. STARTUP:刚加入到复制集中,配置还未加载 2. STARTUP2:配置已加载完,初始化状态 3. RECOVERING:正在恢复,不适用读 4. ARBITER: 仲裁者 5. DOWN:节点不可到达 6. UNKNOWN:未获取其他节点状态而不知是什么状态,一般发生在只有两个成员的架构,脑裂 7. REMOVED:移除复制集 8. ROLLBACK:数据回滚,在回滚结束时,转移到RECOVERING或SECONDARY状态 9. FATAL:出错。查看日志grep “replSet FATAL”找出错原因,重新做同步 10. PRIMARY:主节点 11. SECONDARY:备份节点
作者:天使不哭
微信号:hgmyzhl
微信公众号:小明互联网技术分享社区
CSDN:IT技术分享社区
知乎:IT技术分享社区
出处:小明互联网技术分享社区
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.