docker-compose.yml:
version: '3.9' services: shard1-1: image: mongo:4.4.4 container_name: shard1-1 # --shardsvr: 这个参数仅仅只是将默认的27017端口改为27018,如果指定--port参数,可用不需要这个参数 # --directoryperdb:每个数据库使用单独的文件夹 command: mongod --shardsvr --directoryperdb --replSet shard1 volumes: - /etc/localtime:/etc/localtime - /mymongoDB/shard1-1/data:/data/db networks: - mongo shard1-2: image: mongo:4.4.4 container_name: shard1-2 command: mongod --shardsvr --directoryperdb --replSet shard1 volumes: - /etc/localtime:/etc/localtime - /mymongoDB/shard1-2/data:/data/db networks: - mongo shard1-3: image: mongo:4.4.4 container_name: shard1-3 command: mongod --shardsvr --directoryperdb --replSet shard1 volumes: - /etc/localtime:/etc/localtime - /mymongoDB/shard1-3/data:/data/db networks: - mongo shard2-1: image: mongo:4.4.4 container_name: shard2-1 command: mongod --shardsvr --directoryperdb --replSet shard2 volumes: - /etc/localtime:/etc/localtime - /mymongoDB/shard2-1/data:/data/db networks: - mongo shard2-2: image: mongo:4.4.4 container_name: shard2-2 command: mongod --shardsvr --directoryperdb --replSet shard2 volumes: - /etc/localtime:/etc/localtime - /mymongoDB/shard2-2/data:/data/db networks: - mongo shard2-3: image: mongo:4.4.4 container_name: shard2-3 command: mongod --shardsvr --directoryperdb --replSet shard2 volumes: - /etc/localtime:/etc/localtime - /mymongoDB/shard2-3/data:/data/db networks: - mongo config1: image: mongo:4.4.4 container_name: config1 command: mongod --configsvr --directoryperdb --replSet config volumes: - /etc/localtime:/etc/localtime - /mymongoDB/config1/data:/data/configdb networks: - mongo config2: image: mongo:4.4.4 container_name: config2 command: mongod --configsvr --directoryperdb --replSet config volumes: - /etc/localtime:/etc/localtime - /mymongoDB/config2/data:/data/configdb networks: - mongo config3: image: mongo:4.4.4 container_name: config3 command: mongod --configsvr --directoryperdb --replSet config volumes: - /etc/localtime:/etc/localtime - /mymongoDB/config3/data:/data/configdb networks: - mongo mongos: #生产上需要创建多个mongos,然后借助haproxy 或者lvs做高可用) image: mongo:4.4.4 container_name: mongos command: mongos --configdb config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0.0.0 --port 27017 ports: - 27017:27017 volumes: - /etc/localtime:/etc/localtime #启动顺序 depends_on: - config1 - config2 - config3 networks: - mongo networks: mongo: driver: bridge name: mongo
集群搭建:
启动容器 docker-compose up -d #进入配置服务器成员之一启动副本集 docker exec -it config1 mongo --port 27019 #启动副本集 rs.initiate( { _id: "config", configsvr: true, members: [ { _id : 0, host : "config1:27019" }, { _id : 1, host : "config2:27019" }, { _id : 2, host : "config3:27019" } ] } ) #进入分片1服务器成员之一启动副本集 docker exec -it shard1-1 mongo --port 27018 # rs.initiate( { _id : "shard1", members: [ { _id : 0, host : "shard1-1:27018" }, { _id : 1, host : "shard1-2:27018" }, { _id : 2, host : "shard1-3:27018" } ] } ) #进入分片2服务器成员之一启动副本集 docker exec -it shard2-1 mongo --port 27018 rs.initiate( { _id : "shard2", members: [ { _id : 0, host : "shard2-1:27018" }, { _id : 1, host : "shard2-2:27018" }, { _id : 2, host : "shard2-3:27018" } ] } ) docker exec -it mongos mongo #将分片添加到群集 sh.addShard( "shard1/shard1-1:27018,shard1-2:27018,shard1-3:27018") sh.addShard( "shard2/shard2-1:27018,shard2-2:27018,shard2-3:27018") 分片集群搭建完 剩下就是: #依旧在路由服务器上操作 #于平衡器操作 https://docs.mongodb.com/manual/tutorial/manage-sharded-cluster-balancer/#check-the-balancer-state #启用数据库分片 sh.enableSharding("<database>") #基于哈希或者范围分片操作;空集会自动创建索引;非空集就要先创建索引例如:db.user.createIndex({"id": 1})。 sh.shardCollection("<database>.<collection>", { <shard key field> : "hashed" } )或者sh.shardCollection("<database>.<collection>", { <shard key field> : 1, ... } ) 测试集群: #先把块调小,测试完再调回默认64M use config #进配置库 db.settings.save( { _id:"chunksize", value: 1} ) #设置1M块 db.serverStatus().sharding #查看状态 #测试范围分片: sh.enableSharding("mydb") #开启数据库分片功能 sh.shardCollection("mydb.user",{"id": 1}) #选集合的分片建基于范围分片 #造测试数据 use mydb for(i=1;i<=20000;i++){db.user.insert({"id":i,"name":"jack"+i})} #查看分片效果 sh.status() #哈希分片 和上面一样只是第二步,分片类型不是递增而是哈希 sh.enableSharding("mydb2") sh.shardCollection("mydb2.user",{"id":"hashed"}) use mydb2 for(i=1;i<=50000;i++){db.user.insert({"id":i,"name":"jack"+i})} sh.status() 结果如下: