docker compose 搭建分片集群

复制代码
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()
相关推荐
yava_free6 分钟前
JVM这个工具的使用方法
java·jvm
落非34 分钟前
Ubuntu配置阿里云docker apt源
ubuntu·阿里云·docker
不会编程的懒洋洋34 分钟前
Spring Cloud Eureka 服务注册与发现
java·笔记·后端·学习·spring·spring cloud·eureka
赖龙39 分钟前
java程序打包及执行 jar命令及运行jar文件
java·pycharm·jar
U12Euphoria41 分钟前
java的runnable jar采用exe和.bat两种方式解决jre环境的问题
java·pycharm·jar
java小吕布1 小时前
Java Lambda表达式详解:函数式编程的简洁之道
java·开发语言
程序员劝退师_1 小时前
优惠券秒杀的背后原理
java·数据库
java小吕布1 小时前
Java集合框架之Collection集合遍历
java
一二小选手1 小时前
【Java Web】分页查询
java·开发语言
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ2 小时前
idea 弹窗 delete remote branch origin/develop-deploy
java·elasticsearch·intellij-idea