How to stop mongo DB in one command

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

    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

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

    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.

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

    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

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

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

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

    Using homebrew (recommended way):

    To start:

    brew services start mongodb-community

    To stop:

    brew services stop mongodb-community

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