MongoDB本地测试环境搭建工具--mtools之mlaunch使用方法

摘要

  • 本文介绍mtools工具的使用

  • mtools官方文档mtools-github

  • mtools提供的mlaunch是一个基于python的mongo环境管理工具,可以方便的启动、停止、重启、kill mongo进程,还可以查看mongo进程的运行状态,可以方便初学者快速搭建本地mongo环境,但是其只能在一台机器上运行。

mtools安装

  • mtools基于python3,支持mongo4+以上版本,但是mtools并且不会为我们安装mongodb,所以需要我们自己先在系统中安装好mongodb,mongodb的安装可以参考MongoDB7.0的安装

  • 按照官网的说法,mtools支持的Python版本为 3.7|3.8|3.9|3.10,其他版本的Python目前不受支持或测试,但作者的python版本为3.11.3,并没有出现兼容性问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
➜  ~ python3 --version
Python 3.11.3
➜ ~ pip3 -V
pip 24.0 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)
➜ ~ pip3 list
Package Version
----------------- ------------
certifi 2023.11.17
cffi 1.16.0
cryptography 41.0.7
docutils 0.20.1
idna 3.6
Jinja2 3.1.2
MarkupSafe 2.1.3
packaging 23.2
pip 24.0
pycparser 2.21
Pygments 2.17.2
pyparsing 3.1.1
pytz 2023.3.post1
requests 2.31.0
setuptools 68.2.2
six 1.16.0
tabulate 0.0.0
typing_extensions 4.9.0
urllib3 2.1.0
wheel 0.41.2
  • 安装mtools依赖

1
pip3 install python-dateutil psutil pymongo
  • 安装mtools

1
pip3 install mtools
  • 安装mtools会同时为我们安装mtools的所有工具,如果只希望安装mlaunch,可以使用下面的命令
    mtools提供的其它工具,如mlogfilter,mloginfo,mplotqueries等并不支持logv2格式的日志,logv2mongodb4.4+以后的加入的,从mongodb5.0以后logv2为默认的日志格式,所以这里不做介绍。

1
pip3 install 'mtools[mlaunch]'

mlaunch

  • 查看mlaunch版本

1
2
3
4
5
➜  ~ which mlaunch
/usr/local/bin/mlaunch
➜ ~ mlaunch --version
Detected mongod version: 7.0.6
mtools version 1.7.2 || Python 3.11.6 (main, Oct 3 2023, 02:51:45) [Clang 14.0.3 (clang-1403.0.22.14.1)]

init :构建环境

此命令初始化并启动MongoDB独立实例、副本集或分片集群,它只需要为每个环境调用一次。

构建独立实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 快速构建一个单节点,数据目录在当前路径下的data目录下
$ mlaunch init --single --auth
Detected mongod version: 7.0.6
Generating keyfile: /mongodb/mtools/single/data/keyfile
launching: "mongod" on port 27017
Username "user", password "password"


# 参数说明
# --single :启动单节点
# --auth :开启默认认证,默认用户名/密码:user/password,并授予如下角色
# { role: 'userAdminAnyDatabase', db: 'admin' },
# { role: 'readWriteAnyDatabase', db: 'admin' },
# { role: 'dbAdminAnyDatabase', db: 'admin' },
# { role: 'clusterAdmin', db: 'admin' }
# --dir :指定数据目录,默认为当前路径下的data目录
# --bind_ip:指定监听ip,默认为127.0.0.1
# --port:指定监听端口,默认为27017

构建复制集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 快速构建一个3节点的复制集
$ mlaunch init --replicaset --nodes 3 --auth --port 27040
Detected mongod version: 7.0.6
Generating keyfile: /mongodb/mtools/replicaset/data/keyfile
launching: "mongod" on port 27040
launching: "mongod" on port 27041
launching: "mongod" on port 27042
replica set 'replset' initialized.
waiting for primary to add a user.
Username "user", password "password"

# --replicaset :创建副本集,默认3个节点
# --nodes 3 :指定副本集节点个数,默认3个节点
# --auth : 开启默认认证,同单节点
# --port 27040 : 指定复制集的起始端口,默认从27017开始,依次递增

构建分片集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 快速构建一个2个分片的分片集群,每个分片是一个包含3每个节点的复制集,包含一个3个节点配置复制集,并且启动3个mongos路由
$ mlaunch init --sharded 2 --replicaset --node 3 --config 3 --csrs --mongos 3 --auth --port 27050
Detected mongod version: 7.0.6
Generating keyfile: /mongodb/mtools/sharded/data/keyfile
launching: "mongod" on port 27053
launching: "mongod" on port 27054
launching: "mongod" on port 27055
launching: "mongod" on port 27056
launching: "mongod" on port 27057
launching: "mongod" on port 27058
launching: config server on port 27059
launching: config server on port 27060
launching: config server on port 27061
replica set 'configRepl' initialized.
replica set 'shard01' initialized.
replica set 'shard02' initialized.
launching: mongos on port 27050
launching: mongos on port 27051
launching: mongos on port 27052
adding shards. can take up to 30 seconds...
sent signal 15 to 12 processes.
launching: config server on port 27059
launching: config server on port 27060
launching: config server on port 27061
launching: "mongod" on port 27053
launching: "mongod" on port 27054
launching: "mongod" on port 27055
launching: "mongod" on port 27056
launching: "mongod" on port 27057
launching: "mongod" on port 27058
launching: mongos on port 27050
launching: mongos on port 27051
launching: mongos on port 27052
Username "user", password "password"

# --sharded 2:启用分片集群模式,分片数为2。
# --replicaset : 创建副本集,默认3个节点,可以通过 --nodes 指定复制集节点个数
# --nodes 3:采用3节点的复制集架构,默认就是3个节点。
# --config 3:配置服务器采用3节点的复制集架构模式
# --csrs:指Config Server as a Replica Set,这个也是默认配置
# --mongos 3:启动3个mongos实例进程。
# --auth : 开启默认认证,同单节点
# --port 27050:集群将以27050作为起始端口,集群中的各个实例基于该端口向上递增。

以下命令运行时必须指定数据目录,默认为当前路径下的data目录,可以使用--dir指定数据目录,因为需要通过./data/.mlaunch_startup来获取mongo的构建信息。以分片集群举例:

list: 查看启动信息

  • 查看集群状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 显示mongo进程的列表
$ mlaunch list
Detected mongod version: 7.0.6

PROCESS PORT STATUS PID

mongos 27050 running 61988
mongos 27051 running 61991
mongos 27052 running 61999

config server 27059 running 61903
config server 27060 running 61911
config server 27061 running 61914

shard01
mongod 27053 running 61922
mongod 27054 running 61947
mongod 27055 running 61955

shard02
mongod 27056 running 61964
mongod 27057 running 61972
mongod 27058 running 61980
  • 查看启动命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ mlaunch list --startup
Detected mongod version: 7.0.6

PROCESS PORT STATUS PID STARTUP COMMAND

mongos 27050 running 61988 mongos --logpath /mongodb/mtools/sharded/data/mongos/mongos_27050.log --port 27050 --configdb configRepl/localhost:27059,localhost:27060,localhost:27061 --keyFile /mongodb/mtools/sharded/data/keyfile --fork
mongos 27051 running 61991 mongos --logpath /mongodb/mtools/sharded/data/mongos/mongos_27051.log --port 27051 --configdb configRepl/localhost:27059,localhost:27060,localhost:27061 --keyFile /mongodb/mtools/sharded/data/keyfile --fork
mongos 27052 running 61999 mongos --logpath /mongodb/mtools/sharded/data/mongos/mongos_27052.log --port 27052 --configdb configRepl/localhost:27059,localhost:27060,localhost:27061 --keyFile /mongodb/mtools/sharded/data/keyfile --fork

config server 27059 running 61903 mongod --replSet configRepl --dbpath /mongodb/mtools/sharded/data/configRepl/rs1/db --logpath /mongodb/mtools/sharded/data/configRepl/rs1/mongod.log --port 27059 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --configsvr --wiredTigerCacheSizeGB 1
config server 27060 running 61911 mongod --replSet configRepl --dbpath /mongodb/mtools/sharded/data/configRepl/rs2/db --logpath /mongodb/mtools/sharded/data/configRepl/rs2/mongod.log --port 27060 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --configsvr --wiredTigerCacheSizeGB 1
config server 27061 running 61914 mongod --replSet configRepl --dbpath /mongodb/mtools/sharded/data/configRepl/rs3/db --logpath /mongodb/mtools/sharded/data/configRepl/rs3/mongod.log --port 27061 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --configsvr --wiredTigerCacheSizeGB 1

shard01
mongod 27053 running 61922 mongod --replSet shard01 --dbpath /mongodb/mtools/sharded/data/shard01/rs1/db --logpath /mongodb/mtools/sharded/data/shard01/rs1/mongod.log --port 27053 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --shardsvr --wiredTigerCacheSizeGB 1
mongod 27054 running 61947 mongod --replSet shard01 --dbpath /mongodb/mtools/sharded/data/shard01/rs2/db --logpath /mongodb/mtools/sharded/data/shard01/rs2/mongod.log --port 27054 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --shardsvr --wiredTigerCacheSizeGB 1
mongod 27055 running 61955 mongod --replSet shard01 --dbpath /mongodb/mtools/sharded/data/shard01/rs3/db --logpath /mongodb/mtools/sharded/data/shard01/rs3/mongod.log --port 27055 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --shardsvr --wiredTigerCacheSizeGB 1

shard02
mongod 27056 running 61964 mongod --replSet shard02 --dbpath /mongodb/mtools/sharded/data/shard02/rs1/db --logpath /mongodb/mtools/sharded/data/shard02/rs1/mongod.log --port 27056 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --shardsvr --wiredTigerCacheSizeGB 1
mongod 27057 running 61972 mongod --replSet shard02 --dbpath /mongodb/mtools/sharded/data/shard02/rs2/db --logpath /mongodb/mtools/sharded/data/shard02/rs2/mongod.log --port 27057 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --shardsvr --wiredTigerCacheSizeGB 1
mongod 27058 running 61980 mongod --replSet shard02 --dbpath /mongodb/mtools/sharded/data/shard02/rs3/db --logpath /mongodb/mtools/sharded/data/shard02/rs3/mongod.log --port 27058 --fork --keyFile /mongodb/mtools/sharded/data/keyfile --shardsvr --wiredTigerCacheSizeGB 1


auth: "user:password"

stop: 关闭 mongo进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 关闭全部mongo进程,注意:如果开启了认证,stop会使用默认的用户名和密码进行认证(user/password),否则需要使用 kill
$ mlaunch stop
Detected mongod version: 7.0.6
sent signal 15 to 11 processes.

# 只关闭mongos进程,这里mongos是tag
$ mlaunch stop mongos
Detected mongod version: 7.0.6
sent signal 15 to 2 processes.
# 查看当前mongo进程状态
$ mlaunch list
Detected mongod version: 7.0.6

PROCESS PORT STATUS PID

mongos 27050 down -
mongos 27051 down -

config server 27058 running 23284
config server 27059 running 23287
config server 27060 running 23295

shard01
mongod 27052 running 23303
mongod 27053 running 23326
mongod 27054 running 23334

shard02
mongod 27055 running 23342
mongod 27056 running 23345
mongod 27057 running 23353


auth: "user:password"

start: 启动 mongo进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 启动全部mongo进程
$ mlaunch start
Detected mongod version: 7.0.6
launching: config server on port 27058
launching: config server on port 27059
launching: config server on port 27060
launching: "mongod" on port 27052
launching: "mongod" on port 27053
launching: "mongod" on port 27054
launching: "mongod" on port 27055
launching: "mongod" on port 27056
launching: "mongod" on port 27057
launching: mongos on port 27050
launching: mongos on port 27051

# 只启动当前关闭的mongo进程
$ mlaunch start down
Detected mongod version: 7.0.6
launching: mongos on port 27050
launching: mongos on port 27051

restart: 重启 mongo进程

1
2
3
4
5
6
# 重启,不推荐使用,除了单实例,复制集和分片集群都会出问题
$ mlaunch restart
# 以下是分片集群重启后的报错信息,猜测原因是并没有完全关闭mongo分片集群中的所有节点时就启动了
Detected mongod version: 7.0.6
sent signal 15 to 11 processes.
b'about to fork child process, waiting until server is ready for connections.\nforked process: 20202\nERROR: child process failed, exited with 1\nTo see additional information in this output, start without the "--fork" option.\n'

kill: 杀掉 mongo进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 杀掉全部mongo进程
$ mlaunch kill
Detected mongod version: 7.0.6
sent signal 15 to 11 processes.

# 杀掉某个端口的mongo进程
$ mlaunch kill 27050
Detected mongod version: 7.0.6
sent signal 15 to 1 process.
# 可以看到27050端口的mongo进程的状态为down
$ mlaunch list
Detected mongod version: 7.0.6

PROCESS PORT STATUS PID

mongos 27050 down -
mongos 27051 running 23369

config server 27058 running 23284
config server 27059 running 23287
config server 27060 running 23295

shard01
mongod 27052 running 23303
mongod 27053 running 23326
mongod 27054 running 23334

shard02
mongod 27055 running 23342
mongod 27056 running 23345
mongod 27057 running 23353


auth: "user:password"

# 关闭全部mongos进程
$ mlaunch kill mongos

支持的tags

1
2
3
4
5
6
7
8
all:环境中的所有节点。
running:所有当前运行的节点。
down:目前所有关闭的节点。
mongos:所有mongos进程都带有此标签。
mongod:所有mongod进程(包括仲裁者和配置服务器)。
config:所有配置服务器
<shard name>:对于分片环境,分片的每个成员都带有分片名称作为标签,例如“shard-a”。
<port number>:每个节点都带有其端口号作为标签。