How to keep mongod instance running on Ubuntu 20.04 server

Hello. I’m still using mongo 3.6 version. I’m running mongod by sudo mongod --port 27017 --dbpath /var/lib/mongodb . But after closing the terminal, mongodb instance stops running and I can’t access to the connection via mongo shell.

Hi @Patrick_Edward, welcome to the community :wave:.

Quick fix:

Add the --fork flag to your command along with a logpath, therefore your command would look something like:

sudo mongod --port 27017 --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongod.log --fork

And hopefully this would successfully fork a child-mongod-process and you can close the terminal and it won’t stop the process from running

More details:

Ideally, in production, you might want to create a mongod-config file. The configuration file contains settings that are equivalent to the mongod command-line options. Hence you don’t have to worry about the length and any typos/syntax in your command. Learn more about mongod configuration file through our awesome documentation about Configuration File Options.
Also, checkout Configuration File Options for v3.6.

Having said that, I would highly recommend going through our free MongoDB University course on Basic Cluster Administration.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

@SourabhBagrecha Hello. I know that I should learn mongodb from the zero but I’m having tight deadline. So I hope you don’t mind me. I would like to ask you a few more questions.
When I run

sudo mongod --port 27017 --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongod.log --fork

I have an issue that the mongod instance should not be initiated as root user.

So I run the same cmd without sudo, I got permission denied. What’s the practice of doing this process?

Thanks for your time and support.

Best,
Patrick

Hi @Patrick_Edward, whay do you mean when you say:

Can you please explain more about what issues you are facing?

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

I’m having this warning message in mongo shell.
WARNING: You are running this process as the root user, which is not recommended.

Hi @Patrick_Edward, can you please follow the instriction mentioned in this guide: Procedure to Allow Non-Root Users to Stop/Start/Restart “mongod” Process.

Hopefully you’ll be able to get rid of that warning.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

@SourabhBagrecha Thanks for the guide. I will read it tonight. I have another question to ask you if it’s ok for you.
Is it normal to get Unit mongod.service could not be found. when I cmd service mongod status although I can access to mongod through mongo shell?

No worries @Patrick_Edward, usually the unit mongodb.service is masked.
Use following command to unmask it:

sudo systemctl unmask mongod

and re-run

sudo service mongod start

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like