网站建设资讯

NEWS

网站建设资讯

【Mongodb】往分片复制集添加复制成员

1.新的成员机器上安装mongo,启动shard1
  1. [root@ip-10-1-2-183 etc]# more shard1.conf
  2. logpath=/usr/local/MongoDB/logs/mongo_shard1.log
  3. logappend=true # need logrotae scripts
  4. fork=true
  5. journal=true
  6. port=27019
  7. #vvvvv = true
  8. #diaglog = 3
  9. dbpath=/usr/local/mongodb/shard1
  10. pidfilepath=/usr/local/mongodb/logs/mongo_shard1.pid
  11. bind_ip=10.1.2.183
  12. replSet=shard1
  13. shardsvr=true

 /usr/local/mongodb/bin/mongod -f shard1.conf

2.主节点上加入新的复制集成员
  1. [root@ip-10-1-2-32 etc]# /usr/local/mongodb/bin/mongo 10.1.2.32:27019
  2. shard1:PRIMARY> rs.add({_id: 3, host: "10.1.2.183:27019"})
  3. {
  4.         "ok" : 1,
  5.         "operationTime" : Timestamp(1525752728, 3),
  6.         "$gleStats" : {
  7.                 "lastOpTime" : {
  8.                         "ts" : Timestamp(1525752728, 3),
  9.                         "t" : NumberLong(1)
  10.                 },
  11.                 "electionId" : ObjectId("7fffffff0000000000000001")
  12.         },
  13.         "$clusterTime" : {
  14.                 "clusterTime" : Timestamp(1525752728, 3),
  15.                 "signature" : {
  16.                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
  17.                         "keyId" : NumberLong(0)
  18.                 }
  19.         },
  20.         "$configServerState" : {
  21.                 "opTime" : {
  22.                         "ts" : Timestamp(1525752728, 2),
  23.                         "t" : NumberLong(1)
  24.                 }
  25.         }
  26. }
  27. shard1:PRIMARY> rs.isMaster()
  28. {
  29.         "hosts" : [
  30.                 "10.1.2.32:27019",
  31.                 "10.1.2.68:27019",
  32.                 "10.1.2.175:27019",
  33.                 "10.1.2.183:27019"
  34.         ],
  35.         "setName" : "shard1",
  36.         "setVersion" : 4,
  37.         "ismaster" : true,
  38.         "secondary" : false,
  39.         "primary" : "10.1.2.32:27019",
  40.         "me" : "10.1.2.32:27019",
  41.         "electionId" : ObjectId("7fffffff0000000000000001"),
  42.         "lastWrite" : {
  43.                 "opTime" : {
  44.                         "ts" : Timestamp(1525752728, 3),
  45.                         "t" : NumberLong(1)
  46.                 },
  47.                 "lastWriteDate" : ISODate("2018-05-08T04:12:08Z"),
  48.                 "majorityOpTime" : {
  49.                         "ts" : Timestamp(1525752728, 3),
  50.                         "t" : NumberLong(1)
  51.                 },
  52.                 "majorityWriteDate" : ISODate("2018-05-08T04:12:08Z")
  53.         },
  54.         "maxBsonObjectSize" : 16777216,
  55.         "maxMessageSizeBytes" : 48000000,
  56.         "maxWriteBatchSize" : 100000,
  57.         "localTime" : ISODate("2018-05-08T04:12:12.282Z"),
  58.         "logicalSessionTimeoutMinutes" : 30,
  59.         "minWireVersion" : 0,
  60.         "maxWireVersion" : 6,
  61.         "readOnly" : false,
  62.         "ok" : 1,
  63.         "operationTime" : Timestamp(1525752728, 3),
  64.         "$gleStats" : {
  65.                 "lastOpTime" : {
  66.                         "ts" : Timestamp(1525752728, 3),
  67.                         "t" : NumberLong(1)
  68.                 },
  69.                 "electionId" : ObjectId("7fffffff0000000000000001")
  70.         },
  71.         "$clusterTime" : {
  72.                 "clusterTime" : Timestamp(1525752729, 3),
  73.                 "signature" : {
  74.                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
  75.                         "keyId" : NumberLong(0)
  76.                 }
  77.         },
  78.         "$configServerState" : {
  79.                 "opTime" : {
  80.                         "ts" : Timestamp(1525752729, 3),
  81.                         "t" : NumberLong(1)
  82.                 }
  83.         }
  84. }
  85. shard1:PRIMARY> rs.conf()
  86. {
  87.         "_id" : "shard1",
  88.         "version" : 4,
  89.         "protocolVersion" : NumberLong(1),
  90.         "members" : [
  91.                 {
  92.                         "_id" : 0,
  93.                         "host" : "10.1.2.32:27019",
  94.                         "arbiterOnly" : false,
  95.                         "buildIndexes" : true,
  96.                         "hidden" : false,
  97.                         "priority" : 1,
  98.                         "tags" : {

  99.                         },
  100.                         "slaveDelay" : NumberLong(0),
  101.                         "votes" : 1
  102.                 },
  103.                 {
  104.                         "_id" : 1,
  105.                         "host" : "10.1.2.68:27019",
  106.                         "arbiterOnly" : false,
  107.                         "buildIndexes" : true,
  108.                         "hidden" : false,
  109.                         "priority" : 1,
  110.                         "tags" : {

  111.                         },
  112.                         "slaveDelay" : NumberLong(0),
  113.                         "votes" : 1
  114.                 },
  115.                 {
  116.                         "_id" : 2,
  117.                         "host" : "10.1.2.175:27019",
  118.                         "arbiterOnly" : false,
  119.                         "buildIndexes" : true,
  120.                         "hidden" : false,
  121.                         "priority" : 1,
  122.                         "tags" : {

  123.                         },
  124.                         "slaveDelay" : NumberLong(0),
  125.                         "votes" : 1
  126.                 },
  127.                 {
  128.                         "_id" : 3,
  129.                         "host" : "10.1.2.183:27019",
  130.                         "arbiterOnly" : false,
  131.                         "buildIndexes" : true,
  132.                         "hidden" : false,
  133.                         "priority" : 1,
  134.                         "tags" : {

  135.                         },
  136.                         "slaveDelay" : NumberLong(0),
  137.                         "votes" : 1
  138.                 }
  139.         ],
  140.         "settings" : {
  141.                 "chainingAllowed" : true,
  142.                 "heartbeatIntervalMillis" : 2000,
  143.                 "heartbeatTimeoutSecs" : 10,
  144.                 "electionTimeoutMillis" : 10000,
  145.                 "catchUpTimeoutMillis" : -1,
  146.                 "catchUpTakeoverDelayMillis" : 30000,
  147.                 "getLastErrorModes" : {

  148.                 },
  149.                 "getLastErrorDefaults" : {
  150.                         "w" : 1,
  151.                         "wtimeout" : 0
  152.                 },
  153.                 "replicaSetId" : ObjectId("5aeab9557a9235efbe4a9b59")
  154.         }
  155. shard1:PRIMARY> rs.status()
    {
            "set" : "shard1",
            "date" : ISODate("2018-05-08T05:38:35.245Z"),
            "myState" : 1,
            "term" : NumberLong(1),
            "heartbeatIntervalMillis" : NumberLong(2000),
            "optimes" : {
                    "lastCommittedOpTime" : {
                            "ts" : Timestamp(1525757907, 1),
                            "t" : NumberLong(1)
                    },
                    "readConcernMajorityOpTime" : {
                            "ts" : Timestamp(1525757907, 1),
                            "t" : NumberLong(1)
                    },
                    "appliedOpTime" : {
                            "ts" : Timestamp(1525757907, 1),
                            "t" : NumberLong(1)
                    },
                    "durableOpTime" : {
                            "ts" : Timestamp(1525757907, 1),
                            "t" : NumberLong(1)
                    }
            },
            "members" : [
                    {
                            "_id" : 0,
                            "name" : "10.1.2.32:27019",
                            "health" : 1,
                            "state" : 1,
                            "stateStr" : "PRIMARY",
                            "uptime" : 425793,
                            "optime" : {
                                    "ts" : Timestamp(1525757907, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDate" : ISODate("2018-05-08T05:38:27Z"),
                            "electionTime" : Timestamp(1525332319, 1),
                            "electionDate" : ISODate("2018-05-03T07:25:19Z"),
                            "configVersion" : 4,
                            "self" : true
                    },
                    {
                            "_id" : 1,
                            "name" : "10.1.2.68:27019",
                            "health" : 1,
                            "state" : 2,
                            "stateStr" : "SECONDARY",
                            "uptime" : 425606,
                            "optime" : {
                                    "ts" : Timestamp(1525757907, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDurable" : {
                                    "ts" : Timestamp(1525757907, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDate" : ISODate("2018-05-08T05:38:27Z"),
                            "optimeDurableDate" : ISODate("2018-05-08T05:38:27Z"),
                            "lastHeartbeat" : ISODate("2018-05-08T05:38:34.014Z"),
                            "lastHeartbeatRecv" : ISODate("2018-05-08T05:38:34.014Z"),
                            "pingMs" : NumberLong(0),
                            "syncingTo" : "10.1.2.32:27019",
                            "configVersion" : 4
                    },
                    {
                            "_id" : 2,
                            "name" : "10.1.2.175:27019",
                            "health" : 1,
                            "state" : 2,
                            "stateStr" : "SECONDARY",
                            "uptime" : 425606,
                            "optime" : {
                                    "ts" : Timestamp(1525757907, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDurable" : {
                                    "ts" : Timestamp(1525757907, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDate" : ISODate("2018-05-08T05:38:27Z"),
                            "optimeDurableDate" : ISODate("2018-05-08T05:38:27Z"),
                            "lastHeartbeat" : ISODate("2018-05-08T05:38:34.014Z"),
                            "lastHeartbeatRecv" : ISODate("2018-05-08T05:38:34.016Z"),
                            "pingMs" : NumberLong(0),
                            "syncingTo" : "10.1.2.183:27019",
                            "configVersion" : 4
                    },
                    {
                            "_id" : 3,
                            "name" : "10.1.2.183:27019",
                            "health" : 1,
                            "state" : 2,
                            "stateStr" : "SECONDARY",
                            "uptime" : 5184,
                            "optime" : {
                                    "ts" : Timestamp(1525757907, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDurable" : {
                                    "ts" : Timestamp(1525757907, 1),
                                    "t" : NumberLong(1)
                            },
                            "optimeDate" : ISODate("2018-05-08T05:38:27Z"),
                            "optimeDurableDate" : ISODate("2018-05-08T05:38:27Z"),
                            "lastHeartbeat" : ISODate("2018-05-08T05:38:34.016Z"),
                            "lastHeartbeatRecv" : ISODate("2018-05-08T05:38:33.567Z"),
                            "pingMs" : NumberLong(0),
                            "syncingTo" : "10.1.2.68:27019",
                            "configVersion" : 4
                    }
            ],
            "ok" : 1,
            "operationTime" : Timestamp(1525757907, 1),
            "$gleStats" : {
                    "lastOpTime" : Timestamp(0, 0),
                    "electionId" : ObjectId("7fffffff0000000000000001")
            },
            "$clusterTime" : {
                    "clusterTime" : Timestamp(1525757911, 2),
                    "signature" : {
                            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                            "keyId" : NumberLong(0)
                    }
            },
            "$configServerState" : {
                    "opTime" : {
                            "ts" : Timestamp(1525757911, 2),
                            "t" : NumberLong(1)
                    }
            }
    }

可以看到新的成员已经加入复制集中去了

3.shard2  config 加成员同理(过程略)


4.检查是否加入成功
  1. 登陆mongos端口可以看到
  2. [root@ip-10-1-2-32 etc]# /usr/local/mongodb/bin/mongo 10.1.2.32:30000
  3. MongoDB shell version v3.6.4
  4. connecting to: mongodb://10.1.2.32:30000/test
  5. MongoDB server version: 3.6.4
  6. Server has startup warnings:
  7. 2018-05-03T07:47:09.379+0000 I CONTROL [main]
  8. 2018-05-03T07:47:09.379+0000 I CONTROL [main] ** WARNING: Access control is not enabled for the database.
  9. 2018-05-03T07:47:09.379+0000 I CONTROL [main] ** Read and write access to data and configuration is unrestricted.
  10. 2018-05-03T07:47:09.379+0000 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
  11. 2018-05-03T07:47:09.379+0000 I CONTROL [main]
  12. mongos> db.printShardingStatus();
  13. --- Sharding Status ---
  14.   sharding version: {
  15.         "_id" : 1,
  16.         "minCompatibleVersion" : 5,
  17.         "currentVersion" : 6,
  18.         "clusterId" : ObjectId("5aeabc4db192a4fefca1c888")
  19.   }
  20.   shards:
  21.         { "_id" : "shard1", "host" : "shard1/10.1.2.175:27019,10.1.2.183:27019,10.1.2.32:27019,10.1.2.68:27019", "state" : 1 }   ---新的复制集成员自动同步到分片信息了
  22.         { "_id" : "shard2", "host" : "shard2/10.1.2.175:27018,10.1.2.32:27018,10.1.2.68:27018", "state" : 1 }
  23.         { "_id" : "shard3", "host" : "shard3/10.1.2.175:27017,10.1.2.32:27017,10.1.2.68:27017", "state" : 1 }
  24.   active mongoses:
  25.         "3.6.4" : 3
  26.   autosplit:
  27.         Currently enabled: yes
  28.   balancer:
  29.         Currently enabled: yes
  30.         Currently running: no
  31.         Failed balancer rounds in last 5 attempts: 0
  32.         Migration Results for the last 24 hours:
  33.                 No recent migrations
  34.   databases:
  35.         { "_id" : "config", "primary" : "config", "partitioned" : true }
  36.                 config.system.sessions
  37.                         shard key: { "_id" : 1 }
  38.                         unique: false
  39.                         balancing: true
  40.                         chunks:
  41.                                 shard1 1
  42.                         { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
  43.         { "_id" : "test", "primary" : "shard2", "partitioned" : true }
  44.                 test.users
  45.                         shard key: { "_id" : 1 }
  46.                         unique: false
  47.                         balancing: true
  48.                         chunks:
  49.                                 shard2 1
  50.                         { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0)

  51. mongos> ;

4.最后修改下配置文件
  1. 避免重启问题,写入配置文件
  2. [root@ip-10-1-2-32 etc]# more mongos.conf
  3. logpath=/usr/local/mongodb/logs/mongos.log
  4. logappend=true # need logrotae scripts
  5. fork=true
  6. port=30000
  7. pidfilepath=/usr/local/mongodb/logs/mongos.pid
  8. configdb=conf/10.1.2.32:27000,10.1.2.68:27000,10.1.2.175:27000,10.1.2.183:27000
  9. bind_ip=10.1.2.32



网站栏目:【Mongodb】往分片复制集添加复制成员
浏览路径:http://cdweb.net/article/gispig.html