千家信息网

【Mongodb】分片复制集环境添加新的分片

发表于:2024-11-20 作者:千家信息网编辑
千家信息网最后更新 2024年11月20日,1.每个节点上分别创建新的shard3配置文件[root@ip-10-1-2-68 etc]# more shard3.conflogpath=/usr/local/mongodb/logs/mong
千家信息网最后更新 2024年11月20日【Mongodb】分片复制集环境添加新的分片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. mongos> sh.setBalancerState(true);

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


参考:https://docs.mongodb.com/manual/reference/method/sh.getBalancerState/





0