Home > Back-end >  MongoDB systemctl start failure due to dbpath/keyfile path permission error
MongoDB systemctl start failure due to dbpath/keyfile path permission error

Time:09-16

I have a working mongodb instance that can be run using mongod -f /etc/mongod.conf but it shows systemctl service as failed.

I tried to start it using sudo service mongod start and it fails with exitcode=1

I figured out the permission errors and changed permission of each dbpath and keyfile path by sudo chown -R mongod:root and then it started working under root

I wanna know why it didn't work even under root when dbpath and keyfile path permissions were mongod:mongod and

How to assign directory permission if I want to run systemctl mongod without root access?

OS - RHEL 8 , mongod version 5.0

mongod.conf file

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

storage:
  dbPath: /data/mongodb/mongo
  journal:
    enabled: true

processManagement:
  fork: true  
  pidFilePath: /var/run/mongodb/mongod.pid  
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017
  bindIp: mongo3.xxxxxxx.net  

security:
  authorization: enabled
  keyFile: /data/key/keyfile

replication:
  replSetName: xxxxx

CodePudding user response:

On my system I start the service with sudo systemctl start mongod

You can set everything in the service file, could be like this:

$ systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/etc/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-09-06 16:41:02 CEST; 1 weeks 1 days ago


$ cat /etc/systemd/system/mongod.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
ExecStartPre=/usr/bin/mkdir -p /data/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /data/mongodb
ExecStartPre=/usr/bin/chmod 0755 /data/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking

Note, when you install MongoDB then it creates a default service file at /usr/lib/systemd/system/mongod.service. You should not modify this file, it might be overwritten by your package manager at next update. Instead put your customized file at /etc/systemd/system/mongod.service (see https://unix.stackexchange.com/questions/224992/where-do-i-put-my-systemd-unit-file) - and don't forget systemctl daemon-reload

  • Related