MongoDB本地配置分片

mongodb server version: 7.0.12 社区版

mongo shell version: 2.2.10

平台:win10 64位

控制台:Git Bash

分片相关节点结构示意图

大概步骤

  1. 配置 配置服务器 副本集 (最少3个节点)
bash 复制代码
-- 创建数据目录
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()
  1. 配置 分片服务器 副本集 (最少3个节点)
bash 复制代码
-- 创建数据目录
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()
  1. 启动mongs
bash 复制代码
./mongos.exe --configdb cfgrs1/localhost:20001,localhost:20002,localhost:20003 --port 30000
  1. 启动mongo shell,连接mongos服务器,切换到admin数据库,配置分片
bash 复制代码
-- 连接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()
相关推荐
m0_495496412 分钟前
SQL批量更新状态机字段_使用CASE表达式一次性处理
jvm·数据库·python
2401_850491654 分钟前
Python处理分类不平衡问题_使用平衡随机森林提升召回率
jvm·数据库·python
终生成长者12 分钟前
04LangChain SQL 问答系统知识点详解
数据库·python·sql·langchain
m0_7335654615 分钟前
Golang Redis Pipeline如何用_Golang Redis Pipeline教程【完整】
jvm·数据库·python
2401_8676239825 分钟前
golang如何实现布隆过滤器_golang布隆过滤器实现教程
jvm·数据库·python
m0_7407963627 分钟前
golang如何编写Markdown转HTML工具_golang Markdown转HTML工具编写详解
jvm·数据库·python
dblens 数据库管理和开发工具30 分钟前
除了传统数据库工具,MariaDB 用户现在有了一个 Agent 工作台
数据库·mariadb
2403_8832610931 分钟前
CSS如何实现Bootstrap进度条自定义动画_利用keyframe关键帧
jvm·数据库·python
2301_7693406732 分钟前
CSS如何兼容新旧方案结合响应式容器查询
jvm·数据库·python
weixin_4597539437 分钟前
MySQL 中高效存储与查询时间数据的最佳实践
jvm·数据库·python