MongoDB 复制(副本集)

复制原理

安装配置前可以先通过菜鸟网了解一下https://www.runoob.com/mongodb/mongodb-replication.html

安装部署

选择三台服务器(虚拟机):192.168.100.216(主节点) 192.168.100.217(副本节点) 192.168.100.218(副本节点)

虚拟机安装参考
关闭防火墙

  1. 下载mongodb:

     wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.10.tgz
    
  2. 解压到:/data/program/software

  3. 文件夹重命名为mongodb

  4. 进入mongodb目录:cd /data/program/software/mongodb
    新建两个文件夹:

     mkdir db
     mkdir logs
    
  5. 进入bin目录cd /data/program/software/mongodb/bin
    新建配置文件:touch mongodb.conf

     dbpath=/data/program/software/mongodb/db
     logpath=/data/program/software/mongodb/logs/mongodb.log
     port=27017
     bind_ip=127.0.0.1,192.168.100.216 #修改相应IP
     fork=true            #设置后台运行
     logappend=true       #日志输出方式
    
  6. 分别三台服务器上启动mongodb:

    	 /data/program/software/mongodb/bin/mongod --replSet repset -f /data/program/software/mongodb/bin/mongodb.conf
    
  7. 各个服务器查看,都已经启动:

  8. 在三台机器上任意一台机器登陆mongodb:

     /data/program/software/mongodb/bin/mongo
    
  9. 使用admin数据库

     use admin
    
  10. 定义副本集配置变量,这里的_id:”repset”和上面命令参数–replSet repset保持一致

    config = { _id:"repset", members:[{_id:0,host:"192.168.100.216:27017"},{_id:1,host:"192.168.100.217:27017"},{_id:2,host:"192.168.100.218:27017"}]}
    
  11. 初始化副本集群:

    rs.initiate(config);
    

    当看到"ok" : 1 表示成功,9-11截图
    MongoDB 复制(副本集)_第1张图片

  12. 查看集群节点的状态:

    rs.status();
    

    如下结果:

    {
     "set" : "repset",
     "date" : ISODate("2017-09-21T14:30:17.190Z"),
     "myState" : 1,
     "term" : NumberLong(1),
     "heartbeatIntervalMillis" : NumberLong(2000),
     "optimes" : {
      "lastCommittedOpTime" : {
       "ts" : Timestamp(1506004207, 1),
       "t" : NumberLong(1)
      },
      "appliedOpTime" : {
       "ts" : Timestamp(1506004207, 1),
       "t" : NumberLong(1)
      },
      "durableOpTime" : {
       "ts" : Timestamp(1506004207, 1),
       "t" : NumberLong(1)
      }
     },
     "members" : [
      {
       "_id" : 0,
       "name" : "10.211.55.7:27017",
       "health" : 1,
       "state" : 2,
       "stateStr" : "SECONDARY",
       "uptime" : 111,
       "optime" : {
    "ts" : Timestamp(1506004207, 1),
    "t" : NumberLong(1)
       },
       "optimeDurable" : {
    "ts" : Timestamp(1506004207, 1),
    "t" : NumberLong(1)
       },
       "optimeDate" : ISODate("2017-09-21T14:30:07Z"),
       "optimeDurableDate" : ISODate("2017-09-21T14:30:07Z"),
       "lastHeartbeat" : ISODate("2017-09-21T14:30:16.070Z"),
       "lastHeartbeatRecv" : ISODate("2017-09-21T14:30:16.887Z"),
       "pingMs" : NumberLong(0),
       "syncingTo" : "10.211.55.9:27017",
       "configVersion" : 1
      },
      {
       "_id" : 1,
       "name" : "10.211.55.8:27017",
       "health" : 1,
       "state" : 2,
       "stateStr" : "SECONDARY",
       "uptime" : 111,
       "optime" : {
    "ts" : Timestamp(1506004207, 1),
    "t" : NumberLong(1)
       },
       "optimeDurable" : {
    "ts" : Timestamp(1506004207, 1),
    "t" : NumberLong(1)
       },
       "optimeDate" : ISODate("2017-09-21T14:30:07Z"),
       "optimeDurableDate" : ISODate("2017-09-21T14:30:07Z"),
       "lastHeartbeat" : ISODate("2017-09-21T14:30:16.070Z"),
       "lastHeartbeatRecv" : ISODate("2017-09-21T14:30:16.877Z"),
       "pingMs" : NumberLong(0),
       "syncingTo" : "10.211.55.9:27017",
       "configVersion" : 1
      },
      {
       "_id" : 2,
       "name" : "10.211.55.9:27017",
       "health" : 1,
       "state" : 1,
       "stateStr" : "PRIMARY",
       "uptime" : 163,
       "optime" : {
    "ts" : Timestamp(1506004207, 1),
    "t" : NumberLong(1)
       },
       "optimeDate" : ISODate("2017-09-21T14:30:07Z"),
       "infoMessage" : "could not find member to sync from",
       "electionTime" : Timestamp(1506004115, 1),
       "electionDate" : ISODate("2017-09-21T14:28:35Z"),
       "configVersion" : 1,
       "self" : true
      }
     ],
     "ok" : 1
    }
    

你可能感兴趣的:(MongoDB)