MongoDB设置开机自启动

一、没有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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值