1.检查进程
因为我之前通过mongod --config mongodb.conf 启动了mongodb ,需要先终止进程;
a)先查看进程:
ps -ef | grep mongodb
b)终止进程16214
kill -2 16124
如图:
2. 编写自定义服务
a)在/lib/systemd/system/目录下新建mongodb.service文件
[root@VM-0-3-centos system]# touch mogodb.service
b)编写内容:要记得修改[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-reload
c)设置权限
chmod 754 mongodb.service
3.开机自启动
#启动服务
[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 查看服务状态