1.检查进程
因为我之前通过mongod --config mongodb.conf 启动了mongodb ,需要先终止进程;
a)先查看进程:
ps -ef | grep mongodbb)终止进程16214
kill -2 16124如图:

2. 编写自定义服务
a)在/lib/systemd/system/目录下新建mongodb.service文件
[root@VM-0-3-centos system]# touch mogodb.serviceb)编写内容:要记得修改[service]中的mongod及mogodb.conf的路径!
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/bin/mongod --config /usr/local/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/bin/mongod --shutdown --config /usr/local/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target然后重新加载服务配置文件:
systemctl daemon-reloadc)设置权限
chmod 754 mongodb.service3.开机自启动
#启动服务
[root@VM-0-3-centos system]# systemctl start mongodb
#关闭服务
[root@VM-0-3-centos system]# systemctl stop mongodb
#设置为开机自启动
[root@VM-0-3-centos system]# systemctl enable mongodb
如图:

可以通过 :systemctl status mongodb 查看服务状态
版权声明:本文为weixin_44843687原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。