How to stop mongo DB in one command

前端 未结 18 1249
小蘑菇
小蘑菇 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: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 :-)

提交回复
热议问题