【Mongodb】分片加複製集

小亮520cl發表於2016-10-09

mongo分片加複製集配置檔案如下:

所有的機器上都有

  1. shard1配置檔案

  1. [root@ip-10-1-2-32 etc]# more shard1.conf 
  2. logpath=/usr/local/mongodb/logs/mongo_shard1.log
    logappend=true # need logrotae scripts
    fork=true
    journal=true
    port=27019
    #vvvvv = true
    #diaglog = 3
    dbpath=/usr/local/mongodb/shard1
    pidfilepath=/usr/local/mongodb/logs/mongo_shard1.pid
    bind_ip=10.1.2.32
    replSet=shard1
    shardsvr=true


shard2配置檔案
  1. [root@ip-10-1-2-32 etc]# more shard2.conf 
    logpath=/usr/local/mongodb/logs/mongo_shard2.log
    logappend=true # need logrotae scripts
    fork=true
    journal=true
    port=27018
    #vvvvv = true
    #diaglog = 3
    dbpath=/usr/local/mongodb/shard2
    pidfilepath=/usr/local/mongodb/logs/mongo_shard2.pid
    bind_ip=10.1.2.32
    replSet=shard2
    shardsvr=true


Config Server配置檔案
  1. [root@ip-10-1-2-32 etc]# more config.conf 
    logpath=/usr/local/mongodb/logs/mongo_config.log
    logappend=true # need logrotae scripts
    fork=true
    journal=true
    port=27000
    dbpath=/usr/local/mongodb/config
    pidfilepath=/usr/local/mongodb/logs/mongo_config.pid
    configsvr=true
    replSet=conf
    bind_ip=10.1.2.32


Route Process配置檔案
  1. [root@ip-10-1-2-32 etc]# more mongos.conf 
    logpath=/usr/local/mongodb/logs/mongos.log
    logappend=true # need logrotae scripts
    fork=true
    port=30000
    pidfilepath=/usr/local/mongodb/logs/mongos.pid
    configdb=conf/10.1.2.32:27000,10.1.2.68:27000,10.1.2.175:27000
    bind_ip=10.1.2.32

  2. (3.2開始的寫法,必須指定config的複製集)
 (3.2之前的寫法)
  1. ##configdb=10.1.2.32:27000,10.1.2.68:27000,10.1.2.175:27000

啟動
  1.  /usr/local/mongodb/bin/mongod -f shard1.conf
    1. 搭建shard1的複製集
    2. /usr/local/mongodb/bin/mongo 10.1.2.32:27019
    3. > config_rs1 = {_id: 'shard1', members: [{_id: 0, host: '10.1.2.32:27019', priority:1}, {_id: 1, host: '10.1.2.68:27019'},{_id: 2, host: '10.1.2.175:27019'}]}
    4. {
    5.         "_id" : "shard1",
    6.         "members" : [
    7.                 {
    8.                         "_id" : 0,
    9.                         "host" : "10.1.2.32:27019",
    10.                         "priority" : 1
    11.                 },
    12.                 {
    13.                         "_id" : 1,
    14.                         "host" : "10.1.2.68:27019"
    15.                 },
    16.                 {
    17.                         "_id" : 2,
    18.                         "host" : "10.1.2.175:27019"
    19.                 }
    20.         ]
    21. }
    22. > rs.initiate(config_rs1);
    23. { "ok" : 1 }




  1.  /usr/local/mongodb/bin/mongod -f shard2.conf
    1. 搭建shard2的複製集
    2. /usr/local/mongodb/bin/mongo 10.1.2.32:27018
    3. > config_rs2 = {_id: 'shard2', members: [{_id: 0, host: '10.1.2.32:27018', priority:1}, {_id: 1, host: '10.1.2.68:27018'},{_id: 2, host: '10.1.2.175:27018'}]}
    4. {
    5.         "_id" : "shard2",
    6.         "members" : [
    7.                 {
    8.                         "_id" : 0,
    9.                         "host" : "10.1.2.32:27018",
    10.                         "priority" : 1
    11.                 },
    12.                 {
    13.                         "_id" : 1,
    14.                         "host" : "10.1.2.68:27018"
    15.                 },
    16.                 {
    17.                         "_id" : 2,
    18.                         "host" : "10.1.2.175:27018"
    19.                 }
    20.         ]
    21. }
    22. > rs.initiate(config_rs2);
    23. { "ok" : 1 }



  2.  /usr/local/mongodb/bin/mongod -f config.conf
    1. 搭建config的複製集:
    2. /usr/local/mongodb/bin/mongo 10.1.2.32:27000
    3. > config_rep = {_id: 'conf', members: [{_id: 0, host: '10.1.2.32:27000', priority:1}, {_id: 1, host: '10.1.2.68:27000'},{_id: 2, host: '10.1.2.175:27000'}]}
    4. {
    5.         "_id" : "conf",
    6.         "members" : [
    7.                 {
    8.                         "_id" : 0,
    9.                         "host" : "10.1.2.32:27000",
    10.                         "priority" : 1
    11.                 },
    12.                 {
    13.                         "_id" : 1,
    14.                         "host" : "10.1.2.68:27000"
    15.                 },
    16.                 {
    17.                         "_id" : 2,
    18.                         "host" : "10.1.2.175:27000"
    19.                 }
    20.         ]
    21. }
    22. > rs.initiate(config_rep);
    23. {
    24.         "ok" : 1,
    25.         "operationTime" : Timestamp(1525333057, 1),
    26.         "$gleStats" : {
    27.                 "lastOpTime" : Timestamp(1525333057, 1),
    28.                 "electionId" : ObjectId("000000000000000000000000")
    29.         },
    30.         "$clusterTime" : {
    31.                 "clusterTime" : Timestamp(1525333057, 1),
    32.                 "signature" : {
    33.                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    34.                         "keyId" : NumberLong(0)
    35.                 }
    36.         }
    37. }


  3.  /usr/local/mongodb/bin/mongos -f mongos.conf
    1. 新增分片設定
    2. /usr/local/mongodb/bin/mongo 10.1.2.32:30000
    3. mongos> db.runCommand({ addshard: 'shard1/10.1.2.32:27019,10.1.2.68:27019,10.1.2.175:27019'})
    4. {
    5.         "shardAdded" : "shard1",
    6.         "ok" : 1,
    7.         "$clusterTime" : {
    8.                 "clusterTime" : Timestamp(1525333858, 7),
    9.                 "signature" : {
    10.                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    11.                         "keyId" : NumberLong(0)
    12.                 }
    13.         },
    14.         "operationTime" : Timestamp(1525333858, 7)
    15. }
    16. mongos> db.runCommand({ addshard: 'shard2/10.1.2.32:27018,10.1.2.68:27018,10.1.2.175:27018'})
    17. {
    18.         "shardAdded" : "shard2",
    19.         "ok" : 1,
    20.         "$clusterTime" : {
    21.                 "clusterTime" : Timestamp(1525333888, 4),
    22.                 "signature" : {
    23.                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    24.                         "keyId" : NumberLong(0)
    25.                 }
    26.         },
    27.         "operationTime" : Timestamp(1525333888, 4)
    28. }
    29. mongos> db.runCommand({ enablesharding: 'test'})
    30. {
    31.         "ok" : 1,
    32.         "$clusterTime" : {
    33.                 "clusterTime" : Timestamp(1525333913, 7),
    34.                 "signature" : {
    35.                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    36.                         "keyId" : NumberLong(0)
    37.                 }
    38.         },
    39.         "operationTime" : Timestamp(1525333913, 7)
    40. }




1.只想做複製集

  1. 三臺機器配置如下

  1. [root@mysql02 yum.repos.d]# more /home/mongodb/etc/config.cnf
  2. logpath=/home/mongodb/logs/mongo_config.log
  3. logappend=true # need logrotae scripts
  4. fork=true
  5. journal=true
  6. port=27000
  7. dbpath=/home/mongodb/config
  8. pidfilepath=/home/mongodb/logs/mongo_config.pid
  9. replSet=replset      ---複製集名稱
 

 選取第一臺主進行配置
  1. 注意:這個id一定要與上面配置檔案的複製集名稱一樣
  2. > config_replset = {_id: 'replset', members: [ {_id: 0, host: '10.168.17.201:27000', priority:1}, {_id: 1, host: '10.168.17.202:27000'}, {_id: 2, host: '10.168.17.203:27000'}] }
  3. {
  4.         "_id" : "replset",
  5.         "members" : [
  6.                 {
  7.                         "_id" : 0,
  8.                         "host" : "10.168.17.201:27000",
  9.                         "priority" : 1
  10.                 },
  11.                 {
  12.                         "_id" : 1,
  13.                         "host" : "10.168.17.202:27000"
  14.                 },
  15.                 {
  16.                         "_id" : 2,
  17.                         "host" : "10.168.17.203:27000"
  18.                 }
  19.         ]
  20. }

初始化並檢視是否成功
> rs.initiate(config_replset)
{ "ok" : 1 }
replset:OTHER> rs.status()
{
        "set" : "replset",
        "date" : ISODate("2018-04-11T03:46:23.680Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.168.17.201:27000",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 575,
                        "optime" : {
                                "ts" : Timestamp(1523418353, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-11T03:45:53Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1523418352, 1),
                        "electionDate" : ISODate("2018-04-11T03:45:52Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "10.168.17.202:27000",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 41,
                        "optime" : {
                                "ts" : Timestamp(1523418353, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-11T03:45:53Z"),
                        "lastHeartbeat" : ISODate("2018-04-11T03:46:22.768Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-11T03:46:23.221Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.168.17.201:27000",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "10.168.17.203:27000",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 41,
                        "optime" : {
                                "ts" : Timestamp(1523418353, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-11T03:45:53Z"),
                        "lastHeartbeat" : ISODate("2018-04-11T03:46:22.768Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-11T03:46:23.221Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "10.168.17.201:27000",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
replset:PRIMARY> rs.isMaster()
{
        "hosts" : [
                "10.168.17.201:27000",
                "10.168.17.202:27000",
                "10.168.17.203:27000"
        ],
        "setName" : "replset",
        "setVersion" : 1,
        "ismaster" : true,
        "secondary" : false,
        "primary" : "10.168.17.201:27000",
        "me" : "10.168.17.201:27000",
        "electionId" : ObjectId("7fffffff0000000000000001"),
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 1000,
        "localTime" : ISODate("2018-04-11T03:46:39.233Z"),
        "maxWireVersion" : 4,
        "minWireVersion" : 0,
        "ok" : 1
}



2.只想做分片
  1. 一臺機器上做分片配置即可
  2. [root@mysql04 etc]# ll
  3. 總用量 16
  4. -rw-r--r-- 1 root root 208 4月 11 13:42 config.cnf
  5. -rw-r--r-- 1 root root 169 4月 11 13:48 mongos.cnf
  6. -rw-r--r-- 1 root root 227 4月 11 13:41 shard1.cnf
  7. -rw-r--r-- 1 root root 227 4月 11 13:42 shard2.cnf
  8. [root@mysql04 etc]# more *.cnf
  9. ::::::::::::::
  10. config.cnf
  11. ::::::::::::::
  12. logpath=/home/mongodb/logs/mongo_config.log
  13. logappend=true # need logrotae scripts
  14. fork=true
  15. journal=true
  16. port=27000
  17. dbpath=/home/mongodb/config
  18. pidfilepath=/home/mongodb/logs/mongo_config.pid
  19. configsvr=true
  20. ::::::::::::::
  21. mongos.cnf
  22. ::::::::::::::
  23. logpath=/home/mongodb/logs/mongos.log
  24. logappend=true # need logrotae scripts
  25. fork=true
  26. port=30000
  27. pidfilepath=/home/mongodb/logs/mongos.pid
  28. configdb=10.168.17.204:27000
  29. ::::::::::::::
  30. shard1.cnf
  31. ::::::::::::::
  32. logpath=/home/mongodb/logs/mongo_shard1.log
  33. logappend=true # need logrotae scripts
  34. fork=true
  35. journal=true
  36. port=27019
  37. dbpath=/home/mongodb/shard1
  38. pidfilepath=/home/mongodb/logs/mongo_shard1.pid
  39. shardsvr=true
  40. directoryperdb=true
  41. ::::::::::::::
  42. shard2.cnf
  43. ::::::::::::::
  44. logpath=/home/mongodb/logs/mongo_shard2.log
  45. logappend=true # need logrotae scripts
  46. fork=true
  47. journal=true
  48. port=27018
  49. dbpath=/home/mongodb/shard2
  50. pidfilepath=/home/mongodb/logs/mongo_shard2.pid
  51. shardsvr=true
  52. directoryperdb=true


和分片加複製集的區別不大!


mongodb 執行時新增新的分片複製集

http://blog.csdn.net/mchdba/article/details/51867303

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2125975/,如需轉載,請註明出處,否則將追究法律責任。

相關文章