docker-compose搭建mongo分片集群

docker-compose

version: '3'
services:
    # 分片集群,两个节点
    shard_server11:
        container_name: shard_server11
        image: mongo
        networks:
            mongo:
                ipv4_address: 172.20.0.11
        ports:
            - 27018:27018
        volumes:
            - /opt/mongo/shard_server11/data/db:/data/db
            - /opt/mongo/shard_server11/data/configdb:/data/configdb
            - /opt/mongo/shard_server11/data/backup:/data/backup
        command: --shardsvr --bind_ip_all
        restart: always
        depends_on:
            - rs_config_server1
            - rs_config_server2

    shard_server21:
        container_name: shard_server21
        networks:
            mongo:
                ipv4_address: 172.20.0.12
        image: mongo
        ports:
            - 27028:27018
        volumes:
            - /opt/mongo/shard_server21/data/db:/data/db
            - /opt/mongo/shard_server21/data/configdb:/data/configdb
            - /opt/mongo/shard_server21/data/backup:/data/backup
        command: --shardsvr --bind_ip_all
        restart: always
        depends_on:
            - rs_config_server1
            - rs_config_server2


    # 配置服务器集群两个节点(mongodb3.4之后的版本需要两个config_server)
    rs_config_server1:
        container_name: rs_config_server1
        networks:
            mongo:
                ipv4_address: 172.20.0.13
        image: mongo
        volumes:
            - /opt/mongo/config_server1/data/db:/data/db
            - /opt/mongo/config_server1/data/configdb:/data/configdb
        command: --configsvr --replSet "rs_config_server" --bind_ip_all
        restart: always

    rs_config_server2:
        container_name: rs_config_server2
        networks:
            mongo:
                ipv4_address: 172.20.0.14
        image: mongo
        volumes:
            - /opt/mongo/config_server2/data/db:/data/db
            - /opt/mongo/config_server2/data/configdb:/data/configdb
        command: --configsvr --replSet "rs_config_server" --bind_ip_all
        restart: always

    

    # 路由节点mongos
    mongos:
        container_name: mongos
        networks:
            mongo:
                ipv4_address: 172.20.0.15
        image: mongo
        ports:
            - 27017:27017
        volumes:
            - /opt/mongo/mongos/data/db:/data/db
            - /opt/mongo/mongos/data/configdb:/data/configdb
        entrypoint: mongos
        command: --configdb rs_config_server/172.20.0.13:27019,172.20.0.14:27019 --bind_ip_all
        depends_on:
            - shard_server11
            - shard_server21

networks:
    mongo:
        driver: bridge
        ipam:
            config:
                - subnet: 172.20.0.10/24

配置服务器设置(config_server)

docker exec -it rs_config_server1 /bin/bash
mongo --host localhost --port 27019
rs.initiate({
    _id: "rs_config_server",
    configsvr: true,
    members: [
        { _id : 0, host : "172.20.0.13:27019" },
        { _id : 1, host : "172.20.0.14:27019" }
    ]
});

配置mongos

docker exec -it mongos /bin/bash
mongo --host localhost --port 27017
sh.addShard("172.20.0.11:27018")
sh.addShard("172.20.0.12:27018")

## 测试一下(新建一个集合,并开始插入数据)
sh.enableSharding("test")
sh.shardCollection("test.order", {"_id": "hashed" })
use test
for (i = 1; i <= 1000; i=i+1){db.order.insert({'price': 1})}
## 查看数据数量
db.order.find().count()
## 到两个分片下查看
rs_shard_server1:PRIMARY> db.order.find().count()
rs_shard_server2:PRIMARY> db.order.find().count()

增加分片到集群

docker exec -it mongos bash
mongo --host 172.17.0.8 --port 27017
sh.addShard("172.20.0.13:27018")

你可能感兴趣的:(数据库)