How to stop mongo DB in one command

前端 未结 18 1221
小蘑菇
小蘑菇 2020-12-04 05:21

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 m

相关标签:
18条回答
  • 2020-12-04 05:31

    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
    
    0 讨论(0)
  • 2020-12-04 05:36

    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 this option, because mongod --shutdown, although mentioned in the current MongoDB documentation, curiously doesn't work on my machine (macOS, mongodb v3.4.10, installed with homebrew):
    Error parsing command line: unrecognised option '--shutdown'

    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 :-)

    0 讨论(0)
  • 2020-12-04 05:37

    I simply did:

    quit();

    Please note I'm using mongo 3.0.

    Mongo

    0 讨论(0)
  • 2020-12-04 05:38

    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.

    0 讨论(0)
  • 2020-12-04 05:41

    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.

    0 讨论(0)
  • 2020-12-04 05:43

    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.

    0 讨论(0)
提交回复
热议问题