• windows环境下mongodb 5.0.9分片集群环境搭建


    mongodb分片集群搭建包含三种角色:

    1. 配置服务器,负责整个集群的配置管理;

    2. 分片服务器,负责处理分片内的数据;

    3. 路由服务器,负责接收请求,并转发给合适的分片服务器进行业务处理;

    1. 安装包下载地址

    MongoDB Community Download | MongoDB

    这里下载压缩版,不选择msi版本。

    将压缩包解压后,拷贝10份,分别命名目录为:

     

     mongo-config是配置服务器,后面的数字27018,27019,27020是端口号。

    mongo-router是路由服务器,后面的数字27024是端口号。

    mongo-shard是分片服务器,后面的数字是端口号,其中27021,27022,27023作为一个副本集,27031,27032,27033作为另外一个副本集。

    2. 配置服务器部署

    以管理员打开命令行窗口,切换到配置服务器目录下,新建data目录和log目录,在bin目录下创建配置文件mongod.cfg。

    在mongo-config-27018下的bin目录下的配置文件mongod.cfg中添加如下内容:

    1. storage:
    2. dbPath: F:\Apps\mongo-cluster\mongo-config-27018\data
    3. journal:
    4. enabled: true
    5. # engine:
    6. # mmapv1:
    7. # wiredTiger:
    8. # where to write logging data.
    9. systemLog:
    10. destination: file
    11. logAppend: true
    12. path: F:\Apps\mongo-cluster\mongo-config-27018\log\mongod.log
    13. # network interfaces
    14. net:
    15. port: 27018
    16. bindIp: 127.0.0.1
    17. #processManagement:
    18. #security:
    19. #operationProfiling:
    20. replication:
    21. replSetName: 'mongodb-test'
    22. sharding:
    23. clusterRole: configsvr

    执行安装服务命令:

    1. mongod --config F:\Apps\mongo-cluster\mongo-config-27018\bin\mongod.cfg --serviceName "MongoDB27018" --serviceDisplayName "MongoDB27018" --install
    2. mongod --config F:\Apps\mongo-cluster\mongo-config-27019\bin\mongod.cfg --serviceName "MongoDB27019" --serviceDisplayName "MongoDB27019" --install
    3. mongod --config F:\Apps\mongo-cluster\mongo-config-27020\bin\mongod.cfg --serviceName "MongoDB27020" --serviceDisplayName "MongoDB27020" --install

    执行启动服务命令:

    1. net start MongoDB27018
    2. net start MongoDB27019
    3. net start MongoDB27020

     测试配置服务可用性:

    1. mongo --host 127.0.0.1 --port 27018
    2. mongo --host 127.0.0.1 --port 27019
    3. mongo --host 127.0.0.1 --port 27020

     mongo-config-27019,mongo-config-27020参考上面的方式进行配置和启动,完成后添加副本集。

    添加副本集

    1. rs.initiate({
    2. _id: 'mongodb-test',
    3. configsvr: true,
    4. members: [
    5. {_id: 0, host: '127.0.0.1:27018'},
    6. {_id: 1, host: '127.0.0.1:27019'},
    7. {_id: 2, host: '127.0.0.1:27020'}]
    8. })

    3. 分片服务器部署

    以管理员打开命令行窗口,切换到分片服务器目录下,新建data目录和log目录,在bin目录下创建配置文件mongo.config。

    在mongo-config-27021下的bin目录下的配置文件mongo.config中添加如下内容:

    1. # 数据保存
    2. storage:
    3. dbPath: F:\Apps\mongo-cluster\mongo-shard-27021\data
    4. journal:
    5. enabled: true
    6. # engine:
    7. # mmapv1:
    8. # wiredTiger:
    9. # 日志保存到哪
    10. systemLog:
    11. destination: file
    12. logAppend: true
    13. path: F:\Apps\mongo-cluster\mongo-shard-27021\log\mongod.log
    14. # 绑定的IP和端口号
    15. net:
    16. port: 27021
    17. bindIp: 127.0.0.1
    18. # 复制集名称
    19. replication:
    20. replSetName: 'test-shards'
    21. # 复制集的作用:是一个分片服务器
    22. sharding:
    23. clusterRole: shardsvr

    在mongo-config-27031下的bin目录下的配置文件mongo.config中添加如下内容:

    1. # 数据保存
    2. storage:
    3. dbPath: F:\Apps\mongo-cluster\mongo-shard-27031\data
    4. journal:
    5. enabled: true
    6. # engine:
    7. # mmapv1:
    8. # wiredTiger:
    9. # 日志保存到哪
    10. systemLog:
    11. destination: file
    12. logAppend: true
    13. path: F:\Apps\mongo-cluster\mongo-shard-27031\log\mongod.log
    14. # 绑定的IP和端口号
    15. net:
    16. port: 27031
    17. bindIp: 127.0.0.1
    18. # 复制集名称
    19. replication:
    20. replSetName: 'demo-shards'
    21. # 复制集的作用:是一个分片服务器
    22. sharding:
    23. clusterRole: shardsvr

    执行安装服务命令:

    1. mongod --config F:\Apps\mongo-cluster\mongo-shard-27021\bin\mongo.config --serviceName "MongoDB27021" --serviceDisplayName "MongoDB27021" --install
    2. mongod --config F:\Apps\mongo-cluster\mongo-shard-27022\bin\mongo.config --serviceName "MongoDB27022" --serviceDisplayName "MongoDB27022" --install
    3. mongod --config F:\Apps\mongo-cluster\mongo-shard-27023\bin\mongo.config --serviceName "MongoDB27023" --serviceDisplayName "MongoDB27023" --install
    4. mongod --config F:\Apps\mongo-cluster\mongo-shard-27031\bin\mongo.config --serviceName "MongoDB27031" --serviceDisplayName "MongoDB27031" --install
    5. mongod --config F:\Apps\mongo-cluster\mongo-shard-27032\bin\mongo.config --serviceName "MongoDB27032" --serviceDisplayName "MongoDB27032" --install
    6. mongod --config F:\Apps\mongo-cluster\mongo-shard-27033\bin\mongo.config --serviceName "MongoDB27033" --serviceDisplayName "MongoDB27033" --install

    启动服务

    1. net start MongoDB27021
    2. net start MongoDB27022
    3. net start MongoDB27023
    4. net start MongoDB27031
    5. net start MongoDB27032
    6. net start MongoDB27033

    测试分片服务可用性:

    1. mongo --host 127.0.0.1 --port 27021
    2. mongo --host 127.0.0.1 --port 27022
    3. mongo --host 127.0.0.1 --port 27023
    4. mongo --host 127.0.0.1 --port 27031
    5. mongo --host 127.0.0.1 --port 27032
    6. mongo --host 127.0.0.1 --port 27033

    或者下面这种方式: 

    1. rs.initiate({
    2. _id: 'test-shards',
    3. members: [
    4. {_id: 0, host: '127.0.0.1:27021'},
    5. {_id: 1, host: '127.0.0.1:27022'},
    6. {_id: 2, host: '127.0.0.1:27023'}]
    7. })
    8. rs.initiate({
    9. _id: 'demo-shards',
    10. members: [
    11. {_id: 0, host: '127.0.0.1:27021'},
    12. {_id: 1, host: '127.0.0.1:27022'},
    13. {_id: 2, host: '127.0.0.1:27023'}]
    14. })

    4. 路由服务器部署

    以管理员打开命令行窗口,切换到分片服务器目录下,新建data目录和log目录,在bin目录下创建配置文件mongo.config。

    在mongo-config-27024下的bin目录下的配置文件mongo.config中添加如下内容:

    1. # 日志保存
    2. systemLog:
    3. destination: file
    4. logAppend: true
    5. path: F:\Apps\mongo-cluster\mongo-router-27024\log\mongod.log
    6. # 绑定的IP和端口号
    7. net:
    8. port: 27024
    9. bindIp: 127.0.0.1
    10. # 配置服务器地址
    11. sharding:
    12. configDB: mongodb-test/127.0.0.1:27018,127.0.0.1:27019,127.0.0.1:27020

    注册路由服务

    mongos  --config F:\Apps\mongo-cluster\mongo-router-27024\bin\mongo.config --serviceName "MongoDB27024" --serviceDisplayName "MongoDB27024"  --install
    

    启动服务

    net start MongoDB27024

    测试服务

    mongo --host 127.0.0.1 --port 27024

    添加分片副本服务器:

    1. sh.addShard( "test-shards/127.0.0.1:27021")
    2. sh.addShard( "test-shards/127.0.0.1:27022")
    3. sh.addShard( "test-shards/127.0.0.1:27023")
    4. sh.addShard( "demo-shards/127.0.0.1:27031")
    5. sh.addShard( "demo-shards/127.0.0.1:27032")
    6. sh.addShard( "demo-shards/127.0.0.1:27033")

    5. 配置数据库分片

    给指定库demo开启分片:

    sh.enableSharding("demo")

    分片配置:

    1. > sh.shardCollection("demo.user",{'age':1})
    2. {
    3. "collectionsharded" : "demo.user",
    4. "ok" : 1,
    5. "$clusterTime" : {
    6. "clusterTime" : Timestamp(1655968571, 5),
    7. "signature" : {
    8. "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    9. "keyId" : NumberLong(0)
    10. }
    11. },
    12. "operationTime" : Timestamp(1655968570, 8)
    13. }
    14. > sh.shardCollection("demo.user",{'name':'hashed'})
    15. {
    16. "ok" : 0,
    17. "errmsg" : "sharding already enabled for collection demo.user",
    18. "code" : 23,
    19. "codeName" : "AlreadyInitialized",
    20. "$clusterTime" : {
    21. "clusterTime" : Timestamp(1655968744, 10),
    22. "signature" : {
    23. "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    24. "keyId" : NumberLong(0)
    25. }
    26. },
    27. "operationTime" : Timestamp(1655968744, 6)
    28. }

    查看状态:

    1. mongos> sh.status()
    2. --- Sharding Status ---
    3. sharding version: {
    4. "_id" : 1,
    5. "minCompatibleVersion" : 5,
    6. "currentVersion" : 6,
    7. "clusterId" : ObjectId("62b409460f7f8d24eb00ed39")
    8. }
    9. shards:
    10. { "_id" : "demo-shards", "host" : "demo-shards/127.0.0.1:27031,127.0.0.1:27032,127.0.0.1:27033", "state" : 1, "topologyTime" : Timestamp(1655975907, 1) }
    11. { "_id" : "test-shards", "host" : "test-shards/127.0.0.1:27021,127.0.0.1:27022,127.0.0.1:27023", "state" : 1, "topologyTime" : Timestamp(1655968292, 2) }
    12. active mongoses:
    13. "5.0.9" : 1
    14. autosplit:
    15. Currently enabled: yes
    16. balancer:
    17. Currently enabled: yes
    18. Currently running: no
    19. Failed balancer rounds in last 5 attempts: 0
    20. Migration results for the last 24 hours:
    21. 512 : Success
    22. databases:
    23. { "_id" : "config", "primary" : "config", "partitioned" : true }
    24. config.system.sessions
    25. shard key: { "_id" : 1 }
    26. unique: false
    27. balancing: true
    28. chunks:
    29. demo-shards 512
    30. test-shards 512
    31. too many chunks to print, use verbose if you want to force print
    32. { "_id" : "demo", "primary" : "test-shards", "partitioned" : true, "version" : { "uuid" : UUID("fd13df1e-7bb6-41f5-93aa-2d026a595a54"), "timestamp" : Timestamp(1655968493, 1), "lastMod" : 1 } }
    33. demo.user
    34. shard key: { "age" : 1 }
    35. unique: false
    36. balancing: true
    37. chunks:
    38. test-shards 1
    39. { "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } } on : test-shards Timestamp(1, 0)
    40. { "_id" : "mongo-shard-demo", "primary" : "test-shards", "partitioned" : false, "version" : { "uuid" : UUID("d3dab826-da91-4634-888f-bb289de3de58"), "timestamp" : Timestamp(1655969219, 1), "lastMod" : 1 } }
    41. { "_id" : "test", "primary" : "demo-shards", "partitioned" : false, "version" : { "uuid" : UUID("e6d52d9e-a565-49d8-9bc7-b656d1068698"), "timestamp" : Timestamp(1655977282, 8), "lastMod" : 1 } }

    可以看到已经有两个分片副本集demo-shards和test-shards。

    6. 服务删除方法

    如果服务添加错了,需要删除服务时,可以使用下面的命令。

    1. #停止服务运行
    2. net Stop ServiceName
    3. #删除服务
    4. sc delete ServiceName

    7. 创建数据库并开启分片的过程

    1. #数据库 启用 分片
    2. sh.enableSharding("mongodbDemo")
    3. # _id 字段进行哈希分片:
    4. sh.shardCollection("mongodbDemo.order", {"_id": "hashed" })
    5. #刷新路由
    6. db.adminCommand("flushRouterConfig")
    7. #让当前分片支持平衡
    8. sh.enableBalancing("mongodbDemo.order")
    9. #开启平衡
    10. sh.startBalancer()
    11. #查看详细分片信息
    12. sh.status({"verbose":1})
    13. #插入数据
    14. use mongodbDemo
    15. for (i = 1;i <= 100;i=i+1){
    16. db.order.insert({'price': 1})
    17. }
    18. #查看该表分片数据信息
    19. db.order.getShardDistribution()

    综合上述可知,如果需要使用mongodb的分片功能,首先需要搭建分片集群,只有在集群环境下,才能使用分片功能,其次,还需要开启数据库的分片功能,并设置好分片键,默认情况下,数据库是不进行分片的。

  • 相关阅读:
    C++入门学习1-Clion配置环境和运行第一个C++程序(Clion)
    Rsync分布式应用
    【数据结构】B : DS图应用--最短路径
    GO语言实战之嵌入类型和属性隐私定义
    【学习笔记】redux(阮一峰教程)
    已知CSIDL常量值,求其对应路径。
    Linux异步IO之 io_uring 详解及使用代码示例
    lc[数组]---59.螺旋矩阵②(new二维数组&&循环不变量&&模拟)
    有没有好用的提取歌曲伴奏的软件?
    大数据-玩转数据-Flink状态编程(中)
  • 原文地址:https://blog.csdn.net/liaomingwu/article/details/125425710