how to set mogodb in upstart service with authentication in mongodb way 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