【Mongodb】分片複製集環境新增新的分片

小亮520cl發表於2018-05-08
1.每個節點上分別建立新的shard3配置檔案
  1. [root@ip-10-1-2-68 etc]# more shard3.conf
  2. logpath=/usr/local/mongodb/logs/mongo_shard3.log
  3. logappend=true # need logrotae scripts
  4. fork=true
  5. journal=true
  6. port=27017
  7. #vvvvv = true
  8. #diaglog = 3
  9. dbpath=/usr/local/mongodb/shard3
  10. pidfilepath=/usr/local/mongodb/logs/mongo_shard3.pid
  11. bind_ip=10.1.2.68    ---bind ip 修改為自己的機器
  12. replSet=shard3
  13. shardsvr=true

2. 每個節點啟動shard3 服務
  1. [root@ip-10-1-2-68 etc]# /usr/local/mongodb/bin/mongod -f shard3.conf

3.登陸第一個節點,搭建shard3的複製集
  1. [root@ip-10-1-2-32 etc]# /usr/local/mongodb/bin/mongo 10.1.2.32:27017
  2. MongoDB shell version v3.6.4
  3. connecting to: mongodb://10.1.2.32:27017/test
  4. MongoDB server version: 3.6.4
  5. Server has startup warnings:
  6. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
  7. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
  8. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
  9. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
  10. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
  11. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
  12. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
  13. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
  14. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
  15. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
  16. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
  17. 2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
  18. > config_rs3 = {_id: 'shard3', members: [{_id: 0, host: '10.1.2.32:27017', priority:1}, {_id: 1, host: '10.1.2.68:27017'},{_id: 2, host: '10.1.2.175:27017'}]}
  19. {
  20.         "_id" : "shard3",
  21.         "members" : [
  22.                 {
  23.                         "_id" : 0,
  24.                         "host" : "10.1.2.32:27017",
  25.                         "priority" : 1
  26.                 },
  27.                 {
  28.                         "_id" : 1,
  29.                         "host" : "10.1.2.68:27017"
  30.                 },
  31.                 {
  32.                         "_id" : 2,
  33.                         "host" : "10.1.2.175:27017"
  34.                 }
  35.         ]
  36. }
  37. > rs.initiate(config_rs3);
  38. { "ok" : 1 }

4.登陸第一個節點的mongs埠,將shard3分片加入分片組
  1. [root@ip-10-1-2-32 etc]# /usr/local/mongodb/bin/mongo 10.1.2.32:30000
  2. MongoDB shell version v3.6.4
  3. connecting to: mongodb://10.1.2.32:30000/test
  4. MongoDB server version: 3.6.4
  5. Server has startup warnings:
  6. 2018-05-03T07:47:09.379+0000 I CONTROL [main]
  7. 2018-05-03T07:47:09.379+0000 I CONTROL [main] ** WARNING: Access control is not enabled for the database.
  8. 2018-05-03T07:47:09.379+0000 I CONTROL [main] ** Read and write access to data and configuration is unrestricted.
  9. 2018-05-03T07:47:09.379+0000 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
  10. 2018-05-03T07:47:09.379+0000 I CONTROL [main]

  11. mongos> use admin;
  12. switched to db admin
  13. mongos> db.runCommand({ addshard: 'shard3/10.1.2.32:27017,10.1.2.68:27017,10.1.2.175:27017'})
  14. {
  15.         "shardAdded" : "shard3",
  16.         "ok" : 1,
  17.         "$clusterTime" : {
  18.                 "clusterTime" : Timestamp(1525750332, 7),
  19.                 "signature" : {
  20.                         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
  21.                         "keyId" : NumberLong(0)
  22.                 }
  23.         },
  24.         "operationTime" : Timestamp(1525750332, 7)
  25. }
  26. mongos> db.printShardingStatus(); ---檢視新的分片資訊
  27. --- Sharding Status ---
  28.   sharding version: {
  29.         "_id" : 1,
  30.         "minCompatibleVersion" : 5,
  31.         "currentVersion" : 6,
  32.         "clusterId" : ObjectId("5aeabc4db192a4fefca1c888")
  33.   }
  34.   shards:
  35.         { "_id" : "shard1", "host" : "shard1/10.1.2.175:27019,10.1.2.32:27019,10.1.2.68:27019", "state" : 1 }
  36.         { "_id" : "shard2", "host" : "shard2/10.1.2.175:27018,10.1.2.32:27018,10.1.2.68:27018", "state" : 1 }
  37.         { "_id" : "shard3", "host" : "shard3/10.1.2.175:27017,10.1.2.32:27017,10.1.2.68:27017", "state" : 1 }
  38.   active mongoses:
  39.         "3.6.4" : 3
  40.   autosplit:
  41.         Currently enabled: yes
  42.   balancer:
  43.         Currently enabled: yes
  44.         Currently running: no
  45.         Failed balancer rounds in last 5 attempts: 0
  46.         Migration Results for the last 24 hours:
  47.                 No recent migrations
  48.   databases:
  49.         { "_id" : "config", "primary" : "config", "partitioned" : true }
  50.                 config.system.sessions
  51.                         shard key: { "_id" : 1 }
  52.                         unique: false
  53.                         balancing: true
  54.                         chunks:
  55.                                 shard1 1
  56.                         { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
  57.         { "_id" : "test", "primary" : "shard2", "partitioned" : true }
  58.                 test.users
  59.                         shard key: { "_id" : 1 }
  60.                         unique: false
  61.                         balancing: true
  62.                         chunks:
  63.                                 shard2 1
  64.                         { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0)


5、設定均衡器
  1. # 檢視狀態

  2. mongos> sh.isBalancerRunning();

  3. false


  4.  

  5. # 設定均衡器

  6. mongos> sh.setBalancerState(true);

  7. mongos> sh.enableBalancing("bg")
  8. mongos> sh.getBalancerState()
    true


參考:





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

相關文章