欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MongoDB高可用集群搭建

程序员文章站 2023-11-09 08:22:40
MongoDB副本集搭建 准备三台服务器: 10.175.120.131(主节点) 10.175.121.134(副本节点) 10.175.121.136(副本节点) 1.分别在每台服务器上安装MongoDB,并通过配置文件启动服务,配置文件如下: 2.进入MongoDB后台管理 Shell 3.使 ......

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:备份节点