Windows下MongoDB安装副本集

JimmySusan 发布于1年前
  • 安装环境简介
    Windows10、MongoDB4.0.3、Robo 3T(mongo命令行操作比较方便)
  • 创建数据库文件夹、配置文件等

      (1)数据库文件夹:db27017、db37017、db47017
      (2)log文件夹:log27017、log37017、log47017
      (3)配置文件:mongod27017、mongod37017、mongod47017
      配置文件更改端口号即可,具体内容如下:
      λ cat mongod27017.conf
       systemLog:
           destination: file
           logAppend: true
           path: E:\mongo\log27017\mongod27017.log
       
       storage:
           dbPath: E:\mongo\db27017
           journal:
               enabled: true
           engine: wiredTiger
       
       #processManagement:
       #    fork: true
       #    pidFilePath: E:\mongo\mongod27017.pid
       
       net:
           port: 27017
       
       replication:
           replSetName: mongo
           
  • 启动primary

      λ mongod --config "..\mongod27017.conf"
      2018-11-08T21:23:38.399+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
       #查看状态
       > rs.status()
       {
               "operationTime" : Timestamp(0, 0),
               "ok" : 0,
               "errmsg" : "no replset config has been received",
               "code" : 94,
               "codeName" : "NotYetInitialized",
               "$clusterTime" : {
                       "clusterTime" : Timestamp(0, 0),
                       "signature" : {
                               "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                               "keyId" : NumberLong(0)
                       }
               }
       }
      #副本集配置并启动
      > config = {_id:"mongo",members:[{_id:0,host:"127.0.0.1:27017"}]}
           {
                   "_id" : "mongo",
                   "members" : [
                           {
                                   "_id" : 0,
                                   "host" : "127.0.0.1:27017"
                           }
                   ]
           }
       > rs.initiate(config)
       {
               "ok" : 1,
               "operationTime" : Timestamp(1541685032, 1),
               "$clusterTime" : {
                       "clusterTime" : Timestamp(1541685032, 1),
                       "signature" : {
                               "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                               "keyId" : NumberLong(0)
                       }
               }
       }
    
  • 添加其他成员

     λ mongod --config "..\mongod37017.conf"
    2018-11-08T21:52:18.237+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
    
       mongo:PRIMARY> rs.add("127.0.0.1:37017")
       {
               "ok" : 1,
               "operationTime" : Timestamp(1541685236, 1),
               "$clusterTime" : {
                       "clusterTime" : Timestamp(1541685236, 1),
                       "signature" : {
                               "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                               "keyId" : NumberLong(0)
                       }
               }
       }
     
     λ mongod --config "..\mongod47017.conf"
     2018-11-08T22:01:26.664+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
       #这次指定arbiterOnly参数,以为创建一个仲裁接点
       mongo:PRIMARY> rs.add("127.0.0.1:47017",{arbiterOnly:true})
       {
               "ok" : 1,
               "operationTime" : Timestamp(1541685748, 1),
               "$clusterTime" : {
                       "clusterTime" : Timestamp(1541685748, 1),
                       "signature" : {
                               "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                               "keyId" : NumberLong(0)
                       }
               }
       }
       #查看副本集的状态信息
       mongo:PRIMARY> db.isMaster()
       {
               "hosts" : [
                       "127.0.0.1:27017",
                       "127.0.0.1:37017"
               ],
               "arbiters" : [
                       "127.0.0.1:47017"
               ],
               "setName" : "mongo",
               "setVersion" : 3,
               "ismaster" : true,
               "secondary" : false,
               "primary" : "127.0.0.1:27017",
               "me" : "127.0.0.1:27017",
               "electionId" : ObjectId("7fffffff0000000000000001"),
               "lastWrite" : {
                       "opTime" : {
                               "ts" : Timestamp(1541685884, 1),
                               "t" : NumberLong(1)
                       },
                       "lastWriteDate" : ISODate("2018-11-08T14:04:44Z"),
                       "majorityOpTime" : {
                               "ts" : Timestamp(1541685884, 1),
                               "t" : NumberLong(1)
                       },
                       "majorityWriteDate" : ISODate("2018-11-08T14:04:44Z")
               },
               "maxBsonObjectSize" : 16777216,
               "maxMessageSizeBytes" : 48000000,
               "maxWriteBatchSize" : 100000,
               "localTime" : ISODate("2018-11-08T14:04:52.091Z"),
               "logicalSessionTimeoutMinutes" : 30,
               "minWireVersion" : 0,
               "maxWireVersion" : 7,
               "readOnly" : false,
               "ok" : 1,
               "operationTime" : Timestamp(1541685884, 1),
               "$clusterTime" : {
                       "clusterTime" : Timestamp(1541685884, 1),
                       "signature" : {
                               "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                               "keyId" : NumberLong(0)
                       }
               }
       }

    以上副本集创建完成,接下来看下数据同步

       mongo:PRIMARY> show dbs
       admin   0.000GB
       config  0.000GB
       local   0.000GB
       mongo:PRIMARY> use mongo
       switched to db mongo
       mongo:PRIMARY> db.createCollection("test")
       {
               "ok" : 1,
               "operationTime" : Timestamp(1541686077, 1),
               "$clusterTime" : {
                       "clusterTime" : Timestamp(1541686077, 1),
                       "signature" : {
                               "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                               "keyId" : NumberLong(0)
                       }
               }
       }
       mongo:PRIMARY> show collections
       test
       

    clipboard.png

    以上就是搭建的全过程,后续学习再补充这块内容~

查看原文: Windows下MongoDB安装副本集

  • lazygorilla
  • bluemeercat
  • bigostrich
  • tinywolf
  • beautifulfish
  • crazyleopard
  • tinylion