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
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.
How can I run mongos as background process in an EC2 machine?
I am trying to set up a sharded cluster on EC2 machines and I am able to run mongod as background service, but I am not able to run mongos as background service.
mongod --fork --logpath /var/log/mongod.log
To run a mongod process as a daemon (i.e. fork), and write its output to a log file, use the --fork and --logpath options. You must create the log directory; however, mongod will create the log file if it does not exist.
The following command starts mongod as a daemon and records log output to /var/log/mongodb.log.
mongod --fork --logpath /var/log/mongodb.log
You need to start mongos with --fork parameter. It
Enables a daemon mode that runs the mongos process in the background. By default mongos does not run as a daemon: typically you will run mongos as a daemon, either by using --fork or by using a controlling process that handles the daemonization process (e.g. as with upstart and systemd).
You can also try & with the command to run it as background. for ex- mongod &
If you have ran or want to make current process to background the press Ctrl+z and type bg.
I need to be able to start/stop MongoDB on the cli. It is quite simple to start:
./mongod
But to stop mongo DB, I need to run open mongo shell first and then type two commands:
$ ./mongo
use admin
db.shutdownServer()
So I don't know how to stop mongo DB in one line. Any help?
Starting and Stopping MongoDB is covered in the MongoDB manual. It explains the various options of stopping MongoDB through the shell, cli, drivers etc. It also details the risks of incorrectly stopping MongoDB (such as data corruption) and talks about the different kill signals.
Additionally, if you have installed MongoDB using a package manager for Ubuntu or Debian then you can stop mongodb (currently mongod in ubuntu) as follows:
Upstart: sudo service mongod stop
Sysvinit: sudo /etc/init.d/mongod stop
Or on Mac OS X
Find PID of mongod process using $ top
Kill the process by $ kill <PID> (the Mongo docs have more info on this)
Or on Red Hat based systems:
service mongod stop
Or on Windows if you have installed as a service named MongoDB:
net stop MongoDB
And if not installed as a service (as of Windows 7+) you can run:
taskkill /f /im mongod.exe
To learn more about the problems of an unclean shutdown, how to best avoid such a scenario and what to do in the event of an unclean shutdown, please see: Recover Data after an Unexpected Shutdown.
If you literally want a one line equivalent to the commands in your original question, you could alias:
mongo --eval "db.getSiblingDB('admin').shutdownServer()"
Mark's answer on starting and stopping MongoDB via services is the more typical (and recommended) administrative approach.
mongod --dbpath /path/to/your/db --shutdown
More info at official: http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/
If the server is running as the foreground process in a terminal, this can be done by pressing
Ctrl-C
Another way to cleanly shut down a running server is to use the shutdown command,
> use admin
> db.shutdownServer();
Otherwise, a command like kill can be used to send
the signal. If mongod has 10014 as its PID, the command would be
kill -2 10014
I followed the official MongoDB documentation for stopping with signals. One of the following commands can be used (PID represents the Process ID of the mongod process):
kill PID
which sends signal 15 (SIGTERM), or
kill -2 PID
which sends signal 2 (SIGINT).
Warning from MongoDB documentation:
Never use kill -9 (i.e. SIGKILL) to terminate a mongod instance.
If you have more than one instance running or you don't care about the PID, you could use pkill to send the signal to all running mongod processes:
pkill mongod
or
pkill -2 mongod
or, much more safer, only to the processes belonging to you:
pkill -U $USER mongod
or
pkill -2 -U $USER mongod
NOTE:
If the DB is running as another user, but you have administrative rights, you have invoke the above commands with sudo, in order to run them. E.g.:
sudo pkill mongod
sudo pkill -2 mongod
PS
Note: I resorted to using signals because mongod --shutdown, although mentioned in the current MongoDB documentation, did not work on my machine (macOS, mongodb v3.4.10, installed with homebrew):
Error parsing command line: unrecognised option '--shutdown'
Update 2022-05-10
meanwhile option --shutdown is marked in the documentation as "Supported on Linux only".
PPS
(macOS specific) Before anyone wonders: no, I could not stop it with command
brew services stop mongodb
because I did not start it with
brew services start mongodb
I had started mongod with a custom command line :-)
Use mongod --shutdown
According to the official doc : manage-mongodb-processes/
:D
create a file called mongostop.bat
save the following code in it
mongo admin --eval "db.shutdownServer()"
run the file mongostop.bat and you successfully have mongo stopped
My special case is:
previously start mongod by:
sudo -u mongod mongod -f /etc/mongod.conf
now, want to stop mongod.
and refer official doc Stop mongod Processes, has tried:
(1) shutdownServer but failed:
> use admin
switched to db admin
> db.shutdownServer()
2019-03-06T14:13:15.334+0800 E QUERY [thread1] Error: shutdownServer failed: {
"ok" : 0,
"errmsg" : "shutdown must run from localhost when running db without auth",
"code" : 13
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
DB.prototype.shutdownServer#src/mongo/shell/db.js:302:1
#(shell):1:1
(2) --shutdown still failed:
# mongod --shutdown
There doesn't seem to be a server running with dbpath: /data/db
(3) previous start command adding --shutdown:
sudo -u mongod mongod -f /etc/mongod.conf --shutdown
killing process with pid: 30213
failed to kill process: errno:1 Operation not permitted
(4) use service to stop:
service mongod stop
and
service mongod status
show expected Active: inactive (dead) but mongod actually still running, for can see process from ps:
# ps -edaf | grep mongo | grep -v grep
root 30213 1 0 Feb04 ? 03:33:22 mongod --port PORT --dbpath=/var/lib/mongo
and finally, really stop mongod by:
# sudo mongod -f /etc/mongod.conf --shutdown
killing process with pid: 30213
until now, root cause: still unknown ...
hope above solution is useful for your.
Building on the answer from stennie:
mongo --eval "db.getSiblingDB('admin').shutdownServer();quit()"
I found that mongo was trying to reconnect to the db after the server shut down, which would cause a delay and error messages. Adding quit() after shutdown speeds it up and reduces the messages, but there is still one.
I also want to add context - I'm starting and stopping mongod as part of test cases for other code, so registering as a service does not fit the use case. I am hoping to end up with something that runs on all platforms (this tested in windows right now). I'm using mongod 3.6.9
One liners to start or stop mongodb service using command line;
To start the service use: NET START MONGODB
To stop the service use: NET STOP MONGODB
I use this myself, it does work.
From the given commands I think you're on Linux.
Start MongoDB:
$ sudo service mongod start
mongod start/running, process XXXXX
Check the Status:
$ sudo service mongod status
mongod start/running, process XXXXX
Stop MongoDB:
$ sudo service mongod stop
mongod stop/waiting
Using homebrew (recommended way):
To start:
brew services start mongodb-community
To stop:
brew services stop mongodb-community
I simply did:
quit();
Please note I'm using mongo 3.0.
Mongo
in the terminal window on your mac, press control+c
I use this startup script on Ubuntu.
#!/bin/sh
### BEGIN INIT INFO
# Provides: mongodb
# Required-Sart:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mongodb
# Description: mongo db server
### END INIT INFO
. /lib/lsb/init-functions
PROGRAM=/opt/mongo/bin/mongod
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`
test -x $PROGRAM || exit 0
case "$1" in
start)
log_begin_msg "Starting MongoDB server"
ulimit -v unlimited.
ulimit -n 100000
/opt/mongo/bin/mongod --fork --quiet --dbpath /data/db --bind_ip 127.0.0.1 --rest --config /etc/mongod.conf.
log_end_msg 0
;;
stop)
log_begin_msg "Stopping MongoDB server"
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
fi
log_end_msg 0
;;
status)
;;
*)
log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}"
exit 1
esac
exit 0
Windows
In PowerShell, it's: Stop-Service MongoDB
Then to start it again: Start-Service MongoDB
To verify whether it's started, run: net start | findstr MongoDB.
Note: Above assumes MongoDB is registered as a service.
Kindly take advantage of the Task Manager provided by your OS for a quick and easy solution. Below is the screengrab from/for Windows 10. Right-click on the highlighted process and select stop. Select start, if already stopped.
Please Note: Internally the commands are doing the same thing which you have to do manually using a GUI (Task Manager), provided by Windows/your OS. Though, this approach to be used for study/practice purpose to get started and you won't be blocked due to this.
To start
sudo /etc/init.d/mongodb start
To stop
sudo /etc/init.d/mongodb stop
CTRL + C
on the windows command line
I am running Mongodb server as an daemon process,I suspect that the mongo gets crashed and restarted. Is there any file available to confirm the crash.I believe that everyone acquainted about mysql if its get killed and restarted. we can confirm by checking the file. In that same way we do have a file in Mongodb to check it?
By default mongod sends its logs to stdout. From the mongod --help output:
--logpath arg file to send all output to instead of stdout
--logappend append to logpath instead of over-writing
i.e. you can specifiy a log file for the daemon - or you could just use a shell redirection.
You should have a look at how your deamon process is started to find if and where a log file is written.
Some information on your system might also help us be more helpful.