MongoDB Replica Set 詳細安裝文檔

1. 準備工作

  • 連接網絡

    • 檢查網卡設置

    • 搞通網關連通

    • 設置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,企業Linux附加軟件包。epel的軟件包通常不會與企業版Linux
官方源中的軟件包發生沖突,或者相互替換。


  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 中粘貼如下內容


  [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

執行


  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. 統一目錄

創建數據存儲目錄


  cd /data/
  sudo mkdir db37017
  sudo mkdir db47017

統一配置文件


  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 ,若之前已經使用mmapv1
有生成的數據目錄,再使用同一個數據目錄,但引擎變更為 wiredTiger
時,無法啟動。需另外創建一塊數據目錄,為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

    如果在內網,可以關閉防火墻

    
      sudo systemctl stop firewalld
    
    
  • configration

192.168.1.101:27017 為主節點配置


  [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()

添加其他節點


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

禁止鏈式復制,設置各節點優先級


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

Replica Set 之后狀態如下:


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
 }

執行以下命令啟動:


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

5. 設置monit監控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 監控MongoDB

192.168.1.101 監控配置

配置文件位置:/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 監控配置,由于啟動兩個 MongoDB instance 所以分別監控

配置文件位置:/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 分別執行 sudo monit reload 使配置生效。

你還可以

sudo monit start all 啟動所有監控應用

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

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

sudo monit stop all 停止所有監控應用

sudo monit restart all 重啟所有監控應用

sudo monit unmonitor all 停止監控所有應用

6. 數據文件備份

  • 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

  • 備份生成的文件結構如下

  [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 有效節點不足最小值(Math.floor(4/2 + 1))
該4個節點的集群中,有效節點不足3個,整個集群便不可用。

8 MongoDB 常見的錯誤

出現錯誤,首先查看日志 /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 執行


   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的服務器能相互聯通,是設置Replica Set 的基礎

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

直接創建生成目錄即可


```

  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,強制執行

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 節點是已經運行,線上服務器
B、C是新增的備份節點。

參考

mongodb 官方文檔

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容