0

In a Linux machine, I changed the mongod.service file to run a config file I created.

ExecStart=/usr/bin/mongod --config /etc/some_name.conf

When I start mongod with this config file manually, it runs fine. Yet when I use this config file to run the mongod daemon, it fails.

I did run systemctl daemon-reload after the change, yet still failed.

Anybody also dealt with this issue?

Update

I've isolated the error in my log file to this line:

I STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: 
Attempted to create a lock file on a read-only directory: /data/db, terminating
I CONTROL  [initandlisten] now exiting

I've tried changing the ownership of the directory /data and /data/db to mongodb:mongodb but doesn't fix this error. Also changed the permissions to the directories with sudo chmod -R 775 /data but still same error when I run:

sudo service mongod restart
sudo service mongod status

Status is still failed and same error in log file.

3
  • 1
    How did you manually start mongod and which user account were you using? If you ran as root or a user different than that specified in the service definition (normally mongodb), it is likely that you now have some mixed file or directory permissions in your dbPath which are preventing the service from starting. Check the log file specified in the systemLog.path in your /etc/some_name.conf configuration: there should be some indication of why the service was unable to start. You could also check for consistent file & directory permissions in your dbPath.
    – Stennie
    May 2, 2018 at 9:08
  • I am running as root and there was no problem starting the mongod daemon with the default mongod.conf file. I've isolated the issue when starting the mongod daemon with a diff config file (set up for sharding) to not being able to write to /data/db directory, and elaborated the issue above. Tried several approaches to fix this issue but still fails. Not sure if I'm overlooking anything and what it is..
    – rapidDev
    May 3, 2018 at 17:49
  • How did you resolve? You can post an answer for your own question if you worked out a solution.
    – Stennie
    May 4, 2018 at 7:50

2 Answers 2

0

So, you created mongod.service file, did you remember tell to systemd that you are forking that pid to background? If not, systemd will kill that process soon after start.

Then there is that what @stennie wrote, if you start even once mongod as root user, some of the files are created as root:root and after that, nobody else (like user mongod) can not start mongod-process. So, what are those files and directories, which ownership must be set to mongod (or what ever user you use to start mongod-process)

  • mongod.log /var/log/mongod/-directory (or what you wrote at config file)
  • /var/run/mongodb/mongod.pid (or what ever is set at config-file)
  • directory (and files) where your data is.

Log file is important, because after mongod-process can write to it, it will tell what went wrong if something went wrong...

1
  • I only edited a line in the mongod.service file that was previously working with the default mongod.conf file. I just changed path of the config file (to another one I created) the mongod binary should call to run. So far, I've isolated the error to permission issues with writing to the /data/db directory (according to my log file), and failed at several diff attempts to resolve that. Updated my post for more details.
    – rapidDev
    May 3, 2018 at 18:00
0

What worked for me was to delete the 'db' directory in /data/db path and then create a new 'db' directory. That got rid of this error:

I STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: 
Attempted to create a lock file on a read-only directory: /data/db, terminating
I CONTROL  [initandlisten] now exiting 

Another thing that I did was to create a new mongod.service file with only the barebone necessities (the default mongod.service had a lot of extra configurations):

[Unit]
After=network.target

[Service]
ExecStart=/usr/bin/mongod --config /etc/<config_file_name>.conf

[Install]
WantedBy=multi-user.target

After doing this run this command:

sudo service mongod restart
sudo service mongod status

It should be running. Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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