1、编辑mongodb.service文件
将MongoDB设置成系统服务,就可以通过systemctl进行启动停止重启,在目录/etc/systemd/system下编写mongodb.service文件:
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit] Description=MongoDB Database Server Documentation=https://www.mongodb.com/docs/ After=network.target [Service] ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/mongod.conf Restart=always LimitNOFILE=64000 LimitNPROC=64000 [Install] WantedBy=multi-user.target |
2、重新加载systemd服务配置
|------------------------------|
| sudo systemctl daemon-reload |
3、设置开机自启动
|-------------------------------|
| sudo systemctl enable mongodb |
4、MongoDB常用命令
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 启动MongoDB服务:sudo systemctl start mongodb 停止MongoDB服务:sudo systemctl stop mongodb 重启MongoDB服务:sudo systemctl restart mongodb 查看MongoDB服务状态:sudo systemctl status mongodb |