MongoDB Replica Set 詳細安裝文檔

1. 準備工作

  • 連接網(wǎng)絡

    • 檢查網(wǎng)卡設置

    • 搞通網(wǎng)關連通

    • 設置dns等即可

    參考

2. Install MongoDB

  • Install net-tools

確保ifconfig, netstat, route 等命令可以使用


  sudo yum install net-tools

  • Install wget

    下面會用到wget命令庇茫,CentOS 7 最小安裝默認沒帶wget命令


  sudo yum install wget

  • Install epel

epel能庆,企業(yè)Linux附加軟件包夸赫。epel的軟件包通常不會與企業(yè)版Linux
官方源中的軟件包發(fā)生沖突驰吓,或者相互替換。


  wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
  sudo rpm -ivh epel-release-7-5.noarch.rpm

  yum --enablerepo=epel info mongodb
    Name        : mongodb
    Arch        : x86_64
    Version     : 2.6.11
    Release     : 1.el7
    Size        : 43 M
    Repo        : epel/x86_64

在epel源中顯示的版本為 2.6.11 版本榨了,不符合預期倒源。需安裝最新的3.0.7版本

參考官方的安裝指南
install-mongodb-on-red-hat


  sudo touch /etc/yum.repos.d/mongodb-org-3.0.repo

  sudo vim /etc/yum.repos.d/mongodb-org-3.0.repo

在 /etc/yum.repos.d/mongodb-org-3.0.repo 中粘貼如下內(nèi)容


  [mongodb-org-3.0]
   name=MongoDB Repository
   baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
   gpgcheck=0
   enabled=1

執(zhí)行


  sudo yum install -y mongodb-org

提示安裝成功


  Installed:
  mongodb-org.x86_64 0:3.0.7-1.el7

安裝之后,使用下面命令測試安裝是否成功


  sudo service mongod start  開啟MongoDB

  sudo service mongod stop   關閉MongoDB

3. 統(tǒng)一目錄

創(chuàng)建數(shù)據(jù)存儲目錄


  cd /data/
  sudo mkdir db37017
  sudo mkdir db47017

統(tǒng)一配置文件


  cd /etc/
  sudo cp mongod.conf mongod_37017.conf
  sudo cp mongod.conf mongod_47017.conf

授予當前操作者操作目錄的權限


  sudo chown -R deploy /var/log/mongodb
  sudo chown -R deploy /data/
  sudo chown -R deploy /var/run/mongodb/

192.168.1.100:37017 配置如下

配置文件位置:/etc/mongod37017.conf


  # mongod.conf
  # for documentation of all options, see:
  #   http://docs.mongodb.org/manual/reference/configuration-options/

  systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongod37017.log

  storage:
    dbPath: /data/wt_db37017
    journal:
      enabled: true
    engine: wiredTiger

  processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/mongod37017.pid

  net:
    port: 37017

  replication:
    replSetName: kt_rs

192.168.1.100:47017 配置如下

配置文件位置:/etc/mongod47017.conf


  # mongod.conf
  # for documentation of all options, see:
  #   http://docs.mongodb.org/manual/reference/configuration-options/

  systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongod47017.log

  storage:
    dbPath: /data/wt_db47017
    journal:
      enabled: true
    engine: wiredTiger

  processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/mongod47017.pid

  net:
    port: 47017

  replication:
    replSetName: kt_rs

192.168.1.101:27017 配置如下

配置文件位置:/etc/mongod27017.conf


  # mongod.conf
  # for documentation of all options, see:
  #   http://docs.mongodb.org/manual/reference/configuration-options/

  systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongod27017.log

  storage:
    dbPath: /data/wt_db27017
    journal:
      enabled: true
    engine: wiredTiger

  processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/mongod27017.pid

  net:
    port: 27017

  replication:
   replSetName: kt_rs

注意:MongoDB 的默認引擎為 mmapv1 孵稽,若之前已經(jīng)使用mmapv1
有生成的數(shù)據(jù)目錄许起,再使用同一個數(shù)據(jù)目錄,但引擎變更為 wiredTiger
時肛冶,無法啟動街氢。需另外創(chuàng)建一塊數(shù)據(jù)目錄,為wiredTiger 單獨配置睦袖。
詳細參考下面鏈接:

upgrade-a-replica-set
default-mongodb-port

4. Replica Set Deployment

  • bind_ip

    MongoDB 默認為 127.0.0.1
    要配置 Replica Set 就需要注釋掉bind_ip

  • telnet

    
      yum install telnet
    
    
確保以下命令在各服務器上務必都成功

```

  telnet 192.168.1.101 27017
  telnet 192.168.1.100 37017
  telnet 192.168.1.100 47017

```
  • firewall

    如果在內(nèi)網(wǎng)珊肃,可以關閉防火墻

    
      sudo systemctl stop firewalld
    
    
  • configration

192.168.1.101:27017 為主節(jié)點配置


  [deploy@dev01 ~]$ mongo 192.168.1.101:27017

  config = {
    "_id": "kt_rs",
    "members": [
      {
        "_id": 0,
        "host": "192.168.1.101:27017"
      }
    ]
  }

  rs.initiate(config)
  rs.status()

添加其他節(jié)點


  rs.add("192.168.1.100:37017")
  rs.add("192.168.1.100:47017")

禁止鏈式復制,設置各節(jié)點優(yōu)先級


 cfg = rs.config()
 cfg["settings"]["chainingAllowed"] = false
 cfg["members"][3].priority = 5  ## 設置當前配置最好的機器優(yōu)先級稍微高一些
 rs.reconfig(cfg, { "force": true })

Replica Set 之后狀態(tài)如下:


kt_rs:SECONDARY> rs.status()
 {
  "set" : "kt_rs",
  "date" : ISODate("2015-11-06T10:02:40.831Z"),
  "myState" : 2,
  "syncingTo" : "192.168.1.101:27017",
  "members" : [
      {
          "_id" : 1,
          "name" : "192.168.1.100:37017",
          "health" : 1,
          "state" : 2,
          "stateStr" : "SECONDARY",
          "uptime" : 7937,
          "optime" : Timestamp(14467.2962, 2),
          "optimeDate" : ISODate("2015-11-06T09:42:42Z"),
          "syncingTo" : "192.168.1.101:27017",
          "configVersion" : 33761,
          "self" : true
      },
      {
          "_id" : 2,
          "name" : "192.168.1.100:47017",
          "health" : 1,
          "state" : 2,
          "stateStr" : "SECONDARY",
          "uptime" : 7316,
          "optime" : Timestamp(14467.2962, 2),
          "optimeDate" : ISODate("2015-11-06T09:42:42Z"),
          "lastHeartbeat" : ISODate("2015-11-06T10:02:40.245Z"),
          "lastHeartbeatRecv" : ISODate("2015-11-06T10:02:40.245Z"),
          "pingMs" : 0,
          "syncingTo" : "192.168.1.100:37017",
          "configVersion" : 33761
      },
      {
          "_id" : 3,
          "name" : "192.168.1.101:27017",
          "health" : 1,
          "state" : 1,
          "stateStr" : "PRIMARY",
          "uptime" : 7937,
          "optime" : Timestamp(14467.2962, 2),
          "optimeDate" : ISODate("2015-11-06T09:42:42Z"),
          "lastHeartbeat" : ISODate("2015-11-06T10:02:38.917Z"),
          "lastHeartbeatRecv" : ISODate("2015-11-06T10:02:39.667Z"),
          "pingMs" : 0,
          "electionTime" : Timestamp(1446796155, 1),
          "electionDate" : ISODate("2015-11-06T07:49:15Z"),
          "configVersion" : 33761
      }
  ],
  "ok" : 1
 }

執(zhí)行以下命令啟動:


  mongod -f /etc/mongod27017.conf
  mongod -f /etc/mongod37017.conf
  mongod -f /etc/mongod47017.conf

5. 設置monit監(jiān)控MongoDB

  • 安裝monit

    安裝monit 之前馅笙,確保epel 安裝成功


  sudo yum install monit
  ## 設置開機自動啟動
  sudo systemctl enable monit.service
  sudo service monit start

任務 舊指令 新指令
使某服務自動啟動 chkconfig --level 3 httpd on systemctl enable httpd.service
啟動某服務 service httpd start systemctl start httpd.service
重啟某服務 service httpd restart systemctl restart httpd.service

CentOS 7.x
systemd

monit 配置

配置文件位置: /etc/monitrc


  set daemon  30
  set logfile syslog

  set mailserver smtp.126.com username "wan***" password "****"

  set mail-format {
    from: wanghao293@126.com
    subject: monit alert --  $EVENT 192.168.1.100
    message: $EVENT Service $SERVICE
                  Date:        $DATE
                  Action:      $ACTION
                  Host:        $HOST
                  Description: $DESCRIPTION

             Your faithful employee,
             Monit
  }

  set alert wanghao@kaitongamc.com

  set httpd port 27.2 and
     allow admin:aPIFuc/3

  include /etc/monit.d/*

  • 設置monit 監(jiān)控MongoDB

192.168.1.101 監(jiān)控配置

配置文件位置:/etc/monit.d/m_mongo27017.conf


  check process mongodb with pidfile /var/run/mongodb/mongod27017.pid
    group database
    start program = "/usr/bin/mongod -f /etc/mongod27017.conf"
    stop program  = "/usr/bin/mongod -f /etc/mongod27017.conf --shutdown"
    if failed host 127.0.0.1 port 27017 then restart
    if failed host 127.0.0.1 port 27017 then alert
    if 5 restarts within 5 cycles then timeout
    if 5 restarts within 5 cycles then alert

192.168.1.100 監(jiān)控配置伦乔,由于啟動兩個 MongoDB instance 所以分別監(jiān)控

配置文件位置:/etc/monit.d/m_mongo37017.conf


 check process mongodb37017 with pidfile /var/run/mongodb/mongod37017.pid
   group database
   start program = "/usr/bin/mongod -f /etc/mongod37017.conf"
   stop program  = "/usr/bin/mongod -f /etc/mongod37017.conf --shutdown"
   if failed host 127.0.0.1 port 37017 then restart
   if failed host 127.0.0.1 port 37017 then alert
   if 5 restarts within 5 cycles then timeout
   if 5 restarts within 5 cycles then alert

配置文件位置:/etc/monit.d/m_mongo47017.conf


 check process mongodb47017 with pidfile /var/run/mongodb/mongod47017.pid
   group database
   start program = "/usr/bin/mongod -f /etc/mongod47017.conf"
   stop program  = "/usr/bin/mongod -f /etc/mongod47017.conf --shutdown"
   if failed host 127.0.0.1 port 47017 then restart
   if failed host 127.0.0.1 port 47017 then alert
   if 5 restarts within 5 cycles then timeout
   if 5 restarts within 5 cycles then alert

在192.168.1.101 和 192.168.1.100 分別執(zhí)行 sudo monit reload 使配置生效。

你還可以

sudo monit start all 啟動所有監(jiān)控應用

sudo monit start mongodb37017 啟動端口為37017 的MongoDB instance

sudo monit start mongodb47017 啟動端口為47017 的MongoDB instance

sudo monit stop all 停止所有監(jiān)控應用

sudo monit restart all 重啟所有監(jiān)控應用

sudo monit unmonitor all 停止監(jiān)控所有應用

6. 數(shù)據(jù)文件備份

  • clone 備份腳本

  git clone https://github.com/micahwedemeyer/automongobackup.git

  sudo chown deploy /var/backups/mongodb/

  • 編寫定時任務

  crontab -e

  30 23 * * * /bin/bash /home/deploy/automongobackup/src/automongobackup.sh

  • 備份生成的文件結(jié)構如下

  [deploy@dev01 ~]$ tree /var/backups/mongodb/
  /var/backups/mongodb/
  ├── daily
  │   ├── 2015-11-04_23h30m.Wednesday.tgz
  │   └── 2015-11-05_23h30m.Thursday.tgz
  ├── latest
  │   └── 2015-11-05_23h30m.Thursday.tgz
  ├── monthly
  └── weekly

  4 directories, 3 files

7 集群設置注意點

Replica Set 有效節(jié)點不足最小值(Math.floor(4/2 + 1))
該4個節(jié)點的集群中董习,有效節(jié)點不足3個烈和,整個集群便不可用。

8 MongoDB 常見的錯誤

出現(xiàn)錯誤皿淋,首先查看日志 /var/log/mongodb/mongod.log

8.1 mongodb exception in initAndListen: 12596 old lock file, terminating

  • 刪除data目錄中的.lock 文件

  • 重新啟動mongod

8.2 exception in initAndListen: 15926 Insufficient free space for journals, terminating

  [initandlisten] Insufficient free space for journal files
  [initandlisten] Please make at least 3379MB available in /var/lib/mongo/journal or use --smallfiles
  [initandlisten]
  [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
  [initandlisten] now exiting
  [initandlisten] shutdown: going to close listening sockets...
  [initandlisten] removing socket file: /tmp/mongodb-27017.sock
  [initandlisten] shutdown: going to flush diaglog...
  [initandlisten] shutdown: going to close sockets...
  [initandlisten] shutdown: waiting for fs preallocator...
  [initandlisten] shutdown: final commit...
  [initandlisten] shutdown: closing all files...
  [initandlisten] closeAllFiles() finished
  [initandlisten] dbexit:  rc: 100

解決辦法招刹,

第一種恬试,添加 --smallfiles 啟動
mongod --smallfiles --port 27017 -f /etc/mongod.conf --replSet 20151101 --fork

Sets MongoDB to use a smaller default file size. The --smallfiles option reduces the initial size for data files and limits the maximum size to 512 megabytes. --smallfiles also reduces the size of each journal file from 1 gigabyte to 128 megabytes. Use --smallfiles if you have a large number of databases that each holds a small quantity of data.

The --smallfiles option can lead the mongod instance to create a large number of files, which can affect performance for larger databases.

--smallfiles

第二種,增加 /var/lib/mongo 容量

無損增加容量

https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-disk-storage-parted-resize-part.html
http://serverfault.com/questions/644127/centos-6-3-increase-disk-size-on

8.3 No route to host

  [root@localhost kaitong4]# telnet 10.132.1.199 27017
    Trying 10.132.1.199...
    telnet: connect to address 10.132.1.199: No route to host

檢查防火墻

在 10.132.1.199 執(zhí)行


   systemctl stop firewalld


telnet 連接遠程的服務器


 [root@localhost kaitong4]# telnet 10.132.1.199 27017
   Trying 10.132.1.199...
   Connected to 10.132.1.199.

注意:保證MongoDB的服務器能相互聯(lián)通疯暑,是設置Replica Set 的基礎

8.4 exception in initAndListen: 29 Data directory /var/lib/mongo not found

直接創(chuàng)建生成目錄即可


```

  mkdir -p /var/lib/mongo


```

8.5 Permission denied, terminating

[deploy@dev02 wt_db47017]$ tail -f -n 100 /var/log/mongodb/mongod37017.log

2015-11-11T05:29:54.147-0500 I CONTROL  ***** SERVER RESTARTED *****
2015-11-11T05:29:54.194-0500 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2015-11-11T05:29:54.259-0500 E STORAGE  [initandlisten] WiredTiger (13) [1447237794:259729][17432:0x7f94a378ec80]: /data/wt_db37017/journal/WiredTigerLog.0000000005: Permission denied
2015-11-11T05:29:54.264-0500 I -        [initandlisten] Assertion: 28595:13: Permission denied
2015-11-11T05:29:54.264-0500 I STORAGE  [initandlisten] exception in initAndListen: 28595 13: Permission denied, terminating
2015-11-11T05:29:54.264-0500 I CONTROL  [initandlisten] dbexit:  rc: 100
^C

**解決方式**
[deploy@dev02 wt_db47017]$ sudo chown -R deploy /data/wt_db37017/
[deploy@dev02 wt_db47017]$ /usr/bin/mongod -f /etc/mongod37017.conf
about to fork child process, waiting until server is ready for connections.
forked process: 17502
child process started successfully, parent exiting

此方法也適用于 ** ERROR: Cannot write pid file to /var/run/mongodb/mongod27017.pid: Permission denied**
的這種方式训柴。

8.6 重新設置replica set時,使用force妇拯,強制執(zhí)行

rs.initiate(ctf)
{
"info" : "try querying local.system.replset to see current configuration",
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23
}
rs.reconfig(ctf)
{
"ok" : 0,
"errmsg" : "replSetReconfig should only be run on PRIMARY, but my state is REMOVED; use the "force" argument to override",
"code" : 10107
}
rs.reconfig(ctf, force: true)
2016-02-23T16:23:40.837+0800 E QUERY SyntaxError: Unexpected token :
rs.reconfig({ctf, force: true})
2016-02-23T16:23:48.546+0800 E QUERY SyntaxError: Unexpected token ,
rs.reconfig(ctf, { force: true})
{ "ok" : 1 }

9 線上部署步驟流程圖

其中 A 節(jié)點是已經(jīng)運行幻馁,線上服務器
B、C是新增的備份節(jié)點越锈。

參考

mongodb 官方文檔

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末仗嗦,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子甘凭,更是在濱河造成了極大的恐慌稀拐,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件对蒲,死亡現(xiàn)場離奇詭異钩蚊,居然都是意外死亡,警方通過查閱死者的電腦和手機蹈矮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鸣驱,“玉大人泛鸟,你說我怎么就攤上這事∮欢” “怎么了北滥?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長闸翅。 經(jīng)常有香客問我再芋,道長,這世上最難降的妖魔是什么坚冀? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任济赎,我火速辦了婚禮,結(jié)果婚禮上记某,老公的妹妹穿的比我還像新娘司训。我一直安慰自己,他們只是感情好液南,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布壳猜。 她就那樣靜靜地躺著,像睡著了一般滑凉。 火紅的嫁衣襯著肌膚如雪统扳。 梳的紋絲不亂的頭發(fā)上喘帚,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音咒钟,去河邊找鬼啥辨。 笑死,一個胖子當著我的面吹牛盯腌,可吹牛的內(nèi)容都是我干的溉知。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼腕够,長吁一口氣:“原來是場噩夢啊……” “哼级乍!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起帚湘,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤玫荣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后大诸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體捅厂,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年资柔,在試婚紗的時候發(fā)現(xiàn)自己被綠了焙贷。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡贿堰,死狀恐怖辙芍,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情羹与,我是刑警寧澤故硅,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站纵搁,受9級特大地震影響吃衅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜腾誉,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一徘层、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧妄辩,春花似錦惑灵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至哮伟,卻和暖如春干花,著一層夾襖步出監(jiān)牢的瞬間妄帘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工池凄, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留抡驼,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓肿仑,卻偏偏與公主長得像致盟,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子尤慰,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容