Author: Yu Zhenxing

A member of the DBA team of Akson, keen on technology sharing and writing technical documents.

Source of this article: original contribution

*The original content is produced by the open source community of Aikesheng, and the original content shall not be used without authorization. For reprinting, please contact the editor and indicate the source.


Table of contents:

  • 1. mlaunch and MongoDB software installation
  • 2. mlaunch deployment replica set and operation and maintenance operations
  • 3. mlaunch deploys a sharded cluster
  • Fourth, mlaunch common deployment architecture command example
  • 5. Reference links
In the process of using and learning MongoDB, we will inevitably encounter the need to deploy various architectures for functional verification. Due to different architectures and configurations, we need to spend a lot of time in the process of setting up the environment. It is recommended to use a test that can quickly deploy MongoDB. The tool mlaunch for various architectures in the environment is one of a set of open source toolkits mtools of MongoDB (if you are interested in other tools of mtools, you can also check the link at the end of the article for details), which can be used to quickly deploy MongoDB services of different architectures in the test environment , supports single node, replica set and sharded cluster, installation and operation are very simple, the following are deployment examples of common scenarios and architectures

1. mlaunch and MongoDB software installation

 ## 安装python3,及MongoDB服务的依赖包
shell> yum install -y python3 python3-devel net-snmp-agent-libs
  
## 安装 mlaunch 依赖包
shell> pip3 install mtools psutil pymongo
 
## MongoDB软件包安装(解压即可),MongoDB软件包自行下载,当前示例服务器下载了2个版本软件包,可以任意指定其中版本
shell> ll -ld /opt/mongodb-linux-x86_64-*
drwxr-xr-x 4 root root       148 8月  16 18:30 /opt/mongodb-linux-x86_64-enterprise-rhel70-4.0.16
-rw-r--r-- 1 root root 120608697 8月  16 18:30 /opt/mongodb-linux-x86_64-enterprise-rhel70-4.0.16.tgz
drwxr-xr-x 3 root root       100 8月  16 18:31 /opt/mongodb-linux-x86_64-rhel70-4.4.14
-rw-r--r-- 1 root root  72399766 8月  16 18:25 /opt/mongodb-linux-x86_64-rhel70-4.4.14.tgz

2. mlaunch deployment replica set and operation and maintenance operations

 ## 初始化一个3节点的副本集
## 副本集名称为 demo_replicatset
## 起始端口为 30000
## 指定30000端口的优先级最高(优先级默认为1,主节点调整为10,值越高,优先级越高)
## 版本为 4.4.14
## 数据目录为 /data/demo_rs
shell> mlaunch init --replicaset --nodes 3 --name "demo_replicatset" --priority --port 30000 --binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin --dir /data/demo_rs
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30000
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30001
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30002
replica set 'demo_replicatset' initialized.
  
## 查看部署运行的副本集状态
shell> mlaunch list --dir /data/demo_rs/
PROCESS    PORT     STATUS     PID
mongod     30000    running    9017
mongod     30001    running    9065
mongod     30002    running    9113
  
## 登录副本集(默认无密码,可也在初始化时指定)
shell> /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongo --host=localhost:30000
demo_replicatset:PRIMARY>
  
## 停止该副本集
shell> mlaunch stop --dir /data/demo_rs/
sent signal Signals.SIGTERM to 3 processes.
  
## 重启启动该副本集
shell> mlaunch start --dir /data/demo_rs/
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30000
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30001
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30002
  
## 删除该副本集(停止后直接删除目录即可)
shell> rm /data/demo_rs/ -rf

3. mlaunch deploys a sharded cluster

 ## 创建3节点的分片,每个节点只有1个主(考虑服务器配置有限)
## 配置3节点configServer
## 配置1个mongos
shell> mlaunch init --sharded 3 --nodes 1 --replicaset --config 3 --csrs --mongos 1 --priority --name "demo_shard" --port 30000 --binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin --dir /data/demo_shard
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30001
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30002
launching: "/opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongod" on port 30003
launching: config server on port 30004
launching: config server on port 30005
launching: config server on port 30006
replica set 'configRepl' initialized.
replica set 'shard01' initialized.
replica set 'shard02' initialized.
replica set 'shard03' initialized.
launching: /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongos on port 30000
adding shards. can take up to 30 seconds...
  
## 查看部署的集群架构及基本信息
[root@10-186-61-159 ~]# mlaunch list --dir /data/demo_shard/
PROCESS          PORT     STATUS     PID
mongos           30000    running    15820
config server    30004    running    15514
config server    30005    running    15567
config server    30006    running    15620
shard01
    mongod       30001    running    15377
shard02
    mongod       30002    running    15422
shard03
    mongod       30003    running    15469
  
## 登录集群
shell> /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin/mongo --host=localhost:30000
mongos>

Fourth, mlaunch common deployment architecture command example

 ## 单节点副本集
mlaunch init \
--replicaset \
--priority \
--nodes 1 \
--name "demo_single_rs" \
--port 30000 \
--binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin \
--dir /data/demo_single_rs
  
## 副本集 PSS 架构
mlaunch init \
--replicaset \
--priority \
--nodes 3 \
--name "demo_replicatset" \
--port 30000 \
--binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin \
--dir /data/demo_rs
 
  
## 副本集 PSA 架构
mlaunch init \
--replicaset \
--priority \
--arbiter \
--nodes 3 \
--name "demo_replicatset" \
--port 30000 \
--binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin \
--dir /data/demo_rs
  
## 分片集群高可用架构
## 3节点ConfigServer 副本集
## 3节点shard分片节点 副本集
## 3节点mongos 高可用
mlaunch init \
--sharded 3 \
--nodes 3 \
--replicaset \
--config 3 \
--mongos 3 \
--csrs \
--priority \
--name "demo_shard" \
--port 30000 \
--binarypath /opt/mongodb-linux-x86_64-rhel70-4.4.14/bin \
--dir /data/demo_shard

5. Reference links

http://blog.rueckstiess.com/mtools/mlaunch.html

https://github.com/rueckstiess/mtools


爱可生开源社区
426 声望211 粉丝

成立于 2017 年,以开源高质量的运维工具、日常分享技术干货内容、持续的全国性的社区活动为社区己任;目前开源的产品有:SQL审核工具 SQLE,分布式中间件 DBLE、数据传输组件DTLE。