我的环境
centos版本
[root@localhost /]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
mongodb信息
版本号:6.0.5
服务端绝对路径:/usr/local/mongodb/server/bin/mongod
配置文件绝对路径:/usr/local/mongodb/mongo.conf
开机脚本
#进入系统服务目录,创建mongod.service文件
[root@localhost /]# cd /usr/lib/systemd/system
[root@localhost system]# vi mongod.service
脚本内容
[Unit]
Description=Mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/server/bin/mongod -f /usr/local/mongodb/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/server/bin/mongod -f /usr/local/mongodb/mongo.conf --shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
配置完成保存,在当前**/usr/lib/systemd/system**目录可以看到完整的文件信息,表示已经创建完成
-rw-r--r--. 1 root root 368 May 13 13:41 mongod.service
开启服务
执行systemctl enable mongod添加到开机启动
[root@localhost system]# systemctl enable mongod
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
[root@localhost system]#
常用的命令
# 重启
systemctl restart mongod
# 查看状态
systemctl status mongod
# 关闭服务
systemctl stop mongod
# 启动服务
systemctl start mongod
# 禁止开机启动
systemctl disable mongod