mongodb複製集部署

來源:互聯網
上載者:User

標籤:mongodb複製集部署

部署複製集

由三個節點群組成的 複製集 為網路故障或是其他的系統故障提供了足夠的冗餘。該複製集也有足夠的分布式讀操作的能力。複製集應該保持奇數個節點,這也就保證了 選舉 可以正常的進行

用3台已有的 mongod 執行個體來部署一個由三個節點群組成的 複製集

192.168.1.3 hadoop1.abc.com hadoop1
192.168.1.4 hadoop2.abc.com hadoop2
192.168.1.5 hadoop3.abc.com hadoop3

 

 

部署複製集的注意事項架構

在生產環境中,我們應該將每個節點部署在獨立的機器上,並使用標準的MongoDB連接埠 27017 。使用 bind_ip 參數來限制訪問MongoDB的應用程式的地址。

若使用了異地分布式架構的複製集,請確保多數 mongod 執行個體節點位於主要資料中心中。

 

連通性

確保各個節點之間可以正常通訊,且各個用戶端都處於安全的可信的網路環境中。可以考慮以下事項:

  • 建立虛擬專用網路。確保各個節點之間的流量是在本網範圍內路由的。(Establish a virtual private network. Ensure that your network topology routes all traffic between members within a single site over the local area network.)

  • 配置串連限制來防止未知的用戶端串連到複製集。

  • 配置網路設定和防火牆規則來對將MongoDB的連接埠僅開放給應用程式,來讓應用程式發的進出資料包可以與MongoDB正常交流。

最後請確保複製集各節點可以互相通過DNS或是主機名稱解析。我們需要配置DNS網域名稱或是設定 /etc/hosts 檔案來配置。

 

這裡實驗,是關閉防火牆,並把selinux設定成setenforce 0

詳細步驟

1、建立每個節點都建立據據目錄

[[email protected] ~]# mkidr -pv /mongodb/data/

[[email protected] ~]# chown mongod.mongod /mongodb/data/

2將複製集中的每個節點以適當的配置參數啟動。

在每個節點上啟動 mongod 並通過制定 replSet 參數來指定其複製集名,並可以指定其他需要的參數

 

[[email protected] ~]# vim /etc/mongod.conf

//添加如下

#Replica Set
replSet = testrs0

或者

[[email protected] ~]# mongod --replSet "testrs0"

 

確保每個節點都有相同複製集名稱

[[email protected] ~]# scp /etc/mongod.conf [email protected]:/etc/;scp /etc/mongod.conf [email protected]:/etc/;

 

注意了,如果解決啟動mongod 時,出現addr already in use錯誤,原因啟動連接埠被佔用

[[email protected] 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掉

[[email protected] ~]# netstat -anp|more

unix  2      [ ACC ]     STREAM     LISTENING     15588  2174/mongod         /tmp/mongodb-27017.sock

 

[[email protected] ~]# kill 2174

[[email protected] ~]# /etc/init.d/mongod start
Starting mongod:                                           [確定]

3、開啟 mongo 介面並串連到複製集的某個節點。

[[email protected] ~]# mongo

4、初始化複製集。

//使用rs.initiate()命令,MongoDB將初始化一個由當前節點構成、擁有預設配置的複製集。

 

> 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

 

[[email protected] ~]# service mongod stop
Stopping mongod:                                           [確定]
You have new mail in /var/spool/mail/root
[[email protected] ~]# vim /etc/mongod.conf

開啟了

bind 127.0.0.1 把本地限制訪問了

#bind 127.0.0.1

 

[[email protected] 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、將其他的節點加入複製集。

通過 rs.add() 來將剩下的節點加入複製集。

 

 

mongodb複製集部署

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.