MongoDB本地配置分片

mongodb server version: 7.0.12 社区版

mongo shell version: 2.2.10

平台:win10 64位

控制台:Git Bash

分片相关节点结构示意图

大概步骤

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

-- 创建数据目录
mkdir -p ~/dbs/config1 ~/dbs/config2 ~/dbs/config3
-- 启动配置服务器
./mongod.exe --dbpath ~/dbs/config1 --port 20001 --replSet cfgrs1/localhost:20002 --configsvr
./mongod.exe --dbpath ~/dbs/config2 --port 20002 --replSet cfgrs1/localhost:20001 --configsvr
./mongod.exe --dbpath ~/dbs/config3 --port 20003 --replSet cfgrs1/localhost:20001 --configsvr
./mongosh.exe localhost:20001/local
-- 初始化副本集
rs.initiate(
   {
      _id: "cfgrs1",
      version: 1,
      members: [
         { _id: 0, host : "localhost:20001" },
         { _id: 1, host : "localhost:20002" },
         { _id: 2, host : "localhost:20003" }
      ]
   }
)
-- 查看副本集
db.system.replset.find()

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

-- 创建数据目录
mkdir -p ~/dbs/shard1 ~/dbs/shard2 ~/dbs/shard3
-- 启动分片服务器
./mongod.exe --dbpath ~/dbs/shard1 --port 10001 --replSet shardrs1/localhost:10002 --shardsvr
./mongod.exe --dbpath ~/dbs/shard2 --port 10002 --replSet shardrs1/localhost:10001 --shardsvr
./mongod.exe --dbpath ~/dbs/shard3 --port 10003 --replSet shardrs1/localhost:10001 --shardsvr
./mongosh.exe localhost:10001/local
-- 初始化副本集
rs.initiate(
   {
      _id: "shardrs1",
      version: 1,
      members: [
         { _id: 0, host : "localhost:10001" },
         { _id: 1, host : "localhost:10002" },
         { _id: 2, host : "localhost:10003" }
      ]
   }
)
-- 查看副本集
db.system.replset.find()
-- 查看是否位主节点
rs.isMaster()

3. 启动mongs

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

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

-- 连接mongos
./mongosh.exe localhost:30000/admin

-- 添加分片
db.runCommand({addShard:"shardrs1/localhost:10001,localhost:10002,localhost:10003",allowLocal:true})

-- 开启数据库级别支持分片
db.runCommand({"enableSharding":"foo"})

-- 开启集合级别支持分片
db.runCommand({"shardCollection":"foo.bar","key":{"_id":1}})

-- 切换到 config 数据库
use config

-- 查看分片
 db.shards.find()

-- 查看数据块
db.chunks.find()

-- 测试插入数据
use foo
db.bar.insertOne({"name":"Tom","age":9})
db.bar.find()

相关推荐

  1. MongoDB 数据库分片

    2024-07-11 18:26:02       25 阅读
  2. springboot-mongodb-连接配置

    2024-07-11 18:26:02       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 18:26:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 18:26:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 18:26:02       58 阅读
  4. Python语言-面向对象

    2024-07-11 18:26:02       69 阅读

热门阅读

  1. Mongodb索引使用限制

    2024-07-11 18:26:02       25 阅读
  2. 数据建设实践之大数据平台(七)

    2024-07-11 18:26:02       25 阅读
  3. git revert怎么使用?

    2024-07-11 18:26:02       24 阅读
  4. Webpack配置及工作流程

    2024-07-11 18:26:02       22 阅读
  5. 如何理解李彦宏说的“不要卷模型,要卷应用”

    2024-07-11 18:26:02       22 阅读
  6. 谷歌广告投放策略 -- 业务&成本

    2024-07-11 18:26:02       19 阅读
  7. 表单代码示例

    2024-07-11 18:26:02       23 阅读
  8. Unity中短路法在背包系统的应用

    2024-07-11 18:26:02       18 阅读
  9. 3133. 数组最后一个元素的最小值

    2024-07-11 18:26:02       23 阅读
  10. windows脚本获取 svn版本号

    2024-07-11 18:26:02       20 阅读
  11. 力扣题解(摆动序列)

    2024-07-11 18:26:02       21 阅读