What's difference between using "sudo service mongod start" and "mongod"? - mongodb

I'm using Ubuntu 16.04, and I can't understand what's difference between using
sudo service mongod start
and
mongod
In mongodb official documentation here
said that to start mongodb just use sudo service mongod start, and its log stores in /var/log/mongodb. However, I try to run mongodb using mongod this way, log shows on terminal, and after I turn off the terminal, I can not find the log file.
It is confused.

sudo - Runs the command as root.
service - Manages the following program as a daemon (background process).
mongod - Obviously the MongoDB program in question.
start - A command that tells service what to do with the program in question.
Together, we get "I want to start mongod as a background process, and I want to run it as root so it has permission to do the things it needs to do". Running mongod by itself, however, runs the program in an ordinary fashion, i.e. as a foreground process. Typically you want to run it as a background process so that you're free to do other things, e.g. connecting to the database via shell access.
This is pretty simplified, but it should explain what you actually need to know at this point in time.

Related

Unable to configure mongoDB

Im learning mongoDB and exploring its features, i try several commands when i run the terminal and mongod acts like does not care. It always starts the server even though i simply run for reading help.
Some commands that i run
mongod --help
mongod --port 5000
mongod --dbpath C:\myfolder\myproj\data\db
Everytime, no matter witch i run from the above commands it starts the server always even when searching for help. The port and path do not change also, it looks like it ingores everything other than the mongod in the terminal. Any help?
mongod starts the service, but when you close the terminal then also the service terminates.
However, usually you install mongod as a Service. See mongod.exe for details.
Maybe this package will help you: mongoDB-oneClick

Clean shutdown of the mongod process

My mongodb run under Linux 6. I use the command db.shutdownServer() to close the database but the mongod process does not stop.
Stopping mongo directly with service mongod stop do a clean shutdown?
Thanks for your help
Proper ways to shutdown mongod is described in the documentation. They are:
Use shutdownServer()
From the mongo shell
use admin
db.shutdownServer()
Use --shutdown
From the Linux command line
mongod --shutdown
Use CTRL-C
When running the mongod instance in interactive mode, issue Control-C
Use kill
From the Linux command line
kill mongoProcessID
kill -2 mongodProcessID
So you need to figure out how /etc/init.d/mongodb stop actually stops the process on your Linux distribution. For example, on Debian it uses the wrapper which behaves similar to killall which is a proper method.
As per the documentation of mongodb, mongod --shutdown will work on linux system, but, on Mac, the --shutdown switch is not recognised. However, per the documentation, Ctrl+C will cleanly shutdown the db server. When you do a Ctrl+C on the same terminal where the db server is running, it initiates a dozen or so signalProcessingThread which indicates that the shutdown is proper and smooth. At the end, you can see that the process exits with code:0. Per the convention, Ctrl+C is awkward, but is clean, although not seemingly graceful.

Why does the mongo shell work without mongod server being explicitly run?

Running MongoDB Shell v-3.2.8
I've noticed that articles and tutorials always mention to run the mongod server before running the mongo shell.
However, when I skip the first step and simply type mongo into my terminal, the mongo shell works without any errors / interruptions.
MacBook:Desktop user$ mongo
MongoDB shell version: 3.2.8
connecting to: test
Why does this work? Does mongo call mongod?
The mongod is being ran as a service or daemon, which means that there is always a mongod process running listening to a port. I use ubuntu, and when I install mongodb through the package manager, it immediately starts up a mongod process and begins listening on the standard port.
Running mongo is simply a small utility that attempts to connect to the localhost at the standard ip. The data reading, writing, and querying is done by the mongod process while mongo is a small program that sends the the commands to mongod.
If mongod wasn't running, you would see an error stating "Unable to connect to mongodb server"
I noticed the same. I think mongoose is doing some smart things there, which I'm not sure how it works. But I have noticed such things before, like mongoose will automatically add an "s" to your database's name when you declare it, which is a very thoughtful act =)).
I encountered the same thing and found that in the background services if MongoDB server is running, the mongo shell will work without any error. If we stop the service, the shell will throw an error.

How do I make it so Mongo runs automatically all the time on my Azure server?

I have two Azure virtual machines. On one I have a Mongo server, on the other I just have a service I created which listens to Twitters streaming API and filters tweets.
Neither of these two services work unless I manually activate them and keep my console window open. For example, to run Mongo I need to ssh into my virtual machine and type: mongod --config /etc/mongod.conf. This starts the Mongo server successfully, but if at anytime I close my browser the service stops.
I believe the reason this is occurring is because when I login the system is allocating me a process by which I can navigate around the system and perform commands. When I type mongod --config /etc/mongod.conf I believe I am using that process to run Mongo. I am not sure how to make Mongo run without doing this though.
How do I make it so Mongo runs automatically all the time on my Azure server?
EDIT:
I tried running Mongo as a daemon but I receive an error:
$ mongod --fork --logpath /var/log/mongodb.log
>>>about to fork child process, waiting until server is ready for connections.
>>>forked process: 63470
>>>ERROR: child process failed, exited with error number 1
This issue has nothing to do with Azure; it's all about how you install MongoDB.
If you install mongodb as a service, via apt-get (or whatever other means your version of linux requires), then it will run independent of you being logged in. You shouldn't be running an always-on service through your command shell.
Here are instructions for installing under Ubuntu. You'll see that, once you set up the prerequisite public key and list file, you then run:
sudo apt-get install -y mongodb-org
You can then start and stop the service via
sudo service mongod start
and
sudo service mongod stop
You can enable mongo to autostart on boot by typing this command in your console:
sudo systemctl enable mongod
Then test it by this command:
sudo service mongod status

Mongodb will not start

It has been a while since I have used Mongodb and I am running into a problem. I have an EC2 instance that had Mongodb running on it, but I had turned off the instance for a few months. After turning the instance back on, Mongodb will not start correctly. No log file is being created so I do not know what is causing the problem.
When I type:
sudo service mongodb start
I get the message:
mongodb start/running, process 2432
but mongo is not actually running and a mongod.lock file is being created.
If I type:
mongod
by itself, Mongodb will actually start and be usable. But, I must leave the terminal window up and not cancel out of the command to keep it running. I have to open up a second terminal window to access my databases. I guess my main question is how I get the mongod service to stay running itself without having to leave a terminal window open with the command running.
Sorry if my explanation doesn't make sense. Hopefully you get what I mean. Anyone have any idea what I am missing? I had this problem before months ago and was able to solve it. Sadly, I didn't write down the missing ingredient.
For that, run command using Nohup
nohup mongod &
Then you can terminate you terminal.