1、进入cd /lib/systemd/system目录下
2、创建编辑 vi mongodb.service
3、把内容粘贴到mongodb.service中,保存退出
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/bin/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
4、执行此命令 systemctl start mongodb.service
如果报错执行 systemctl daemon-reload