How to convert a MongoDB replica set to a stand alone server

Amol M Kulkarni picture Amol M Kulkarni · Jun 4, 2013 · Viewed 37.5k times · Source

Consider, I have 4 replicate sets and the config is as follows:

{
 "_id": "rs_0",
 "version": 5,
 "members" : [
  {"_id": 1, "host": "127.0.0.1:27001"},
  {"_id": 2, "host": "127.0.0.1:27002"},
  {"_id": 3, "host": "127.0.0.1:27003"},
  {"_id": 4, "host": "127.0.0.1:27004"}
 ]
}

I am able to connect to all sets using mongo --port <port>

There are documents for getting information on Convert a Standalone to a Replica Set, but can anyone tell me how to convert back to standalone from replica set?

Answer

amvalente picture amvalente · Jul 31, 2015

Remove all secondary hosts from replica set (rs.remove('host:port')), restart the mongo deamon without replSet parameter (editing /etc/mongo.conf) and the secondary hosts starts in standalone mode again.

The Primary host is tricky one, because you can't remove it from the replica set with rs.remove. Once you have only the primary node in the replica set, you should exit mongo shell and stop mongo. Then you edit the /etc/mongo.conf and remove the replSet parameter and start mongo again. Once you start mongo you are already in standalone mode, but the mongo shell will prompt a message like:

2015-07-31T12:02:51.112+0100 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.replset

to remove the warning you can do 2 procedures: 1) Droping the local db and restarting mongo:

use local
db.dropDatabase();

/etc/init.d/mongod restart

2)Or if you don't want to be so radical, you can do:

use local
db.system.replset.find()

and it will prompt a message like:

{ "_id" : "replicaSetName", "version" : 1, "members" : [ { "_id" : 0, "host" : "hostprimary:mongoport" } ] }

then you will erase it using:

db.system.replset.remove({ "_id" : "replicaSetName", "version" : 1, "members" : [ { "_id" : 0, "host" : "hostprimary:mongoport" } ] })

and it will probably prompt:

WriteResult({ "nRemoved" : 1 })

Now, you can restart the mongo and the warning should be gone, and you will have your mongo in standalone mode without warnings