• 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()
  • 相关阅读:
    目标检测YOLO实战应用案例100讲-基于改进YOLOv6的轧钢表面细小缺陷检测(续)
    C++——string
    Rust入门教程(五):错误处理
    FPGA通信—千兆网(UDP)软件设计
    深入理解嵌入式系统【基于Arduino的嵌入式系统入门与实践】相关基础知识概述:嵌入式系统/技术(定义、分类、组成、简介);Arduino开发板分类;VCC,GND;模拟信号和数字信号;杜邦线,面包板
    新版Testwell CTC++带来哪些新变化?
    微服务集成Spring Cloud Alibaba Seata(一)Seata服务搭建
    【Vue】组件化和声明周期函数
    spring容器ioc和di
    MyBatis一对多、多对一映射
  • 原文地址:https://blog.csdn.net/sweet6hero/article/details/132861202