Huo
Huo

Reputation: 808

How to shut down replica set in mongodb?

I use ubuntu.

I start three mongod replica set as follows:

$ mongod --replSet setname --logpath "1.log" --dbpath /data/rs1 --port 27017 --smallfiles --fork
$ mongod --replSet setname --logpath "2.log" --dbpath /data/rs2 --port 27018 --smallfiles --fork
$ mongod --replSet setname --logpath "3.log" --dbpath /data/rs3 --port 27019 --smallfiles --fork

How can I shut them down?

Upvotes: 13

Views: 24217

Answers (8)

Shivam Nagpal
Shivam Nagpal

Reputation: 11

This is an old question but I still think that I should answer this.

Run the following command in the unix shell

ps -ef | grep mongo

This would give you a list of pid's (process ids) corresponding to the port 27017, 27018 and 27019. You can use the pid's to kill all the members of replicaset

Upvotes: 1

laike9m
laike9m

Reputation: 19388

I don't think the accepted answer is correct. After some googling, I found this in mongodb-user group to be the best answer.

To shutdown:
- Run db.runCommand({ replSetFreeze: numOfSeconds }) on secondaries to prevent from promoting to primary
- Run rs.stepDown(seconds) on the primary; it will check to make sure at least one of the secondaries is sufficiently caught up to oplog before stepping down. Choose a reasonably long wait time.
- Once everything is a secondary, db.shutdownServer() on everything

To start up:
- Run rs.freeze(seconds) on both secondaries with a lengthy timeout (say, 1-2 minutes)
- Bring up primary
- Use stepDown to fix in case a secondary somehow becomes primary

Simply shutting down is not enough, you have to prevent secondary nodes from promoting to primary.

Upvotes: 14

Shrinivas Kalangutkar
Shrinivas Kalangutkar

Reputation: 1009

1) Login to mongo shell on Secondary servers

2) Stop the secondary servers by using below command:

use admin
db.shutdownServer()

2] Go to Linux shell- on secondary servers and type below command:

sudo service mongod stop

For more information, Please check the below link:

http://www.tutespace.com/2016/03/stopping-and-starting-mongodb.html

Upvotes: 4

user8205152
user8205152

Reputation:

shutdown secondaries first to prevent rollbacks.

mongo admin --port <port> --eval "<db.auth if needed>;db.shutdownServer()"

source: mongodb university course

Upvotes: 0

Michael Sebald
Michael Sebald

Reputation: 195

In MongoDB 3.2 on Windows, I am using the following sequence:

mongo mongodb://localhost:27019/admin --eval "db.shutdownServer({timeoutSecs: 10})"
mongo mongodb://localhost:27018/admin --eval "db.shutdownServer({timeoutSecs: 10})"
mongo mongodb://localhost:27017/admin --eval "db.shutdownServer({timeoutSecs: 10})"

Upvotes: 0

John Feibusch
John Feibusch

Reputation: 11

mongod --port 27017 --shutdown 
mongod --port 27018 --shutdown
mongod --port 27019 --shutdown

By the way, if there is any possibility you will use sharding in the future, it would be best to avoid ports 27018 and 27019. Those are default ports for some components of a shard cluster.

Upvotes: 0

Asya Kamsky
Asya Kamsky

Reputation: 42352

Run the following commands from the Unix shell:

mongo --port 27017 --eval 'db.adminCommand("shutdown")'
mongo --port 27018 --eval 'db.adminCommand("shutdown")'
mongo --port 27019 --eval 'db.adminCommand("shutdown")'

Upvotes: 17

Silver_Clash
Silver_Clash

Reputation: 405

Use ps -ef | grep mongod, get pids for mongod with port numbers 27017-27019, and kill processes with thats pids.

Upvotes: 2

Related Questions