version: '3'
services:
mongo_shard1:
image: mongo:latest
container_name: mongo_shard1
# --shardsvr: 这个参数仅仅只是将默认的27017端口改为27018,如果指定--port参数,可用不需要这个参数
# --directoryperdb:每个数据库使用单独的文件夹
command: mongod --shardsvr --directoryperdb --replSet mongo_shard1 --keyFile /data/mongo-keyfile
ports:
- 27021:27018
volumes:
- /etc/localtime:/etc/localtime
- ./data/shard1:/data/db
- ./mongo-keyfile:/data/mongo-keyfile
privileged: true
mongo_shard2:
image: mongo:latest
container_name: mongo_shard2
command: mongod --shardsvr --directoryperdb --replSet mongo_shard2 --keyFile /data/mongo-keyfile
ports:
- 27022:27018
volumes:
- /etc/localtime:/etc/localtime
- ./data/shard2:/data/db
- ./mongo-keyfile:/data/mongo-keyfile
privileged: true
mongo_shard3:
image: mongo:latest
container_name: mongo_shard3
command: mongod --shardsvr --directoryperdb --replSet mongo_shard3 --keyFile /data/mongo-keyfile
ports:
- 27023:27018
volumes:
- /etc/localtime:/etc/localtime
- ./data/shard3:/data/db
- ./mongo-keyfile:/data/mongo-keyfile
privileged: true
mongo_config1:
image: mongo:latest
container_name: mongo_config1
# --configsvr: 这个参数仅仅是将默认端口由27017改为27019, 如果指定--port可不添加该参数
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --keyFile /data/mongo-keyfile
ports:
- 27031:27019
volumes:
- /etc/localtime:/etc/localtime
- ./data/config1:/data/configdb
- ./mongo-keyfile:/data/mongo-keyfile
mongo_config2:
image: mongo:latest
container_name: mongo_config2
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --keyFile /data/mongo-keyfile
ports:
- 27032:27019
volumes:
- /etc/localtime:/etc/localtime
- ./data/config2:/data/configdb
- ./mongo-keyfile:/data/mongo-keyfile
mongo_config3:
image: mongo:latest
container_name: mongo_config3
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --keyFile /data/mongo-keyfile
ports:
- 27033:27019
volumes:
- /etc/localtime:/etc/localtime
- ./data/config3:/data/configdb
- ./mongo-keyfile:/data/mongo-keyfile
mongo-mongos:
image: mongo:latest
container_name: mongo_mongos
command: /bin/sh -c 'mongos --configdb fates-mongo-config/mongo_config1:27019,mongo_config2:27019,mongo_config3:27019 --bind_ip 0.0.0.0 --port 27017 --keyFile /data/mongo-keyfile'
ports:
- 27017:27017
volumes:
- /etc/localtime:/etc/localtime
- ./mongo-keyfile:/data/mongo-keyfile
depends_on:
- mongo_config1
- mongo_config2
- mongo_config3
networks:
dev_network:
external: true
name: dev_network
#!/bin/sh
rm -rf ./data/*
mkdir -p ./data/shard1
mkdir -p ./data/shard2
mkdir -p ./data/shard3
mkdir -p ./data/config1
mkdir -p ./data/config2
mkdir -p ./data/config3
#创建网络
docker network create --driver bridge dev_network
#创建key:文件放入yml文件目录
openssl rand -base64 745 > mongo-keyfile
chmod 400 ./mongo-keyfile
chown 999:999 ./mongo-keyfile
docker-compose exec mongo_config1 bash -c "echo 'rs.initiate({_id: \"fates-mongo-config\",configsvr: true, members: [{ _id : 0, host : \"mongo_config1:27019\" },{ _id : 1, host : \"mongo_config2:27019\" }, { _id : 2, host : \"mongo_config3:27019\" }]})' | mongosh --port 27019"
docker-compose exec mongo_shard1 bash -c "echo 'rs.initiate({_id: \"mongo_shard1\",members: [{ _id : 0, host : \"mongo_shard1:27018\" }]})' | mongosh --port 27018"
docker-compose exec mongo_shard2 bash -c "echo 'rs.initiate({_id: \"mongo_shard2\",members: [{ _id : 0, host : \"mongo_shard2:27018\" }]})' | mongosh --port 27018"
docker-compose exec mongo_shard3 bash -c "echo 'rs.initiate({_id: \"mongo_shard3\",members: [{ _id : 0, host : \"mongo_shard3:27018\" }]})' | mongosh --port 27018"
docker-compose -f docker-compose.yml exec mongo-mongos bash -c "echo 'sh.addShard(\"mongo_shard1/mongo_shard1:27018\")' | mongosh"
docker-compose -f docker-compose.yml exec mongo-mongos bash -c "echo 'sh.addShard(\"mongo_shard2/mongo_shard2:27018\")' | mongosh"
docker-compose -f docker-compose.yml exec mongo-mongos bash -c "echo 'sh.addShard(\"mongo_shard3/mongo_shard3:27018\")' | mongosh"
use admin
db.createUser({user:"admin",pwd:"admin",roles:[{role:"root",db:"admin"}]})
use test
db.createUser({user:"admin",pwd:"admin",roles:[{role:"root",db:"admin"}]})
db.auth("admin","admin")
sh.enableSharding("test")
sh.shardCollection("test.mytable", {"_id": "hashed" })
db.mytable.insert({'price': 1})
db.mytable.find().count()
docker compose 搭建分片集群
sweet6hero2023-09-14 8:44
相关推荐
小羊没烦恼!3 天前
微服务化的基石——持续集成俊昭喜喜里3 天前
java中的继承和多态的区别小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升譕痕3 天前
JSONObject与JSONArray封装数据格式区别胡写代码3 天前
别再前后端各写一套表单校验了小鱼能吃糖3 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域此时不提桶,更待何时3 天前
01-06-A-JVM排查实战详解分布式存储与RustFS3 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办vipxieliang3 天前
ValidX 在 DDD 领域驱动设计中的实践ba_pi3 天前
mysql 查询所有表名并授权