• 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()
  • 相关阅读:
    外汇天眼:虚假宣传 FCA发出最新警告 远离该平台!
    DNS协议隧道(2)
    第三章 Elasticsearch简介
    仿真2. 离散事件仿真
    Spring Bean的生命周期
    【华为OD机试真题 python】数组二叉树【2022 Q4 | 200分】
    python数据管理和分析
    2023icpc网络预选赛I. Pa?sWorD(dp)
    西宁市初中生地会考报名照片尺寸要求及手机自拍方法
    (附源码)springboot 学生选课系统 毕业设计 612555
  • 原文地址:https://blog.csdn.net/sweet6hero/article/details/132861202