当前位置:   article > 正文

MongoDB本地配置分片

MongoDB本地配置分片

mongodb server version: 7.0.12 社区版

mongo shell version: 2.2.10

平台:win10 64位

控制台:Git Bash

分片相关节点结构示意图

大概步骤

1. 配置 配置服务器 副本集 (最少3个节点)

  1. -- 创建数据目录
  2. mkdir -p ~/dbs/config1 ~/dbs/config2 ~/dbs/config3
  3. -- 启动配置服务器
  4. ./mongod.exe --dbpath ~/dbs/config1 --port 20001 --replSet cfgrs1/localhost:20002 --configsvr
  5. ./mongod.exe --dbpath ~/dbs/config2 --port 20002 --replSet cfgrs1/localhost:20001 --configsvr
  6. ./mongod.exe --dbpath ~/dbs/config3 --port 20003 --replSet cfgrs1/localhost:20001 --configsvr
  7. ./mongosh.exe localhost:20001/local
  8. -- 初始化副本集
  9. rs.initiate(
  10. {
  11. _id: "cfgrs1",
  12. version: 1,
  13. members: [
  14. { _id: 0, host : "localhost:20001" },
  15. { _id: 1, host : "localhost:20002" },
  16. { _id: 2, host : "localhost:20003" }
  17. ]
  18. }
  19. )
  20. -- 查看副本集
  21. db.system.replset.find()

2. 配置 分片服务器 副本集 (最少3个节点)

  1. -- 创建数据目录
  2. mkdir -p ~/dbs/shard1 ~/dbs/shard2 ~/dbs/shard3
  3. -- 启动分片服务器
  4. ./mongod.exe --dbpath ~/dbs/shard1 --port 10001 --replSet shardrs1/localhost:10002 --shardsvr
  5. ./mongod.exe --dbpath ~/dbs/shard2 --port 10002 --replSet shardrs1/localhost:10001 --shardsvr
  6. ./mongod.exe --dbpath ~/dbs/shard3 --port 10003 --replSet shardrs1/localhost:10001 --shardsvr
  7. ./mongosh.exe localhost:10001/local
  8. -- 初始化副本集
  9. rs.initiate(
  10. {
  11. _id: "shardrs1",
  12. version: 1,
  13. members: [
  14. { _id: 0, host : "localhost:10001" },
  15. { _id: 1, host : "localhost:10002" },
  16. { _id: 2, host : "localhost:10003" }
  17. ]
  18. }
  19. )
  20. -- 查看副本集
  21. db.system.replset.find()
  22. -- 查看是否位主节点
  23. rs.isMaster()

3. 启动mongs

./mongos.exe --configdb cfgrs1/localhost:20001,localhost:20002,localhost:20003 --port 30000

4. 启动mongo shell,连接mongos服务器,切换到admin数据库,配置分片

  1. -- 连接mongos
  2. ./mongosh.exe localhost:30000/admin
  3. -- 添加分片
  4. db.runCommand({addShard:"shardrs1/localhost:10001,localhost:10002,localhost:10003",allowLocal:true})
  5. -- 开启数据库级别支持分片
  6. db.runCommand({"enableSharding":"foo"})
  7. -- 开启集合级别支持分片
  8. db.runCommand({"shardCollection":"foo.bar","key":{"_id":1}})
  9. -- 切换到 config 数据库
  10. use config
  11. -- 查看分片
  12. db.shards.find()
  13. -- 查看数据块
  14. db.chunks.find()
  15. -- 测试插入数据
  16. use foo
  17. db.bar.insertOne({"name":"Tom","age":9})
  18. db.bar.find()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/814737
推荐阅读
相关标签
  

闽ICP备14008679号