mongodb sets the method to run in the background

  • 2020-05-15 02:27:37
  • OfStack

By default, shell is turned off and mongodb stops running.

If you want to run in the background, just add the --fork function at startup.

You can add --logappend after the log path to prevent logs from being deleted.


bin/mongodb  --fork --dbpath=//  --logpath=//  --logappend

Running in the background, if you want to turn it off, you need to send it shutdownServer()

1. General commands:


$ ./mongod
> use admin
> db.shutdownServer()

Note that this command is only allowed locally, or on an authenticated client.

2. If this is a master-slave replication cluster, follow the steps below to close it after version 1.9.1
Check the data update time from Mongodb
If the time difference between Mongodb and the master is more than 10, mongodb will not be turned off (in this case, we can configure timeoutSecs to update the data from Mongodb).
If one of them is within 10 seconds of the main service, the primary server will shut down and wait for the update from Mongodb to complete and shut down.
3. If there is no up-to-date from Mongodb and you want to force the shutdown of the service, you can do so by adding force:true; The command is as follows:

> db.adminCommand({shutdown : 1, force : true})
> //or
> db.shutdownServer({force : true})

4, specify a specific timeout time to shut down the server, command as above, plus 1 timeoutsec: parameter
> db.adminCommand(shutdown : 1, force : true, timeoutsec : 5)
> //or
> db.shutdownServer({force : true, timeoutsec : 5})


Related articles: