成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

mongodb之副本集的實現(xiàn)及故障自動轉(zhuǎn)移

MongoDB之副本集的實現(xiàn)----repliSet

鐵鋒網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),鐵鋒網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為鐵鋒上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的鐵鋒做網(wǎng)站的公司定做!

準(zhǔn)備三個節(jié)點:

RS1:192.168.1.155:27017

RS2:192.168.1.11:27017

node1:192.168.1.112:27017

1、時間同步

ntpdate192.168.1.11

2、安裝mongodb服務(wù)

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:給副本集定義一個名稱

然后同步到另外兩個節(jié)點

4、啟動mongod服務(wù)

service mongodstart

選擇192.168.1.155:27017這個服務(wù)登錄

mongo

help---àrs.help()---àrs.initiate()用此命令進(jìn)行初始化

發(fā)現(xiàn)報錯如下:

mongodb之副本集的實現(xiàn)及故障自動轉(zhuǎn)移

該主機映射不到節(jié)點上,進(jìn)行谷歌搜索查到原因,由于配置文件bindip綁定的是127.0.0.1上,而/etc/hosts文件中127.0.0.1余hostname---》RS1并沒有指定關(guān)聯(lián)于是做此修改

127.0.0.1   localhost localhost.localdomain localhost4localhost4.localdomain4 RS1

在最后加上hostname,于是重新初始化,發(fā)現(xiàn)果然解決問題

5、由于上步驟已經(jīng)把服務(wù)都啟動起來了,于是繼續(xù)在192.168.1.155上進(jìn)行添加主機

Rs.add(‘192.168.1.11:27017’)--à發(fā)現(xiàn)又報錯,如下圖

mongodb之副本集的實現(xiàn)及故障自動轉(zhuǎn)移

可以看出連接不上另外的節(jié)點,在155節(jié)點上進(jìn)行嘗試登陸

mongo  --host 192.168.1.11 –port 27017發(fā)現(xiàn)還是登陸不上,翻看很多文章,描述的都不是很詳細(xì),于是一個群友解決了疑惑,由于配置文件中bindip并沒有進(jìn)行修改只是監(jiān)聽的127.0.0.1,在一個系統(tǒng)中配置三個實例mongodb這種方式可以不用修改,但是現(xiàn)在是三臺不同的系統(tǒng)ip,于是得進(jìn)行修改

net:

  port: 27017

  bindIp: 192.168.1.155將其他兩個節(jié)點也進(jìn)行修改,添加為本機ip,而不是127.0.0.1,(當(dāng)然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

完成以上步驟之后,然后進(jìn)行再次添加

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

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

{ "ok": 1 }:沒有報錯于是成功

然后再次添加下個節(jié)點

rs.add(‘192.168.1.112:27017’)

rs.status():進(jìn)行查看節(jié)點間的狀態(tài)

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",:主節(jié)點

                 "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",:從節(jié)點

                 "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主節(jié)點上一次進(jìn)行如下操作:

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上這個系統(tǒng)進(jìn)行登錄驗證

mongo –host 192.168.1.11–port 27017

use huangdb

show collections發(fā)現(xiàn)報錯如下:

mongodb之副本集的實現(xiàn)及故障自動轉(zhuǎn)移

根據(jù)報錯提示進(jìn)行

rs.slaveOk()

然后進(jìn)行查看

Db.testcoll.find()于是可以看見集合的內(nèi)容記錄,至此主從同步ok

7、接下來就是觀察節(jié)點間能否進(jìn)行failover故障轉(zhuǎn)移

將192.168.1.155的mongod服務(wù)停止

[root@RS1 ~]#service mongod stop

Stoppingmongod:                                           [確定]

于是在192.168.1.11上查看副本集狀態(tài)

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

}

根據(jù)狀態(tài)可以看出192.168.1.155狀態(tài)已經(jīng)不健康了,而192.168.1.112從從節(jié)點勁升為了主節(jié)點,于是故障自動轉(zhuǎn)移成功

附件:http://down.51cto.com/data/2367981

標(biāo)題名稱:mongodb之副本集的實現(xiàn)及故障自動轉(zhuǎn)移
網(wǎng)頁地址:http://jinyejixie.com/article28/gpegcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站營銷、手機網(wǎng)站建設(shè)、建站公司做網(wǎng)站、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
玛纳斯县| 耒阳市| 彭山县| 双辽市| 广汉市| 同仁县| 连南| 建湖县| 建湖县| 安达市| 霍州市| 石楼县| 福海县| 文山县| 德兴市| 高阳县| 璧山县| 乌拉特前旗| 宝鸡市| 桐柏县| 油尖旺区| 班玛县| 抚州市| 台南市| 吴川市| 八宿县| 中阳县| 临清市| 临澧县| 鹤山市| 肥乡县| 凤阳县| 玉山县| 宜春市| 凤凰县| 宜章县| 电白县| 绩溪县| 航空| 刚察县| 三台县|