I set username and password in MongoDB every time I have to run this command in terminal:

sudo mongod --auth --port 27017 --dbpath /var/lib/mongodb

As soon as I close terminal, MongoDB db also closes so how will I set MongoDB in startup service?


You need to use sudo service to start MongoDB as a service, check the documentation for further details:

sudo service mongod --auth --port 27017 --dbpath /var/lib/mongodb

How to install on Ubuntu is handled extremely well in the MongoDB Docs so I won't rehash that here (plus it is updated more frequently than this answer will be).

Once you have installed MongoDB using that guide you can then modify the config file at /etc/mongod.conf to reflect your desired config (auth etc.). Finally, you then start/stop the database with sudo service mongod start and sudo service mongod stop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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