摘要
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
1 pip3 install python-dateutil psutil pymongo
1 pip3 install 'mtools[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 $ 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"
构建复制集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ 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"
构建分片集群
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 $ 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"
以下命令运行时必须指定数据目录,默认为当前路径下的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 $ 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 $ mlaunch stop Detected mongod version: 7.0.6 sent signal 15 to 11 processes. $ mlaunch stop mongos Detected mongod version: 7.0.6 sent signal 15 to 2 processes. $ 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 $ 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 $ 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 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 $ mlaunch kill Detected mongod version: 7.0.6 sent signal 15 to 11 processes. $ mlaunch kill 27050 Detected mongod version: 7.0.6 sent signal 15 to 1 process. $ 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" $ 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>:每个节点都带有其端口号作为标签。