10

I have installed a mongodb instance using yum.. Now everything works fine. I started the service using service mongod start. It works well. Then I changed the data directory and log path in the configuration file. I restarted the server again and I started the service. But I get the below error:

Restarting mongod (via systemctl):  Job for mongod.service failed. See 'systemctl status mongod.service' and 'journalctl -xn' for details.
                                                           [FAILED]

When I give systemctl status mongod.service I get the below:

 Loaded: loaded (/etc/rc.d/init.d/mongod)
   Active: failed (Result: exit-code) since Wed 2015-03-18 11:35:56 IST; 22s ago
  Process: 10672 ExecStop=/etc/rc.d/init.d/mongod stop (code=exited, status=0/SUCCESS)
  Process: 10841 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=1/FAILURE)
 Main PID: 10509 (code=exited, status=0/SUCCESS)

Mar 18 11:35:56 localhost systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
Mar 18 11:35:56 localhost runuser[10850]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Mar 18 11:35:56 localhost runuser[10850]: pam_unix(runuser:session): session closed for user mongod
Mar 18 11:35:56 localhost mongod[10841]: Starting mongod: [FAILED]
Mar 18 11:35:56 localhost systemd[1]: mongod.service: control process exited, code=exited status=1
Mar 18 11:35:56 localhost systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database..
Mar 18 11:35:56 localhost systemd[1]: Unit mongod.service entered failed state.

When I give journalctl -xn I get the below:

-- Logs begin at Wed 2015-03-18 08:56:56 IST, end at Wed 2015-03-18 11:35:56 IST. --
Mar 18 11:30:01 localhost systemd[1]: Starting Session 20 of user root.
-- Subject: Unit session-20.scope has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit session-20.scope has begun starting up.
Mar 18 11:30:01 localhost systemd[1]: Started Session 20 of user root.
-- Subject: Unit session-20.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit session-20.scope has finished starting up.
-- 
-- The start-up result is done.
Mar 18 11:30:01 localhost CROND[10712]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 18 11:35:56 localhost systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
-- Subject: Unit mongod.service has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mongod.service has begun starting up.
Mar 18 11:35:56 localhost runuser[10850]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Mar 18 11:35:56 localhost runuser[10850]: pam_unix(runuser:session): session closed for user mongod
Mar 18 11:35:56 localhost mongod[10841]: Starting mongod: [FAILED]
Mar 18 11:35:56 localhost systemd[1]: mongod.service: control process exited, code=exited status=1
Mar 18 11:35:56 localhost systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database..
-- Subject: Unit mongod.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

Can anyone help me to fix this? Thanks!!!

P.S.: The data directory I have created has the all permissions for the user. But again if I change the data directory to default (/var/lib/mongodb) it works fine.

1

9 Answers 9

6

I ran into a similar issue and found it to be a misconfigured mongod.conf file in my case. It could also be the permissions on the new directory are not set properly. chown -R mongod:mongod <directory name> was how I ensured access (and of course the chmod 600 <dir> as well). Lastly, run an ls -Z to ensure the context is correct. I just compared to the default directory which worked for me.

If this is not resolved already, please show the contents of your log file as well. There may be some clues there.

0
5

I had this on Raspberry Pi as well as on my Ubuntu server.

“Job for mongod.service failed. See 'systemctl status mongod.service' and 'journalctl -xn' for details.”

I had this issue on different occasions for different reasons:

  1. Mis-named .conf file – The mongodb script (which I moved to /etc/init.d/mongodb), line 57 CONF=/etc/mongod.conf when my actual file was /etc/mongodb.conf. Changing line 57 corrected it. Also, I could have changed the script name just the same.

  2. mongod.lock file – The last time mongod stopped it didn’t get a chance to close the database. This leaves a file in your database folder called mongod.lock. Inside the folder is a number (I believe it’s the PID that mongo was using last). If this file exists you will not be able to start the mongod service. Delete the file and try again.

  3. mongo user – I needed to create a linux user that would be responsible for starting and running the mongod.service. I named mine mongo and updated my /etc/init.d/mongodb script, line 95 for me, to say DAEMONUSER=${DAEMONUSER:-mongo}. This of course will only work if you have created a new user named mongo (or whatever you want I think).

  4. DB Permissions – This is a popular one. Once you declare where the database folder will be located you need to ensure that your ‘mongo’ user has ownership of that file. For example, my database is stored at /data/db. I ran the following command:
    sudo chown –R mongo:mongo /data
    and this moved ownership of /data and all its sub-directories to the mongo user.

  5. Wrong service – This one was a little embarrassing for me. I was trying to start mongo as the service instead of mongod. mongo is the shell that you can run and input commands manually straight to mongo. This is how I created my database and added a few objects for example. mongod on the other hand is the mongo daemon that runs in the background hosting your database for other applications you write/use to access. Make sure you don’t mix them up anywhere in your conf files, scripts, etc.

Hope one of these will fix the problem for you.

On a side note, my mongodb.conf file is empty. Even with it empty, though, you need to point to it correctly.

4

Expanding on what @mustaccio said, the answer for me was the SELinux context on the new logpath and dbpath. I ran the following commands and all was well:

sudo chcon -Rv --type=mongod_log_t $logpath
sudo chcon -Rv --type=mongod_var_lib_t $dbpath

(this was on RHEL 7.1 btw)

0
2

I've tried this and it worked.

sudo chown -R mongodb:mongodb /var/log/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chmod -R 755 /var/lib/mongodb
sudo chmod -R 755 /var/log/mongodb
1

I ran into this problem upgrading from mongo that comes with Centos 7 Repos, to the Mongos own repos. In effect upgrading from V2 to V3.

Turns out that centos 7 repo requires the user mongodb, while mongos own repo wants the user mongod

In the end it was the log file that still existed from the old installation that the new installation couldn't write to, as the usernames were different and I hadn't noticed.

0
1

On my system (Fedora) I have "/var/log" on a tmpfs (ram), si each time I reboot everything on that partition gets lost. Many people to this because they have SSD drives and want to reduce the I/O (saving drive lifetime).

The solution is to create the /var/log/mongodb directory and set mongodb as owner every time the system reboots.

Use a script like:

#!/bin/sh
sudo mkdir /var/log/mongodb
sudo chown mongodb:mongodb /var/log/mongodb

If you are not sure what is the user used by mongod just do:

cat /etc/passwd | grep mongo

Add the script to your system startup.

0

You don't edit the path. I solved the problem by the command:

mongod --dbpath /data/mongo
0
0

This is a permission issue. when we change path of data directory or log file then we have to give permissions to the new directory. Then it runs fine. If any problem occurred like this firstly check the log file "mongod.log".

1
  • This didn't help, I gave full permission to the "Log" folder to the user running the mongod service, I'm using Windows Server 2016. Still can't restart the service after changing the path to the log file, still getting error: "The service is not responding to the control function.", any idea please? Sep 12, 2019 at 11:22
0

Stop MongoDB Server:

service mongod stop

Copy mongo directory to new directory:

rsync -av /var/lib/mongo /home/data/

Rename old directory:

mv /var/lib/mongo /var/lib/mongo.bak

Symlink to the new location:

ln -s /home/data/mongo /var/lib/mongo

Start MongoDB Server:

service mongod start

Not the answer you're looking for? Browse other questions tagged or ask your own question.