1.在/lib/systemd/system/目录下新建mongodb.service文件:
vim /usr/lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/home/software/mongodb-linux-x86_64-4.0.7/bin/mongod --config /home/software/mongodb-linux-x86_64-4.0.7/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/home/software/mongodb-linux-x86_64-4.0.7/bin/mongod --shutdown --config /home/software/mongodb-linux-x86_64-4.0.7/bin/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
上面的路径根据自己实际文件的路径来填
2.设置权限
cd /usr/lib/systemd/system
chmod 754 mongodb.service
3.启动关闭服务,设置开机启动
启动
systemctl start mongodb.service
关闭
systemctl stop mongodb.service
开机启动
systemctl enable mongodb.service
然后重启服务器看是否自动开启了