Unable to start mongodb service with mongodb version 4.4.21 on RHEL 7, required start commands got removed

It is observed that after an upgrade of mongodb to 4.4.21, mongodb service unable to start after a system reboot and reporting error like:

{"t":{"$date":"2023-05-09T07:03:14.573+02:00"},"s":"I",  "c":"CONTROL",  "id":23330,   "ctx":"main","msg":"ERROR: Cannot write pid file to {path_string}: {errAndStr_second}","attr":{"path_string":"/var/run/mo
ngodb/mongod.pid","errAndStr_second":"No such file or directory"}}

On RHEL 7 server. Creating the path and updating the permission temporarly fixing issue however issue will popup after restart of system.
/var/run is a temporary filesystem path and this will be cleared on every reboot.

Upon further verifcation on the commit history, found that this commit removed the required lines from systemd unit file.

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

Could you please look into the issue and fix this?

I hope you understand that if you have to recreate /var/run/mongodb every time you restart it means you lose all your data every time.

It might be what you want. But it is not for most. Having those command in the service file is very dangerous because they have more to do about installating MongoDB rather than running MongoDB. The directories and permissions should be created and set once and only once.

There is no data being kept there, but the pid information.

The /var/run/ directory contains system information data describing the system since it was booted. The files under this directory are cleared (removed or truncated as appropriate) at the beginning of the boot process. The directory is for programs that use more than one run-time file (e.g. utmp file, which stores information about who is currently using the system). The files in this directory are created dynamically by individual services as they start and during the boot time.

and the pid path/ file generation should be taken care by the systemd unit files. Hope the situation is more clear now!

1 Like

I recently setup a new install on Almalinux and am faced with the same problem. There seems to be a lot of posts out there regarding this that point to adding the directory creation/permission entries to the service file which I did. Each time our servers patch and mongodb updates, the changes are reverted and I am faced with an unhappy service owner. What is the real ‘fix’ for this??

For anyone coming across this the systemd file should have the following entry…


And this will handle the creation of the respective run directory at startup.

Last time I checked the official mongodb packages this was in place.