How to shut down replica set in mongodb?
Asked Answered
P

8

13

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?

Pia answered 4/3, 2013 at 3:59 Comment(0)
F
17

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")'
Fraise answered 4/3, 2013 at 8:7 Comment(0)
A
14

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.

Allodium answered 2/5, 2015 at 5:53 Comment(1)
This answer is not fully correct either. step 2 => rs.stepDown(seconds) on the primary; will never succeed because the primary cannot elect other nodes before stepping down as they are 'frozen'. I think you may get an error "No electable secondaries caught up.... "Contexture
D
4

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

Dray answered 16/3, 2016 at 13:34 Comment(1)
The given link is not available now.Balkhash
J
2

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

Jonquil answered 4/3, 2013 at 6:33 Comment(1)
ps -ef seems to print too much information. How about ps -e instead?Pia
O
1

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

Oswaldooswalt answered 4/3, 2018 at 12:48 Comment(1)
This is solving the issue for meCaa
O
0
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.

Oxyacetylene answered 7/3, 2013 at 7:43 Comment(0)
C
0

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})"
Ceuta answered 15/11, 2016 at 15:0 Comment(0)
K
0

shutdown secondaries first to prevent rollbacks.

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

source: mongodb university course

Ki answered 19/2, 2018 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.