一、没有mongodb.service文件
起初,在安装mongodb的bin文件夹中运行如下命令会报错:
[root@centos-3 bin]# sudo systemctl start mongodb
Failed to start mongodb.service: Unit not found.
1)在/etc/systemd/system目录下创建一个mongodb.service文件
2)利用vi编辑器打开这个配置文件进行配置
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/mongodb-linux-x86_64-3.6.13/bin/mongod --config /etc/mongodb/primary.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/mongodb-linux-x86_64-3.6.13/bin/mongod --shutdown --config /etc/mongodb/primary.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
~
这里的ExecStart写自己的mongodb的绝对路径以及配置文件的绝对路径
二、配置文件报错
如果自己的mongodb.service文件为空白。进行增加网络上其他的配置文件会报错
[root@centos-3 bin]# sudo systemctl status mongodb.service
● mongodb.service
Loaded: error (Reason: Invalid argument)
Active: inactive (dead)
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:2] Assi...g.
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:3] Assi...g.
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:4] Assi...g.
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:5] Assi...g.
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:6] Assi...g.
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:7] Assi...g.
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:8] Assi...g.
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:9] Assi...g.
Feb 24 10:06:48 centos-3 systemd[1]: [/etc/systemd/system/mongodb.service:10] Ass...g.
Feb 24 10:06:48 centos-3 systemd[1]: mongodb.service lacks both ExecStart= and Ex...g.
Hint: Some lines were ellipsized, use -l to show in full.
所以需要一中提到的配置文件进行配置。
三、启动并设置开机自启动
1.mongodb.service文件给与754权限
[root@centos-3 bin]# chmod 754 /etc/systemd/system/mongodb.service
2.启动并且设置开机自启动
启动
sudo systemctl start mongodb
查看启动信息
sudo systemctl status mongodb
永久启动
sudo systemctl enable mongodb
关闭自启动服务
systemctl disable mongod.service
停止服务
systemctl stop mongod