亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB Replica Set 副本集

發布時間:2020-06-29 23:02:56 來源:網絡 閱讀:845 作者:Darren_Chen 欄目:關系型數據庫

1、簡介

MongoDB復制是將數據同步在多個服務器的過程,類似MySQL Replication是一種異步復制;

復制提供了數據的冗余備份,并在多個服務器上存儲數據副本,提高了數據的可用性, 并可以保證數據的安全性。


2、MongoDB復制特點

(1) mongodb的節點有三種角色:primary,seconfary,arbiter, 

primary負責處理客戶端請求,讀寫數據;

secondary通過oplog復制主節點上的數據,也可讀數據; 

arbiter節點不存儲數據,僅投票功能,所以不需要單獨的硬件配置;

(2)如果primary不可以用,secondary會提升為新的primary; 由參數electionTimeoutMillis=10s控制


3、實驗環境

MongoDB-3.0.6版本

一臺物理機器上三個MongoDB實例,分別占用三個節點

172.16.101.54  27017

172.16.101.54  27018

172.16.101.54  27019


4、創建數據和日志目錄

[root@sht-sgmhadoopcm-01 mongodb]# mkdir -p /usr/local/mongodb/data/{db27017,db27018,db27019}

[root@sht-sgmhadoopcm-01 mongodb]# mkdir -p /usr/local/mongodb/log


5、配置文件/etc/mongod.conf

三個節點分別創建配置文件,文件內容區別是節點不同,其他都相同,參數replSetname用來指定副本集名稱為rs0

[root@sht-sgmhadoopcm-01 mongodb]# ll /etc/mongod2701*.conf

-rw-r--r-- 1 root root 405 Nov  2 20:21 /etc/mongod27017.conf

-rw-r--r-- 1 root root 405 Nov  2 20:13 /etc/mongod27018.conf

-rw-r--r-- 1 root root 405 Nov  2 20:13 /etc/mongod27019.conf

mongodb配置文件每個冒號后面要有一個空格,否則會報錯

[root@sht-sgmhadoopcm-01 mongodb]# cat /etc/mongod27017.conf
systemLog:
   destination: file
   path: "/usr/local/mongodb/log/mongod27017.log"
   logAppend: true
storage:
   dbPath: /usr/local/mongodb/data/db27017
   journal:
      enabled: true
processManagement:
   fork: true
   pidFilePath: /usr/local/mongodb/data/db27017/mongod27017.pid
net:
   port: 27017
   bindIp: 0.0.0.0
setParameter:
   enableLocalhostAuthBypass: false
replication:
  replSetName: rs0


6、啟動MongoDB

分別啟動三個節點的MongoDB服務,并指定配置文件

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27018.conf 

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27019.conf 


7、初始化副本集

登錄27017節點

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo  172.16.101.54:27017/admin

> rs.initiate()

{

    "info2" : "no configuration explicitly specified -- making one",

    "me" : "sht-sgmhadoopcm-01:27017",

    "ok" : 1

}


添加成員到副本集,priority大的節點提成為主的可能性就大

rs0:PRIMARY> rs.add('172.16.101.54:27018')

{ "ok" : 1 }

rs0:PRIMARY> rs.add('172.16.101.54:27019',priority:5)

{ "ok" : 1 }


查看副本集配置

rs0:PRIMARY> rs.conf()
{
    "_id" : "rs0",
    "version" : 3,
    "members" : [
        {
            "_id" : 0,
            "host" : "sht-sgmhadoopcm-01:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : 0,
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "172.16.101.54:27018",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : 0,
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "172.16.101.54:27019",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : 0,
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatTimeoutSecs" : 10,
        "getLastErrorModes" : {
            
        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        }
    }
}


查看各個角色狀態

rs0:PRIMARY> rs.status()
{
    "set" : "rs0",
    "date" : ISODate("2018-11-02T12:27:44.570Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "sht-sgmhadoopcm-01:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 626,
            "optime" : Timestamp(1541161589, 1),
            "optimeDate" : ISODate("2018-11-02T12:26:29Z"),
            "electionTime" : Timestamp(1541161465, 2),
            "electionDate" : ISODate("2018-11-02T12:24:25Z"),
            "configVersion" : 3,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "172.16.101.54:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 161,
            "optime" : Timestamp(1541161589, 1),
            "optimeDate" : ISODate("2018-11-02T12:26:29Z"),
            "lastHeartbeat" : ISODate("2018-11-02T12:27:43.430Z"),
            "lastHeartbeatRecv" : ISODate("2018-11-02T12:27:43.311Z"),
            "pingMs" : 0,
            "syncingTo" : "sht-sgmhadoopcm-01:27017",
            "configVersion" : 3
        },
        {
            "_id" : 2,
            "name" : "172.16.101.54:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 75,
            "optime" : Timestamp(1541161589, 1),
            "optimeDate" : ISODate("2018-11-02T12:26:29Z"),
            "lastHeartbeat" : ISODate("2018-11-02T12:27:43.430Z"),
            "lastHeartbeatRecv" : ISODate("2018-11-02T12:27:43.430Z"),
            "pingMs" : 0,
            "lastHeartbeatMessage" : "could not find member to sync from",
            "configVersion" : 3
        }
    ],
    "ok" : 1
}


8、數據同步測試

向27017節點插入數據

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27017/admin

rs0:PRIMARY> db.user.insert({"username":"test","age":12,"sex":"m"})

WriteResult({ "nInserted" : 1 })

rs0:PRIMARY> db.user.find()

{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }


數據成功的同步到27018和27019節點

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo  172.16.101.54:27018/admin

rs0:SECONDARY> db.getMongo().setReadPref('secondaryPreferred')

rs0:SECONDARY> db.user.find()

{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }


[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo  172.16.101.54:27019/admin

rs0:SECONDARY> db.getMongo().setReadPref('secondaryPreferred')

rs0:SECONDARY> db.user.find()

{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }


9、AutoFailover測試

kill當前的primary27017節點

[root@sht-sgmhadoopcm-01 mongodb]# ps -ef|grep mongod

root      5703     1  0 20:17 ?        00:00:52 bin/mongod --config /etc/mongod27017.conf

root      5747     1  0 20:17 ?        00:00:51 bin/mongod --config /etc/mongod27018.conf

root      5979     1  0 20:20 ?        00:00:52 bin/mongod --config /etc/mongod27019.conf


[root@sht-sgmhadoopcm-01 mongodb]# kill -9 5703

可以看到27019變成primary,此時27017不可用

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo  172.16.101.54:27018/admin

rs0:SECONDARY> rs.status()
{
    "set" : "rs0",
    "date" : ISODate("2018-11-02T15:10:18.746Z"),
    "myState" : 2,
    "members" : [
        {
            "_id" : 0,
            "name" : "sht-sgmhadoopcm-01:27017",
            "health" : 0,
            "state" : 8,
            "stateStr" : "(not reachable/healthy)",
            "uptime" : 0,
            "optime" : Timestamp(0, 0),
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2018-11-02T15:10:18.631Z"),
            "lastHeartbeatRecv" : ISODate("2018-11-02T15:09:28.583Z"),
            "pingMs" : 0,
            "lastHeartbeatMessage" : "Failed attempt to connect to sht-sgmhadoopcm-01:27017; couldn't connect to server sht-sgmhadoopcm-01:27017 (172.16.101.54), connection attempt failed",
            "configVersion" : -1
        },
        {
            "_id" : 1,
            "name" : "172.16.101.54:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 10349,
            "optime" : Timestamp(1541170688, 2),
            "optimeDate" : ISODate("2018-11-02T14:58:08Z"),
            "configVersion" : 3,
            "self" : true
        },
        {
            "_id" : 2,
            "name" : "172.16.101.54:27019",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 9827,
            "optime" : Timestamp(1541170688, 2),
            "optimeDate" : ISODate("2018-11-02T14:58:08Z"),
            "lastHeartbeat" : ISODate("2018-11-02T15:10:18.611Z"),
            "lastHeartbeatRecv" : ISODate("2018-11-02T15:10:18.592Z"),
            "pingMs" : 0,
            "electionTime" : Timestamp(1541171370, 1),
            "electionDate" : ISODate("2018-11-02T15:09:30Z"),
            "configVersion" : 3
        }
    ],
    "ok" : 1
}


重新啟動27017節點,會自動變成了27019的備庫,說明auto failover成功了

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf

rs0:PRIMARY> rs.status()
{
    "set" : "rs0",
    "date" : ISODate("2018-11-02T15:13:27.035Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "sht-sgmhadoopcm-01:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 42,
            "optime" : Timestamp(1541170688, 2),
            "optimeDate" : ISODate("2018-11-02T14:58:08Z"),
            "lastHeartbeat" : ISODate("2018-11-02T15:13:26.764Z"),
            "lastHeartbeatRecv" : ISODate("2018-11-02T15:13:25.055Z"),
            "pingMs" : 0,
            "configVersion" : 3
        },
        {
            "_id" : 1,
            "name" : "172.16.101.54:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 10017,
            "optime" : Timestamp(1541170688, 2),
            "optimeDate" : ISODate("2018-11-02T14:58:08Z"),
            "lastHeartbeat" : ISODate("2018-11-02T15:13:26.662Z"),
            "lastHeartbeatRecv" : ISODate("2018-11-02T15:13:26.678Z"),
            "pingMs" : 0,
            "lastHeartbeatMessage" : "could not find member to sync from",
            "configVersion" : 3
        },
        {
            "_id" : 2,
            "name" : "172.16.101.54:27019",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 10349,
            "optime" : Timestamp(1541170688, 2),
            "optimeDate" : ISODate("2018-11-02T14:58:08Z"),
            "electionTime" : Timestamp(1541171370, 1),
            "electionDate" : ISODate("2018-11-02T15:09:30Z"),
            "configVersion" : 3,
            "self" : true
        }
    ],
    "ok" : 1
}


FAQ

[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf &

[1] 4885

[root@sht-sgmhadoopcm-01 mongodb]# Error parsing YAML config file: yaml-cpp: error at line 8, column 11: illegal map value

try 'bin/mongod --help' for more information

[1]+  Exit 2                  bin/mongod --config /etc/mongod27017.conf


原因是配置文件/etc/mongod.conf冒號后面沒有跟空格。


參考鏈接

MongoDB Replication

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

竹北市| 武功县| 忻城县| 大洼县| 桃园市| 靖远县| 固原市| 社会| 台北市| 磐安县| 黄浦区| 大埔区| 仪陇县| 兴国县| 承德市| 利津县| 彝良县| 布尔津县| 舞阳县| 华阴市| 集贤县| 南靖县| 金门县| 永春县| 金华市| 英山县| 上犹县| 友谊县| 靖边县| 伊宁县| 广河县| 永仁县| 沈丘县| 万安县| 巩留县| 四川省| 高淳县| 天峻县| 沭阳县| 高雄县| 方正县|