mlaunch 是一个用于在本地计算机上快速初始化并启动复杂 MongoDB 测试环境(具有任意节点数的副本集或分片集群)的实用程序,通过 –dir 参数指定测试环境数据目录,默认为 ./data/
具体操作流程如下:
环境介绍:
1》Rocky Linux release 8.9 (kernel 4.18.0)
2》mtools 1.7.2,MongoDB 7.0.11
基础安装
1、部署 mtools
dnf install python39
pip3 install mtools
pip3 install 'mtools[all]'
mlaunch --version
#> Detected mongod version: 7.0.11
#> mtools version 1.7.2 || Python 3.9.19 (main, May 20 2024, 05:19:58) [GCC 8.5.0 20210514 (Red Hat 8.5.0-22)]
2、部署 MongoDB 软件包
cat > /etc/yum.repos.d/mongodb-org-7.0.repo << EOF
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-7.0.asc
EOF
# 安装指定版本
sudo dnf install -y mongodb-org-7.0.11
副本集
副本集是一组维护相同数据集的 mongod 进程,提供冗余和高可用性,是所有生产部署的基础
其架构图如下:
1、创建一个具有 3 个节点的副本集环境
mlaunch init --replicaset --nodes 3 --dir ./data/
mlaunch list --tags
#> Detected mongod version: 7.0.11
#> PROCESS PORT STATUS PID TAGS
#> mongod 27017 running 24324 27017, all, mongod, running
#> mongod 27018 running 24382 27018, all, mongod, running
#> mongod 27019 running 24440 27019, all, mongod, running
2、停止指定节点
mlaunch stop 27018
mlaunch list --tags
#> Detected mongod version: 7.0.11
#> PROCESS PORT STATUS PID TAGS
#> mongod 27017 running 24324 27017, all, mongod, running
#> mongod 27018 down - 27018, all, down, mongod
#> mongod 27019 running 24440 27019, all, mongod, running
分片集群
分片是一种跨多台机器分布数据的方法,MongoDB 使用分片来支持超大数据集和高吞吐量操作的部署
其架构图如下:
1》Shard 在集合级别对数据进行分片,并且该分片以 replicaset 方式部署
2》Mongos 充当查询路由器,在客户端应用程序和分片集群之间提供接口
3》Config 服务器会存储集群的元数据、配置信息
1、使用 mlaunch 创建一个 3 分片集群,每个分片节点数 3(1 主 2 从),配置服务器节点数 2,路由器节点数 1
# 选项 --sharded 3 可替换为 --sharded sh1 sh2 sh2 实现自定义各分片名称
mlaunch init --sharded 3 --replicaset --nodes 3 --config 2 --mongos 1 --dir ./data/
#> Detected mongod version: 7.0.11
#> launching: "mongod" on port 27018
#> launching: "mongod" on port 27019
#> launching: "mongod" on port 27020
#> launching: "mongod" on port 27021
#> launching: "mongod" on port 27022
#> launching: "mongod" on port 27023
#> launching: "mongod" on port 27024
#> launching: "mongod" on port 27025
#> launching: "mongod" on port 27026
#> launching: config server on port 27027
#> launching: config server on port 27028
#> replica set 'configRepl' initialized.
#> replica set 'shard01' initialized.
#> replica set 'shard02' initialized.
#> replica set 'shard03' initialized.
#> launching: mongos on port 27017
#> adding shards. can take up to 30 seconds...
2、查看分片集群状态
mlaunch list --tags
#> Detected mongod version: 7.0.11
#>
#> PROCESS PORT STATUS PID TAGS
#>
#> mongos 27017 running 28716 27017, all, mongos, running
#>
#> config server 27027 running 28321 27027, all, config, mongod, running
#> config server 27028 running 28398 27028, all, config, mongod, running
#>
#> shard01
#> mongod 27018 running 27764 27018, all, mongod, running, shard01
#> secondary 27019 running 27825 27019, all, mongod, running, secondary, shard01
#> secondary 27020 running 27887 27020, all, mongod, running, secondary, shard01
#>
#> shard02
#> primary 27021 running 27950 27021, all, mongod, primary, running, shard02
#> secondary 27022 running 28011 27022, all, mongod, running, secondary, shard02
#> secondary 27023 running 28073 27023, all, mongod, running, secondary, shard02
#>
#> shard03
#> primary 27024 running 28136 27024, all, mongod, primary, running, shard03
#> secondary 27025 running 28198 27025, all, mongod, running, secondary, shard03
#> secondary 27026 running 28259 27026, all, mongod, running, secondary, shard03
3、查看各实例启动参数
mlaunch list --startup
#> Detected mongod version: 7.0.11
#>
#> PROCESS PORT STATUS PID STARTUP COMMAND
#>
#> mongos 27017 running 36540 mongos --logpath /root/data/mongos.log --port 27017 --configdb configRepl/localhost:27027,localhost:27028 --fork
#>
#> config server 27027 running 35036 mongod --replSet configRepl --dbpath /root/data/configRepl/rs1/db --logpath /root/data/configRepl/rs1/mongod.log --port 27027 --fork --configsvr --wiredTigerCacheSizeGB 1
#> config server 27028 running 35134 mongod --replSet configRepl --dbpath /root/data/configRepl/rs2/db --logpath /root/data/configRepl/rs2/mongod.log --port 27028 --fork --configsvr --wiredTigerCacheSizeGB 1
#>
#> shard01
#> primary 27018 running 35247 mongod --replSet shard01 --dbpath /root/data/shard01/rs1/db --logpath /root/data/shard01/rs1/mongod.log --port 27018 --fork --shardsvr --wiredTigerCacheSizeGB 1
#> secondary 27019 running 35409 mongod --replSet shard01 --dbpath /root/data/shard01/rs2/db --logpath /root/data/shard01/rs2/mongod.log --port 27019 --fork --shardsvr --wiredTigerCacheSizeGB 1
#> secondary 27020 running 35520 mongod --replSet shard01 --dbpath /root/data/shard01/rs3/db --logpath /root/data/shard01/rs3/mongod.log --port 27020 --fork --shardsvr --wiredTigerCacheSizeGB 1
#>
#> shard02
#> primary 27021 running 35632 mongod --replSet shard02 --dbpath /root/data/shard02/rs1/db --logpath /root/data/shard02/rs1/mongod.log --port 27021 --fork --shardsvr --wiredTigerCacheSizeGB 1
#> secondary 27023 running 35874 mongod --replSet shard02 --dbpath /root/data/shard02/rs3/db --logpath /root/data/shard02/rs3/mongod.log --port 27023 --fork --shardsvr --wiredTigerCacheSizeGB 1
#> secondary 27022 running 35751 mongod --replSet shard02 --dbpath /root/data/shard02/rs2/db --logpath /root/data/shard02/rs2/mongod.log --port 27022 --fork --shardsvr --wiredTigerCacheSizeGB 1
#>
#> shard03
#> primary 27024 running 36002 mongod --replSet shard03 --dbpath /root/data/shard03/rs1/db --logpath /root/data/shard03/rs1/mongod.log --port 27024 --fork --shardsvr --wiredTigerCacheSizeGB 1
#> secondary 27025 running 36136 mongod --replSet shard03 --dbpath /root/data/shard03/rs2/db --logpath /root/data/shard03/rs2/mongod.log --port 27025 --fork --shardsvr --wiredTigerCacheSizeGB 1
#> secondary 27026 running 36270 mongod --replSet shard03 --dbpath /root/data/shard03/rs3/db --logpath /root/data/shard03/rs3/mongod.log --port 27026 --fork --shardsvr --wiredTigerCacheSizeGB 1