Mongo6 分片集群(单机)
2026/9/17 3:02:42 网站建设 项目流程

准备目录

mkdir-p /root/Mongo6_Shardingchmod777-R /root/Mongo6_Shardingcd/root/Mongo6_Sharding# 生成证书openssl rand -base64128>/root/Mongo6_Sharding/keyFilechmod600/root/Mongo6_Sharding/keyFile

搭建Config Server

# 创建目录并拷贝证书sudomkdir-p /root/Mongo6_Sharding/mongo6-config-0/dbsudomkdir-p /root/Mongo6_Sharding/mongo6-config-0/configdbsudochmod-R777/root/Mongo6_Sharding/mongo6-config-0cp-a /root/Mongo6_Sharding/keyFile /root/Mongo6_Sharding/mongo6-config-0/configdb/# 配置文件cat>/root/Mongo6_Sharding/mongo6-config-0/configdb/mongod.conf<<EOF net: port: 21000 bindIpAll: true security: keyFile: /data/configdb/keyFile authorization: enabled replication: replSetName: rs-conf-0 sharding: clusterRole: configsvr storage: wiredTiger: engineConfig: cacheSizeGB: 0.5 EOF

搭建Shard-0

# 创建目录并拷贝证书mkdir-p /root/Mongo6_Sharding/mongo6-shard-0/dbmkdir-p /root/Mongo6_Sharding/mongo6-shard-0/configdbchmod-R777/root/Mongo6_Sharding/mongo6-shard-0cp-a /root/Mongo6_Sharding/keyFile /root/Mongo6_Sharding/mongo6-shard-0/configdb/# 配置文件cat>/root/Mongo6_Sharding/mongo6-shard-0/configdb/mongod.conf<<EOF net: port: 22000 bindIpAll: true security: keyFile: /data/configdb/keyFile authorization: enabled replication: replSetName: rs-shard-0 sharding: clusterRole: shardsvr storage: wiredTiger: engineConfig: cacheSizeGB: 0.5 EOF

搭建Shard-1

# 创建目录并拷贝证书mkdir-p /root/Mongo6_Sharding/mongo6-shard-1/dbmkdir-p /root/Mongo6_Sharding/mongo6-shard-1/configdbchmod-R777/root/Mongo6_Sharding/mongo6-shard-1cp-a /root/Mongo6_Sharding/keyFile /root/Mongo6_Sharding/mongo6-shard-1/configdb/# 配置文件cat>/root/Mongo6_Sharding/mongo6-shard-1/configdb/mongod.conf<<EOF net: port: 23000 bindIpAll: true security: keyFile: /data/configdb/keyFile authorization: enabled replication: replSetName: rs-shard-1 sharding: clusterRole: shardsvr storage: wiredTiger: engineConfig: cacheSizeGB: 0.5 EOF

搭建Shard-2

# 创建目录并拷贝证书mkdir-p /root/Mongo6_Sharding/mongo6-shard-2/dbmkdir-p /root/Mongo6_Sharding/mongo6-shard-2/configdbchmod-R777/root/Mongo6_Sharding/mongo6-shard-2cp-a /root/Mongo6_Sharding/keyFile /root/Mongo6_Sharding/mongo6-shard-2/configdb/# 配置文件cat>/root/Mongo6_Sharding/mongo6-shard-2/configdb/mongod.conf<<EOF net: port: 24000 bindIpAll: true security: keyFile: /data/configdb/keyFile authorization: enabled replication: replSetName: rs-shard-2 sharding: clusterRole: shardsvr storage: wiredTiger: engineConfig: cacheSizeGB: 0.5 EOF

搭建Mongos-0

# 创建目录并拷贝证书mkdir-p /root/Mongo6_Sharding/mongos6-0/dbmkdir-p /root/Mongo6_Sharding/mongos6-0/configdbchmod-R777/root/Mongo6_Sharding/mongos6-0cp-a /root/Mongo6_Sharding/keyFile /root/Mongo6_Sharding/mongos6-0/configdb/chown999/root/Mongo6_Sharding/mongos6-0/configdb/keyFile# 配置文件cat>/root/Mongo6_Sharding/mongos6-0/configdb/mongos.conf<<EOF net: port: 27027 bindIpAll: true security: keyFile: /data/configdb/keyFile sharding: configDB: rs-conf-0/172.19.40.9:21000 EOF

docker-compose

# 创建并启动version:'3.8'services:mongo6-config-0:container_name:mongo6-config-0image:mongo:6restart:alwaysnetwork_mode:hostvolumes:-/root/Mongo6_Sharding/mongo6-config-0/db:/data/db-/root/Mongo6_Sharding/mongo6-config-0/configdb:/data/configdb-/etc/localtime:/etc/localtime:rocommand:mongod-f /data/configdb/mongod.confmongo6-shard-0:container_name:mongo6-shard-0image:mongo:6restart:alwaysnetwork_mode:hostvolumes:-/root/Mongo6_Sharding/mongo6-shard-0/db:/data/db-/root/Mongo6_Sharding/mongo6-shard-0/configdb:/data/configdb-/etc/localtime:/etc/localtime:rocommand:mongod-f /data/configdb/mongod.confmongo6-shard-1:container_name:mongo6-shard-1image:mongo:6restart:alwaysnetwork_mode:hostvolumes:-/root/Mongo6_Sharding/mongo6-shard-1/db:/data/db-/root/Mongo6_Sharding/mongo6-shard-1/configdb:/data/configdb-/etc/localtime:/etc/localtime:rocommand:mongod-f /data/configdb/mongod.confmongo6-shard-2:container_name:mongo6-shard-2image:mongo:6restart:alwaysnetwork_mode:hostvolumes:-/root/Mongo6_Sharding/mongo6-shard-2/db:/data/db-/root/Mongo6_Sharding/mongo6-shard-2/configdb:/data/configdb-/etc/localtime:/etc/localtime:rocommand:mongod-f /data/configdb/mongod.confmongos6-0:container_name:mongos6-0image:mongo:6restart:alwaysnetwork_mode:hostvolumes:-/root/Mongo6_Sharding/mongos6-0/db:/data/db-/root/Mongo6_Sharding/mongos6-0/configdb:/data/configdb-/etc/localtime:/etc/localtime:rocommand:mongos-f /data/configdb/mongos.conf

初始化Config Server

#进入容器dockerexec-it mongo6-config-0 /bin/bash#进入mongomongosh --port21000#切换到admin数据库use admin#初始confi server复制集配置rs.initiate({_id:"rs-conf-0", members:[{_id:0,host:"172.19.40.9:21000"}]})db.createUser({user:"admin", pwd:"123456", roles:[{role:"root", db:"admin"}]});

初始化shard-0复制集

#进入容器dockerexec-it mongo6-shard-0 /bin/bash#进入mongomongosh --port22000#切换到admin数据库use admin#初始confi server复制集配置rs.initiate({_id:"rs-shard-0", members:[{_id:0,host:"172.19.40.9:22000"}]})db.createUser({user:"admin", pwd:"123456", roles:[{role:"root", db:"admin"}]});

初始化shard-1复制集

#进入容器dockerexec-it mongo6-shard-1 /bin/bash#进入mongomongosh --port23000#切换到admin数据库use admin#初始confi server复制集配置rs.initiate({_id:"rs-shard-1", members:[{_id:0,host:"172.19.40.9:23000"}]})db.createUser({user:"admin", pwd:"123456", roles:[{role:"root", db:"admin"}]});

初始化shard-2复制集

#进入容器dockerexec-it mongo6-shard-2 /bin/bash#进入mongomongosh --port24000#切换到admin数据库use admin#初始confi server复制集配置rs.initiate({_id:"rs-shard-2", members:[{_id:0,host:"172.19.40.9:24000"}]})db.createUser({user:"admin", pwd:"123456", roles:[{role:"root", db:"admin"}]});

初始化mongos-0配置文件

#登录mongos-0,进行分片配置#进入容器dockerexec-it mongos6-0 /bin/bash#进入mongomongosh --port27027#切换到admin数据库use admin#登录db.auth('admin','123456')#添加分片sh.addShard("rs-shard-0/172.19.40.9:22000");sh.addShard("rs-shard-1/172.19.40.9:23000");sh.addShard("rs-shard-2/172.19.40.9:24000");#对指定库进行分片sh.enableSharding("testdb")# 使用hash分片键sh.shardCollection('testdb.collection',{'field':'hashed'})#移除分片db.adminCommand({removeShard:<shardToRemove>})#分片集群中的每个数据库都有一个主分片。如果您要删除的分片也是集群数据库之一的主分片,则在迁移分片中的所有数据后,您必须手动将数据库移至新分片。#当您删除集群中块分布不均匀的分片时,平衡器首先从排出分片中删除块,然后平衡剩余的不均匀块分布。

分片相关命令,在mongos上使用

# 启用数据库分片:sh.enableSharding("<database>")# 使用hash分片键sh.shardCollection('db.collection',{'field':'hashed'})# 使用递增分片键sh.shardCollection('db.collection',{field:1})# 查看分片是否成功db.collection.stats().sharded# 查看数据分布db.collection.getShardDistribution()

设置平衡器运行窗口时间

use config#设置平衡器窗口时间db.settings.updateOne({_id:"balancer"},{$set:{activeWindow:{start:"<start-time>", stop:"<stop-time>"}}},{upsert:true});#将<开始时间>和<结束时间>替换为使用两位数小时和分钟值(即HH:MM)的时间值,该值指定平衡窗口的开始和结束边界。#对于HH值,使用00-23之间的小时值。#对于MM值,请使用00-59之间的分钟值。#对于内部部署或自管理的分片集群,MongoDB评估相对于配置服务器副本集中主要成员的时区的开始和停止时间。#块只有在到达指定大小才会进行分裂,默认128MB#获取平衡器状态sh.getBalancerState()#启动平衡器sh.startBalancer()#停止平衡器sh.stopBalancer()

分片操作相关命令

#获取所有分片相关信息db.runCommand("getShardMap")

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询