您好,登錄后才能下訂單哦!
由三個節(jié)點組成的 復(fù)制集 為網(wǎng)絡(luò)故障或是其他的系統(tǒng)故障提供了足夠的冗余。該復(fù)制集也有足夠的分布式讀操作的能力。復(fù)制集應(yīng)該保持奇數(shù)個節(jié)點,這也就保證了 選舉 可以正常的進行
用3臺已有的 mongodb mongodb-program docutils literal">mongod 實例來部署一個由三個節(jié)點組成的 復(fù)制集
192.168.1.3 hadoop1.abc.com hadoop1
192.168.1.4 hadoop2.abc.com hadoop2
192.168.1.5 hadoop3.abc.com hadoop3
在生產(chǎn)環(huán)境中,我們應(yīng)該將每個節(jié)點部署在獨立的機器上,并使用標(biāo)準(zhǔn)的MongoDB端口 27017 。使用 bind_ip 參數(shù)來限制訪問MongoDB的應(yīng)用程序的地址。
若使用了異地分布式架構(gòu)的復(fù)制集,請確保多數(shù) mongod 實例節(jié)點位于主數(shù)據(jù)中心中。
確保各個節(jié)點之間可以正常通訊,且各個客戶端都處于安全的可信的網(wǎng)絡(luò)環(huán)境中??梢钥紤]以下事項:
建立虛擬的專用網(wǎng)絡(luò)。確保各個節(jié)點之間的流量是在本地網(wǎng)絡(luò)范圍內(nèi)路由的。(Establish a virtual private network. Ensure that your network topology routes all traffic between members within a single site over the local area network.)
配置連接限制來防止未知的客戶端連接到復(fù)制集。
配置網(wǎng)絡(luò)設(shè)置和防火墻規(guī)則來對將MongoDB的端口僅開放給應(yīng)用程序,來讓應(yīng)用程序發(fā)的進出數(shù)據(jù)包可以與MongoDB正常交流。
最后請確保復(fù)制集各節(jié)點可以互相通過DNS或是主機名解析。我們需要配置DNS域名或是設(shè)置 /etc/hosts 文件來配置。
這里實驗,是關(guān)閉防火墻,并把selinux設(shè)置成setenforce 0
系統(tǒng)環(huán)境如下:
[root@hadoop2 data]# cat /etc/issue CentOS release 6.5 (Final) Kernel \r on an \m [root@hadoop2 data]# uname -r 2.6.32-431.el6.x86_64
配置文件選項:
port = 27017
bind_ip =
dbpath =
fore = true
replSet = testrs0
rest = true
1、建立每個節(jié)點都建立據(jù)據(jù)目錄
[root@hadoop1 ~]# mkidr -pv /mongodb/data/ [root@hadoop1 ~]# chown mongod.mongod /mongodb/data/
2
在每個節(jié)點上啟動 mongod 并通過制定 replSet 參數(shù)來指定其復(fù)制集名,并可以指定其他需要的參數(shù)
[root@hadoop1 ~]# vim /etc/mongod.conf //添加如下 #Replica Set replSet = testrs0 或者 [root@hadoop1 ~]# mongod --replSet "testrs0"
確保每個節(jié)點都有相同復(fù)制集名稱
[root@hadoop1 ~]# scp /etc/mongod.conf root@hadoop2:/etc/;scp /etc/mongod.conf root@hadoop2:/etc/;
注意了,如果解決啟動mongod 時,出現(xiàn)addr already in use錯誤,原因啟動端口被占用
[root@hadoop1 data]# mongod 2015-07-29T19:15:51.728+0800 E NETWORK [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017 2015-07-29T19:15:51.728+0800 E NETWORK [initandlisten] addr already in use 2015-07-29T19:15:51.729+0800 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating 2015-07-29T19:15:51.729+0800 I CONTROL [initandlisten] dbexit: rc: 100
把端口找出來,kill掉
[root@hadoop1 ~]# netstat -anp|more unix 2 [ ACC ] STREAM LISTENING 15588 2174/mongod /tmp/mongodb-27017.sock
[root@hadoop1 ~]# kill 2174 [root@hadoop1 ~]# /etc/init.d/mongod start Starting mongod: [確定]
[root@hadoop1 ~]# mongo
//使用rs.initiate()命令,MongoDB將初始化一個由當(dāng)前節(jié)點構(gòu)成、擁有默認(rèn)配置的復(fù)制集。
> rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "hadoop1.abc.com:27017", "info" : "try querying local.system.replset to see current configuration", "ok" : 0, "errmsg" : "already initialized", "code" : 23 } > rs.status() { "state" : 10, "stateStr" : "REMOVED", "uptime" : 38, "optime" : Timestamp(1438168698, 1), "optimeDate" : ISODate("2015-07-29T11:18:18Z"), "ok" : 0, "errmsg" : "Our replica set config is invalid or we are not a member of it", "code" : 93 }
查看日志 文件
2015-07-29T20:00:45.433+0800 W NETWORK [ReplicationExecutor] Failed to connect to 192.168.1.3:27017, reason: errno:111 Connection refused
2015-07-29T20:00:45.433+0800 W REPL [ReplicationExecutor] Locally stored replica set configuration does not have a valid entry for the current node; waiting for reconfig or remote heartbeat; Got "NodeNotFound No host described in new configuration 1 for replica set testrs0 maps to this node" while validating { _id: "testrs0", version: 1, members: [ { _id: 0, host: "hadoop1.abc.com:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatTimeoutSecs: 10, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }
2015-07-29T20:00:45.433+0800 I REPL [ReplicationExecutor] New replica set config in use: { _id: "testrs0", version: 1, members: [ { _id: 0, host: "hadoop1.abc.com:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatTimeoutSecs: 10, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }
2015-07-29T20:00:45.433+0800 I REPL [ReplicationExecutor] This node is not a member of the config
2015-07-29T20:00:45.433+0800 I REPL [ReplicationExecutor] transition to REMOVED
2015-07-29T20:00:45.433+0800 I REPL [ReplicationExecutor] Starting replication applier threads
2015-07-29T20:00:49.067+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58852 #1 (1 connection now open)
2015-07-29T20:01:17.436+0800 I COMMAND [conn1] replSet info initiate : no configuration specified. Using a default configuration for the set
2015-07-29T20:01:17.436+0800 I COMMAND [conn1] replSet created this configuration for initiation : { _id: "testrs0", version: 1, members: [ { _id: 0, host: "hadoop1.abc.com:27017" } ] }
2015-07-29T20:01:17.436+0800 I REPL [conn1] replSetInitiate admin command received from client
[root@hadoop1 ~]# service mongod stop Stopping mongod: [確定] You have new mail in /var/spool/mail/root [root@hadoop1 ~]# vim /etc/mongod.conf #開啟了 bind 127.0.0.1 把本地限制訪問了 #bind 127.0.0.1 [root@hadoop1 data]# service mongod start Starting mongod: [確定] > rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "hadoop1.abc.com:27017", "info" : "try querying local.system.replset to see current configuration", "ok" : 0, "errmsg" : "already initialized", "code" : 23 testrs0:PRIMARY> rs.status() { "set" : "testrs0", "date" : ISODate("2015-07-29T12:13:27.839Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "hadoop1.abc.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 232, "optime" : Timestamp(1438168698, 1), "optimeDate" : ISODate("2015-07-29T11:18:18Z"), "electionTime" : Timestamp(1438171776, 1), "electionDate" : ISODate("2015-07-29T12:09:36Z"), "configVersion" : 1, "self" : true } ], "ok" : 1 }
5、將其他的節(jié)點加入復(fù)制集。
通過 rs.add() 來將剩下的節(jié)點加入復(fù)制集。
testrs0:PRIMARY> rs.add("192.168.1.4:27017") { "ok" : 1 } testrs0:PRIMARY> rs.status() { "set" : "testrs0", "date" : ISODate("2015-07-30T02:09:45.871Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "hadoop1.abc.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 50410, "optime" : Timestamp(1438222179, 1), "optimeDate" : ISODate("2015-07-30T02:09:39Z"), "electionTime" : Timestamp(1438171776, 1), "electionDate" : ISODate("2015-07-29T12:09:36Z"), "configVersion" : 2, "self" : true }, { "_id" : 1, "name" : "192.168.1.4:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 6, "optime" : Timestamp(1438222179, 1), "optimeDate" : ISODate("2015-07-30T02:09:39Z"), "lastHeartbeat" : ISODate("2015-07-30T02:09:45.081Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T02:09:45.183Z"), "pingMs" : 1, "configVersion" : 2 } ], "ok" : 1 }
testrs0:PRIMARY> rs.add("192.168.1.5:27017") { "ok" : 1 } testrs0:PRIMARY> rs.status() { "set" : "testrs0", "date" : ISODate("2015-07-30T02:28:52.382Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "hadoop1.abc.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 51557, "optime" : Timestamp(1438223187, 1), "optimeDate" : ISODate("2015-07-30T02:26:27Z"), "electionTime" : Timestamp(1438171776, 1), "electionDate" : ISODate("2015-07-29T12:09:36Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "192.168.1.4:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1153, "optime" : Timestamp(1438223187, 1), "optimeDate" : ISODate("2015-07-30T02:26:27Z"), "lastHeartbeat" : ISODate("2015-07-30T02:28:52.337Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T02:28:50.438Z"), "pingMs" : 0, "syncingTo" : "hadoop1.abc.com:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "192.168.1.5:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 13, "optime" : Timestamp(1438223187, 1), "optimeDate" : ISODate("2015-07-30T02:26:27Z"), "lastHeartbeat" : ISODate("2015-07-30T02:28:50.437Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T02:28:50.478Z"), "pingMs" : 1, "configVersion" : 3 } ], "ok" : 1 } testrs0:PRIMARY> rs.isMaster() { "setName" : "testrs0", "setVersion" : 3, "ismaster" : true, "secondary" : false, "hosts" : [ "hadoop1.abc.com:27017", "192.168.1.4:27017", "192.168.1.5:27017" ], "primary" : "hadoop1.abc.com:27017", "me" : "hadoop1.abc.com:27017", "electionId" : ObjectId("55b8c280790a6c1f967f6147"), "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-07-30T02:30:18Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1 }
其他節(jié)點hadoop2驗證一下
testrs0:SECONDARY> rs.isMaster() { "setName" : "testrs0", "setVersion" : 3, "ismaster" : false, "secondary" : true, "hosts" : [ "hadoop1.abc.com:27017", "192.168.1.4:27017", "192.168.1.5:27017" ], "primary" : "hadoop1.abc.com:27017", "me" : "192.168.1.4:27017", "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-07-30T02:32:43.546Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1 }
6、在主節(jié)點上創(chuàng)建數(shù)據(jù),從節(jié)點是否獲取到
testrs0:PRIMARY> use testdb switched to db testdb testrs0:PRIMARY> db.testcoll.insert({Name: "test",Age: 50,Gender: "F"}) WriteResult({ "nInserted" : 1 }) testrs0:PRIMARY> db.testcoll.find() { "_id" : ObjectId("55b9945b92ad0ab98483695e"), "Name" : "test", "Age" : 60, "Gender" : "F" } { "_id" : ObjectId("55b994ce92ad0ab98483695f"), "Name" : "test", "Age" : 50, "Gender" : "F" }
在從節(jié)點上查詢,是不可以直接查詢,要使用一個命令rs.slave()把自己提升為從節(jié)點
testrs0:SECONDARY> rs.slaveOk() testrs0:SECONDARY> use testdb; switched to db testdb testrs0:SECONDARY> db.testcoll.find() { "_id" : ObjectId("55b9945b92ad0ab98483695e"), "Name" : "test", "Age" : 60, "Gender" : "F" } { "_id" : ObjectId("55b994ce92ad0ab98483695f"), "Name" : "test", "Age" : 50, "Gender" : "F" }
7、讓主節(jié)點hadoop1掛掉
[root@hadoop1 data]# service mongod stop Stopping mongod:
在hadoop3驗證一下
testrs0:PRIMARY> rs.status() { "set" : "testrs0", "date" : ISODate("2015-07-30T04:36:19.677Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "hadoop1.abc.com:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2015-07-30T04:36:19.503Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T04:33:18.147Z"), "pingMs" : 0, "lastHeartbeatMessage" : "Failed attempt to connect to hadoop1.abc.com:27017; couldn't connect to server hadoop1.abc.com:27017 (192.168.1.3), connection attempt failed", "configVersion" : -1 }, { "_id" : 1, "name" : "192.168.1.4:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 7661, "optime" : Timestamp(1438225614, 1), "optimeDate" : ISODate("2015-07-30T03:06:54Z"), "lastHeartbeat" : ISODate("2015-07-30T04:36:19.335Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T04:36:19.348Z"), "pingMs" : 0, "configVersion" : 3 }, { "_id" : 2, "name" : "192.168.1.5:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 7664, "optime" : Timestamp(1438225614, 1), "optimeDate" : ISODate("2015-07-30T03:06:54Z"), "electionTime" : Timestamp(1438230801, 1), "electionDate" : ISODate("2015-07-30T04:33:21Z"), "configVersion" : 3, "self" : true } ], "ok" : 1 } testrs0:PRIMARY> db.isMaster() { "setName" : "testrs0", "setVersion" : 3, "ismaster" : true, "secondary" : false, "hosts" : [ "hadoop1.abc.com:27017", "192.168.1.4:27017", "192.168.1.5:27017" ], "primary" : "192.168.1.5:27017", "me" : "192.168.1.5:27017", "electionId" : ObjectId("55b9a91100e446910c89a0a3"), "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-07-30T04:37:33.090Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1 } testrs0:PRIMARY> db.testcoll.insert ({Name: "tom",Age: 45,Gender: "G"}) WriteResult({ "nInserted" : 1 })
回到hadoop2查看一下數(shù)據(jù)
testrs0:SECONDARY> db.testcoll.find() { "_id" : ObjectId("55b9942d92ad0ab98483695c"), "Name" : "test", "Age" : 60, "Gender" : "F" } { "_id" : ObjectId("55b9944892ad0ab98483695d"), "Name" : "test", "Age" : 60, "Gender" : "F" } { "_id" : ObjectId("55b9945b92ad0ab98483695e"), "Name" : "test", "Age" : 60, "Gender" : "F" } { "_id" : ObjectId("55b994ce92ad0ab98483695f"), "Name" : "test", "Age" : 50, "Gender" : "F" } { "_id" : ObjectId("55b9aa714b575261aff42f25"), "Name" : "tom", "Age" : 45, "Gender" : "G" }
讓hadoop1上線
[root@hadoop1 data]# service mongod start Starting mongod:
再驗證一下狀態(tài),但不能奪回主動權(quán),除非讓它優(yōu)先級高一點。
這種場景下,它自動成為從的了
testrs0:SECONDARY> rs.status() { "set" : "testrs0", "date" : ISODate("2015-07-30T04:44:16.534Z"), "myState" : 2, "syncingTo" : "192.168.1.4:27017", "members" : [ { "_id" : 0, "name" : "hadoop1.abc.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 165, "optime" : Timestamp(1438231153, 1), "optimeDate" : ISODate("2015-07-30T04:39:13Z"), "syncingTo" : "192.168.1.4:27017", "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "192.168.1.4:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 164, "optime" : Timestamp(1438231153, 1), "optimeDate" : ISODate("2015-07-30T04:39:13Z"), "lastHeartbeat" : ISODate("2015-07-30T04:44:16.199Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T04:44:15.824Z"), "pingMs" : 0, "syncingTo" : "192.168.1.5:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "192.168.1.5:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 164, "optime" : Timestamp(1438231153, 1), "optimeDate" : ISODate("2015-07-30T04:39:13Z"), "lastHeartbeat" : ISODate("2015-07-30T04:44:16.185Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T04:44:14.902Z"), "pingMs" : 0, "electionTime" : Timestamp(1438230801, 1), "electionDate" : ISODate("2015-07-30T04:33:21Z"), "configVersion" : 3 } ], "ok" : 1 } testrs0:SECONDARY> rs.slaveOk() testrs0:SECONDARY> db.testcoll.find() testrs0:SECONDARY> use testdb switched to db testdb testrs0:SECONDARY> db.testcoll.find() { "_id" : ObjectId("55b9942d92ad0ab98483695c"), "Name" : "test", "Age" : 60, "Gender" : "F" } { "_id" : ObjectId("55b9944892ad0ab98483695d"), "Name" : "test", "Age" : 60, "Gender" : "F" } { "_id" : ObjectId("55b9945b92ad0ab98483695e"), "Name" : "test", "Age" : 60, "Gender" : "F" } { "_id" : ObjectId("55b994ce92ad0ab98483695f"), "Name" : "test", "Age" : 50, "Gender" : "F" } { "_id" : ObjectId("55b9aa714b575261aff42f25"), "Name" : "tom", "Age" : 45, "Gender" : "G" }
7、定義優(yōu)先級
使用 rs.conf() 來查看 復(fù)制集配置對象 :
testrs0:SECONDARY> rs.conf() { "_id" : "testrs0", "version" : 3, "members" : [ { "_id" : 0, "host" : "hadoop1.abc.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "192.168.1.4:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "192.168.1.5:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } }
在hadoop3的主節(jié)點把hadoop1的優(yōu)先級定義為2,讓它成為主節(jié)點
將復(fù)制集配置對象復(fù)制給一個變量(如 mycfg )。然后通過該變量對該節(jié)點設(shè)置優(yōu)先級。然后通過 rs.reconfig() 來更新復(fù)制集配置。
注意,設(shè)置優(yōu)先級用這個命令mycfg.members[數(shù)組中第幾個節(jié)點].priority = 2
testrs0:PRIMARY> rs.conf() { "_id" : "testrs0", "version" : 3, "members" : [ { "_id" : 0, "host" : "hadoop1.abc.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "192.168.1.4:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "192.168.1.5:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } } testrs0:PRIMARY> mycfg=rs.conf() { "_id" : "testrs0", "version" : 3, "members" : [ { "_id" : 0, "host" : "hadoop1.abc.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "192.168.1.4:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "192.168.1.5:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } } testrs0:PRIMARY> mycfg.members[0].priority = 2 2 testrs0:PRIMARY> rs.reconfig(mycfg) { "ok" : 1 } testrs0:PRIMARY> 2015-07-30T14:34:44.437+0800 I NETWORK DBClientCursor::init call() failed 2015-07-30T14:34:44.439+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2015-07-30T14:34:44.452+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok testrs0:SECONDARY>
在hadoop1上查看,并停掉hadoop1的服務(wù)
testrs0:PRIMARY> rs.status() { "set" : "testrs0", "date" : ISODate("2015-07-30T06:51:11.952Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "hadoop1.abc.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 7780, "optime" : Timestamp(1438238074, 1), "optimeDate" : ISODate("2015-07-30T06:34:34Z"), "electionTime" : Timestamp(1438238079, 1), "electionDate" : ISODate("2015-07-30T06:34:39Z"), "configVersion" : 4, "self" : true }, { "_id" : 1, "name" : "192.168.1.4:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 7780, "optime" : Timestamp(1438238074, 1), "optimeDate" : ISODate("2015-07-30T06:34:34Z"), "lastHeartbeat" : ISODate("2015-07-30T06:51:11.072Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T06:51:11.375Z"), "pingMs" : 0, "configVersion" : 4 }, { "_id" : 2, "name" : "192.168.1.5:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 7780, "optime" : Timestamp(1438238074, 1), "optimeDate" : ISODate("2015-07-30T06:34:34Z"), "lastHeartbeat" : ISODate("2015-07-30T06:51:11.779Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T06:51:10.299Z"), "pingMs" : 0, "configVersion" : 4 } ], "ok" : 1 } testrs0:PRIMARY> quit() You have new mail in /var/spool/mail/root [root@hadoop1 ~]# service mongo stop mongo: 未被識別的服務(wù) [root@hadoop1 ~]# service mongod stop Stopping mongod:
在hadoop3查看,兩個節(jié)點還是會自動選舉主節(jié)點的
testrs0:PRIMARY> rs.status() { "set" : "testrs0", "date" : ISODate("2015-07-30T06:55:18.238Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "hadoop1.abc.com:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2015-07-30T06:55:16.275Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T06:51:44.879Z"), "pingMs" : 5, "lastHeartbeatMessage" : "Failed attempt to connect to hadoop1.abc.com:27017; couldn't connect to server hadoop1.abc.com:27017 (192.168.1.3), connection attempt failed", "configVersion" : -1 }, { "_id" : 1, "name" : "192.168.1.4:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 16000, "optime" : Timestamp(1438238074, 1), "optimeDate" : ISODate("2015-07-30T06:34:34Z"), "lastHeartbeat" : ISODate("2015-07-30T06:55:17.988Z"), "lastHeartbeatRecv" : ISODate("2015-07-30T06:55:17.988Z"), "pingMs" : 1, "lastHeartbeatMessage" : "could not find member to sync from", "configVersion" : 4 }, { "_id" : 2, "name" : "192.168.1.5:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 16003, "optime" : Timestamp(1438238074, 1), "optimeDate" : ISODate("2015-07-30T06:34:34Z"), "electionTime" : Timestamp(1438239108, 1), "electionDate" : ISODate("2015-07-30T06:51:48Z"), "configVersion" : 4, "self" : true } ], "ok" : 1 }
把hadoop1服務(wù)啟動,因為優(yōu)先級設(shè)為2,故推舉它為主節(jié)點
testrs0:PRIMARY> rs.i rs.initiate( rs.isMaster( testrs0:PRIMARY> rs.isMaster() { "setName" : "testrs0", "setVersion" : 4, "ismaster" : true, "secondary" : false, "hosts" : [ "hadoop1.abc.com:27017", "192.168.1.4:27017", "192.168.1.5:27017" ], "primary" : "hadoop1.abc.com:27017", "me" : "hadoop1.abc.com:27017", "electionId" : ObjectId("55b9ca84ddeeac6a93355c18"), "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-07-30T06:56:28.472Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1 }
8、觸發(fā)重新選舉
優(yōu)先級為0的節(jié)點,無權(quán)觸發(fā);僅參與選舉
使用的命令rs.addArb()
9、多端口復(fù)制集
應(yīng)用程序需要連接多個復(fù)制集,那么每個復(fù)制集需要有不同的名字
1)為每個節(jié)點建立必要的數(shù)據(jù)文件夾:
[root@hadoop1 ~]# mkdir -pv /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2 mkdir: 已創(chuàng)建目錄 "/srv/mongodb" mkdir: 已創(chuàng)建目錄 "/srv/mongodb/rs0-0" mkdir: 已創(chuàng)建目錄 "/srv/mongodb/rs0-1" mkdir: 已創(chuàng)建目錄 "/srv/mongodb/rs0-2" You have new mail in /var/spool/mail/root
2)啟動mongod實例
第一個節(jié)點
[root@hadoop1 rs0-0]# mongod --port 27018 --dbpath /srv/mongodb/rs0-0 --replSet rs0 --smallfiles --oplogSize 128 &
第二個節(jié)點
[root@hadoop1 ~]# mongod --port 27019 --dbpath /srv/mongodb/rs0-1 --replSet rs0 --smallfiles --oplogSize 128 &
第三個節(jié)點
[root@hadoop1 ~]# mongod --port 27020 --dbpath /srv/mongodb/rs0-2 --replSet rs0 --smallfiles --oplogSize 128 &
驗證
[root@hadoop1 ~]# netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 15718/mongod tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN 15785/mongod tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1081/rpcbind tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 14221/mongod tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1285/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1157/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1361/master tcp 0 0 0.0.0.0:44378 0.0.0.0:* LISTEN 1099/rpc.statd tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 14221/mongod tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 15640/mongod tcp 0 0 :::111 :::* LISTEN 1081/rpcbind tcp 0 0 :::22 :::* LISTEN 1285/sshd tcp 0 0 ::1:631 :::* LISTEN 1157/cupsd tcp 0 0 ::1:25 :::* LISTEN 1361/master tcp 0 0 :::48510 :::* LISTEN 1099/rpc.statd
3)指明所需連接的端口,來通過 mongo 命令連接到某個 mongod 實例
[root@hadoop1 mongodb]# mongo --port 27018 MongoDB shell version: 3.0.5 connecting to: 127.0.0.1:27018/test 2015-07-30T16:26:01.442+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:54185 #1 (1 connection now open) Server has startup warnings: 2015-07-30T16:19:14.667+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2015-07-30T16:19:14.667+0800 I CONTROL [initandlisten] 2015-07-30T16:19:14.668+0800 I CONTROL [initandlisten] 2015-07-30T16:19:14.668+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2015-07-30T16:19:14.668+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-07-30T16:19:14.668+0800 I CONTROL [initandlisten] 2015-07-30T16:19:14.668+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2015-07-30T16:19:14.668+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-07-30T16:19:14.668+0800 I CONTROL [initandlisten] >
最后關(guān)于
由四個節(jié)點組成的異地分布復(fù)制集有以下兩個需要注意的:
一個節(jié)點(如``hadoop4.abc.net``) 必須是一個 arbiter。這個節(jié)點可以在服務(wù)的任何機器上運行,或者在其他的MongoDB機器上運行。
我們需要決定如何分配節(jié)點。下列有3種架構(gòu)類型:
在大多數(shù)情況下,由于第一種架構(gòu)的易用性,我們更推薦第一種架構(gòu)。
3個節(jié)點在Site A中,一個 優(yōu)先級為0的節(jié)點 在Site B中,與此同時Site A中還要有個投票節(jié)點。
兩個節(jié)點在Site A,兩個 優(yōu)先級為0的節(jié)點 在Site B ,同時一個投票節(jié)點在Site A。
兩個節(jié)點在Site A,一個優(yōu)先級為0的節(jié)點在Site B,一個優(yōu)先級為0的節(jié)點在Site C,同時一個投票節(jié)點在Site A。
在大多數(shù)情況下,由于第一種架構(gòu)的易用性,更推薦第一種架構(gòu)。
復(fù)制集參考
Replication Methods in the mongo Shell 命令 描述 rs.add() 為復(fù)制集新增節(jié)點。 rs.addArb() 為復(fù)制集新增一個 arbiter rs.conf() 返回復(fù)制集配置信息 rs.freeze() 防止當(dāng)前節(jié)點在一段時間內(nèi)選舉成為主節(jié)點。 rs.help() 返回 replica set 的命令幫助 rs.initiate() 初始化一個新的復(fù)制集。 rs.printReplicationInfo() 以主節(jié)點的視角返回復(fù)制的狀態(tài)報告。 rs.printSlaveReplicationInfo() 以從節(jié)點的視角返回復(fù)制狀態(tài)報告。 rs.reconfig() 通過重新應(yīng)用復(fù)制集配置來為復(fù)制集更新配置。 rs.remove() 從復(fù)制集中移除一個節(jié)點。 rs.slaveOk() 為當(dāng)前的連接設(shè)置 slaveOk 。不推薦使用。使用 readPref() 和 Mongo.setReadPref() 來設(shè)置 read preference 。 rs.status() 返回復(fù)制集狀態(tài)信息。 rs.stepDown() 讓當(dāng)前的 primary 變?yōu)閺墓?jié)點并觸發(fā) election 。 rs.syncFrom() 設(shè)置復(fù)制集節(jié)點從哪個節(jié)點處同步數(shù)據(jù),將會覆蓋默認(rèn)選取邏輯。
復(fù)制集數(shù)據(jù)庫命令
命令 描述 replSetFreeze 防止當(dāng)前節(jié)點在一段時間內(nèi)選舉成為主節(jié)點。 replSetGetStatus 返回復(fù)制集的狀態(tài)報告。 replSetInitiate 初始化一個新的復(fù)制集。 replSetMaintenance 開啟活關(guān)閉維護模式,維護模式將使 secondary 進入 RECOVERING 狀態(tài)。 replSetReconfig 為已存在的復(fù)制集應(yīng)用新的配置。 replSetStepDown 強制當(dāng)前的 primary *降職*變?yōu)?nbsp;secondary ,并觸發(fā)選舉。 replSetSyncFrom 覆蓋默認(rèn)的復(fù)制來源選取邏輯。 resync 強制 mongod 重新從 master 進行初始化復(fù)制。僅在主-從模式。 applyOps 內(nèi)部命令應(yīng)用 oplog 在現(xiàn)在的數(shù)據(jù)集上。 isMaster 返回該節(jié)點的角色信息,包括是否為主節(jié)點。 getoptime 內(nèi)部命令,返回optime。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。