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 官方文档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容