温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mongodb之副本集的实现及故障自动转移

发布时间:2020-06-26 12:28:46 来源:网络 阅读:803 作者:落轩易离落 栏目:MongoDB数据库

Mongodb之副本集的实现----repliSet

准备三个节点:

RS1192.168.1.15527017

RS2192.168.1.11:27017

node1:192.168.1.112:27017

1、时间同步

ntpdate192.168.1.11

2、安装mongodb服务

rpm包地址:https://repo.mongodb.org/yum/redhat

yum –y localinstall*.rpm

mkdir –p/mongodb/data

chown –Rmongod:mongod /mongodb/data

usermod –d/mongodb/data mongod

3、修改配置文件

vim /etc/mongod.conf

#dbPath:/var/lib/mongo

  dbPath: /mongodb/data

replication:启用这项

  replSetName: testrs:给副本集定义一个名称

然后同步到另外两个节点

4、启动mongod服务

service mongodstart

选择192.168.1.155:27017这个服务登录

mongo

help---àrs.help()---àrs.initiate()用此命令进行初始化

发现报错如下:

mongodb之副本集的实现及故障自动转移

该主机映射不到节点上,进行谷歌搜索查到原因,由于配置文件bindip绑定的是127.0.0.1上,而/etc/hosts文件中127.0.0.1hostname---RS1并没有指定关联于是做此修改

127.0.0.1   localhost localhost.localdomain localhost4localhost4.localdomain4 RS1

在最后加上hostname,于是重新初始化,发现果然解决问题

5、由于上步骤已经把服务都启动起来了,于是继续在192.168.1.155上进行添加主机

Rs.add(‘192.168.1.11:27017’)--à发现又报错,如下图

mongodb之副本集的实现及故障自动转移

可以看出连接不上另外的节点,在155节点上进行尝试登陆

mongo  --host 192.168.1.11 –port 27017发现还是登陆不上,翻看很多文章,描述的都不是很详细,于是一个群友解决了疑惑,由于配置文件中bindip并没有进行修改只是监听的127.0.0.1,在一个系统中配置三个实例mongodb这种方式可以不用修改,但是现在是三台不同的系统ip,于是得进行修改

net:

  port: 27017

  bindIp: 192.168.1.155将其他两个节点也进行修改,添加为本机ip,而不是127.0.0.1,(当然bindip也可以绑定多个ip:192.168.1.155 127.0.0.1)然后在hosts文件中加入:

192.168.1.155  RS1

192.168.1.11  RS2

192.168.1.112  node1

完成以上步骤之后,然后进行再次添加

rs.add(‘192.168.1.11:27017’)-à

testrs:PRIMARY>rs.add('192.168.1.11:27017')

{ "ok": 1 }:没有报错于是成功

然后再次添加下个节点

rs.add(‘192.168.1.112:27017’)

rs.status():进行查看节点间的状态

testrs:PRIMARY>rs.status()

{

      "set" : "testrs",

      "date" : ISODate("2016-07-26T04:21:32.729Z"),

      "myState" : 1,

      "term" : NumberLong(3),

      "heartbeatIntervalMillis" :NumberLong(2000),

      "members" : [

           {

                 "_id" : 0,

                 "name" :"RS1:27017",

                 "health" : 1,

                 "state" : 1,

                 "stateStr" :"PRIMARY",:主节点

                 "uptime" : 346,

                 "optime" : {

                      "ts" :Timestamp(1469506890, 1),

                      "t" :NumberLong(3)

                 },

                 "optimeDate" :ISODate("2016-07-26T04:21:30Z"),

                 "electionTime" :Timestamp(1469506554, 1),

                 "electionDate" :ISODate("2016-07-26T04:15:54Z"),

                 "configVersion" : 3,

                 "self" : true

           },

           {

                 "_id" : 1,

                 "name" :"192.168.1.11:27017",

                 "health" : 1,

                 "state" : 2,

                 "stateStr" :"SECONDARY",:从节点

                 "uptime" : 156,

                 "optime" : {

                      "ts" :Timestamp(1469506735, 1),

                      "t" :NumberLong(3)

                 },

                 "optimeDate" :ISODate("2016-07-26T04:18:55Z"),

                 "lastHeartbeat" :ISODate("2016-07-26T04:21:30.870Z"),

                 "lastHeartbeatRecv" :ISODate("2016-07-26T04:21:30.974Z"),

                 "pingMs" :NumberLong(1),

                 "configVersion" : 2

           },

           {

                 "_id" : 2,

                 "name" : "192.168.1.112:27017",

                 "health" : 1,

                 "state" : 0,

                 "stateStr" :"STARTUP",:由于是刚刚添加,正在开启

                 "uptime" : 1,

                 "optime" : {

                      "ts" :Timestamp(0, 0),

                      "t" :NumberLong(-1)

                 },

                 "optimeDate" :ISODate("1970-01-01T00:00:00Z"),

                 "lastHeartbeat" :ISODate("2016-07-26T04:21:31.165Z"),

                 "lastHeartbeatRecv" :ISODate("2016-07-26T04:21:32.498Z"),

                 "pingMs" :NumberLong(193),

                 "configVersion" : -2

           }

      ],

      "ok" : 1

}

至此,副本集已然完成,接下来就是验证主从是否同步?

6、在192.168.1.155:27017主节点上一次进行如下操作:

testrs:PRIMARY>use huangdb:切换到库

switched to dbhuangdb

testrs:PRIMARY>show collections

testcoll

testrs:PRIMARY>db.testcoll.find()

{"_id" : ObjectId("5792d2a3a4769176f5babaaa"),"Name" : "huang", "Age" : 24, "Gender": "F" }

testrs:PRIMARY>db.testcoll.insert({Name:"xiaoming",Age:23,url:"www.baidu.com"})

WriteResult({"nInserted" : 1 }):添加一条文档记录

testrs:PRIMARY>db.testcoll.find():查看文档记录

{"_id" : ObjectId("5792d2a3a4769176f5babaaa"),"Name" : "huang", "Age" : 24, "Gender": "F" }

{"_id" : ObjectId("5796e7333f3249e9b0b44ded"),"Name" : "xiaoming", "Age" : 23, "url": "www.baidu.com" }

 

然后在192.168.1.11:27017上这个系统进行登录验证

mongo –host 192.168.1.11–port 27017

use huangdb

show collections发现报错如下:

mongodb之副本集的实现及故障自动转移

根据报错提示进行

rs.slaveOk()

然后进行查看

Db.testcoll.find()于是可以看见集合的内容记录,至此主从同步ok

7、接下来就是观察节点间能否进行failover故障转移

192.168.1.155mongod服务停止

[root@RS1 ~]#service mongod stop

Stoppingmongod:                                           [确定]

于是在192.168.1.11上查看副本集状态

testrs:SECONDARY>rs.status()

{

      "set" : "testrs",

      "date" : ISODate("2016-07-26T05:06:39.686Z"),

      "myState" : 2,

      "term" : NumberLong(4),

      "syncingTo" :"192.168.1.112:27017",

      "heartbeatIntervalMillis" :NumberLong(2000),

      "members" : [

           {

                 "_id" : 0,

                 "name" :"RS1:27017",

                 "health" : 0,

                 "state" : 8,

                 "stateStr" :"(not reachable/healthy)",

                 "uptime" : 0,

                 "optime" : {

                      "ts" :Timestamp(0, 0),

                      "t" :NumberLong(-1)

                 },

                 "optimeDate" :ISODate("1970-01-01T00:00:00Z"),

                 "lastHeartbeat" :ISODate("2016-07-26T05:06:39.641Z"),

                 "lastHeartbeatRecv" :ISODate("2016-07-26T05:05:58.844Z"),

                 "pingMs" :NumberLong(2),

                 "lastHeartbeatMessage": "Connection refused",

                 "configVersion" : -1

           },

           {

                 "_id" : 1,

                 "name" :"192.168.1.11:27017",

                 "health" : 1,

                 "state" : 2,

                 "stateStr" : "SECONDARY",

                 "uptime" : 3019,

                 "optime" : {

                      "ts" :Timestamp(1469509568, 2),

                      "t" :NumberLong(4)

                 },

                 "optimeDate" :ISODate("2016-07-26T05:06:08Z"),

                 "syncingTo" :"192.168.1.112:27017",

                 "configVersion" : 3,

                 "self" : true

           },

           {

                 "_id" : 2,

                 "name" :"192.168.1.112:27017",

                 "health" : 1,

                 "state" : 1,

                 "stateStr" :"PRIMARY",

                 "uptime" : 2708,

                 "optime" : {

                      "ts" :Timestamp(1469509568, 2),

                      "t" :NumberLong(4)

                 },

                 "optimeDate" :ISODate("2016-07-26T05:06:08Z"),

                 "lastHeartbeat" :ISODate("2016-07-26T05:06:38.433Z"),

                 "lastHeartbeatRecv" :ISODate("2016-07-26T05:06:38.907Z"),

                 "pingMs" :NumberLong(1),

                 "electionTime" :Timestamp(1469509568, 1),

                 "electionDate" :ISODate("2016-07-26T05:06:08Z"),

                 "configVersion" : 3

           }

      ],

      "ok" : 1

}

根据状态可以看出192.168.1.155状态已经不健康了,而192.168.1.112从从节点劲升为了主节点,于是故障自动转移成功


附件:http://down.51cto.com/data/2367981
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI