/tmp/mongodb-27017.sock file premission changed when run mongod command

The “mongod” command will reset the owner and group of the sock file.
every time when I run this command it will change the owner and group from mongod to root
due to this service not running and showing a service stop get in log when I restart the service
"ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}
after chown the mongod:mongod /tmp/mongodb-27017.sock it will work but I want a permanent solution. is there any way?

You should not run mongod as root
How did you install mongodb?
If installed to start as service use sudo sysctl command
Check your installation instructions for exact command
If you want to start mongod manually try to run the command as normal user
If you run just mongod it tries start the instance on default port & default dirpath /data/db
If you want to start your mongod on another port & dirpath pass these parameters on command line
mongod --port 28000 --dbpath yourdir --logpath etc
Check mongodb documentation for various command line params like fork etc

I have installed mongodb using Yum and I start the service using systemctl mongod start the service is running.

but when I run mongod command as root it will change the sock file permission.

If it is working with sysctl why you are running mongod as root again?
If you try to run it as root alk permissions on dirs,tmp files will change
You have to use sudo chmod commands to change it back to mongod owner

This is the expected outcome when running as root.

If you feel the need to run mongod manually do it as mongodb. sudo -u mongodb mongod ....

1 Like