Clean shutdown of the mongod process - mongodb

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.

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

How to shut down mongos server?

I found out that mongod has --shutdown to cleanly shut down a server, what is the corresponding command for a mongos server?
The only way i found out was to simply find the PID for the server and kill -9 it, but it seems like this is not the smartest way to do it.
Using mongodb version 3.0 btw.
Try the following steps:
Login to mongos
switch to admin database
run db.shutdownServer()

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

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.

Start mongod service at system startup

I recently updated mongodb, and I run Linux Mint (an Ubuntu based system) and every time I start it up, i cannot use Robomongo because the service mongod is not automatically started; every time I need to open up a terminal and use
sudo service mongod start
Is there a way to start mongod automatically at system start?
Use the following command to autostart mongodb when system start.
systemctl enable mongod.service
You can either put the command in your /etc/bashrc script under and if condition i.e. if the mongod process is not already running, then start it.
Other way is to modify your /etc/rc.local and add the command to start mongod in that file. It will start at bootup.
Using crontab on Ubuntu 20 you can try this.
crontab -e
And input this as a crontab entry
#reboot service mongod start

mongodb running only while ssh connection is kept

Hello I'm testing mongo experimental version, which doesn't have an installer and is ran from directory by putting ./mongod but the problem is that the process quits whenever I close my ssh connection. What I'm supposed to do to keep it running even after closing ssh connection?
To start the mongod process as a background daemon you should run it with the parameters --fork (run as a daemon) and --logpath (a filename for log output).
You probably want to specify the --dbpath as well (unless you are OK with the default of /data/db).
So putting that together:
$ mongod --fork --dbpath /path/to/data --logpath /path/to/mongod.log
about to fork child process, waiting until server is ready for connections.
forked process: 64043
all output going to: /path/to/mongod.log
child process started successfully, parent exiting
$
After the server starts you will be returned to a command prompt.
If you want to shut down the server gracefully, log in via the mongo shell and run db.shutdownServer().
For more information see Manage mongod Processes in the MongoDB Manual.
It looks like that it is not started as a daemon so when you close your ssh session it got HUP signal. You can avoid it by using:
nohup command-to-start-mongo &