MongoDB提供了mongod命令用于启动MongoDB服务器端;而停止MongoDB服务器却可以通过几种不同的方式完成。下面分别进行介绍。
通过执行下面的语句可以查看启动MongoDB服务器的帮助信息:
mongod --help
# 输出的信息如下:
......
Storage options:
--storageEngine arg What storage engine to use - defaults
to wiredTiger if no data files present
--dbpath arg Directory for datafiles - defaults to
/data/db
--directoryperdb Each database will be stored in a
separate directory
......
MongoDB除了可以将启动的配置参数写入配置文件以外,还可以直接写到mongod的启动命令中。视频讲解如下:
下面通过具体的步骤来进行演示。
(1)创建一个新的目录/data/db2用于存放MongoDB数据文件。
mkdir -p /data/db2
(2)使用mongod命令启动MongoDB服务器。
mongod --dbpath /data/db2/ --port 1234 --fork \
--logpath /data/db2/db2.log --directoryperdb
# 其中:
# --port:指定MongoDB监听的端口。
# --fork:指定MongoDB服务器将运行在后台。
# --logpath:指定MongoDB输出的日志文件。
# --directoryperdb:指定将不同的数据库存放在单独的目录下以方便管理。
# 输出的信息如下:
about to fork child process, waiting until server is ready for connections.
forked process: 28215
child process started successfully, parent exiting
(3)使用mongoshell登录MongoDB,并创建一个新的数据库和集合。
mongo --port 1234
> use demo
> db.test1.insert({_id:'user001',name:'Tom'})
(4)查看目录/data/db2下的目录和文件。
tree /data/db2
# 输出的信息如下:
/data/db2
├── admin
│ ├── collection-0--1928158110699126729.wt
│ └── index-1--1928158110699126729.wt
├── config
│ ├── collection-4--1928158110699126729.wt
│ ├── index-5--1928158110699126729.wt
│ └── index-6--1928158110699126729.wt
├── db2.log
├── demo
│ ├── collection-7--1928158110699126729.wt
│ └── index-8--1928158110699126729.wt
├── diagnostic.data
│ ├── metrics.2022-04-05T07-48-58Z-00000
│ └── metrics.interim
├── journal
│ ├── WiredTigerLog.0000000001
│ ├── WiredTigerPreplog.0000000001
│ └── WiredTigerPreplog.0000000002
├── local
│ ├── collection-2--1928158110699126729.wt
│ └── index-3--1928158110699126729.wt
├── _mdb_catalog.wt
......
# 提示:由于在启动MongoDB服务器时使用了--directoryperdb参数,
# 因此在第(3)步中创建的demo数据库将单独存放一个目录。
停止MongoDB数据库服务器可以通过三种不同的方式来完成。视频讲解如下:
下面通过具体的示例来进行演示。
> use admin
> db.shutdownServer()
# 提示:使用shutdownServer关闭数据库服务器,MongoDB会在关闭前先等待
# MongoDB集群中的从节点与主节点保持同步,这会将数据回滚的可能性降到最低。
> db.adminCommand({"shutdown":1,"force":true})
# 这时会打印下面的错误信息,该信息表示数据库已经停止。
uncaught exception: Error: error doing query: failed:
network error while attempting to run command 'shutdown' on host '127.0.0.1:1234' :
DB.prototype.runCommand@src/mongo/shell/db.js:188:19
DB.prototype.adminCommand@src/mongo/shell/db.js:200:12
@(shell):1:1
kill -2 PID
# 其中:PID是MongoDB的服务器进程号。
以上三种方式都能够安全地停止MongoDB的运行。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。