32

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?

dskrvk
  • 1,318
  • 15
  • 24
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164

5 Answers5

41

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

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
amvalente
  • 542
  • 1
  • 4
  • 7
  • I had to run `rs.initiate()` to recreate the replication cluster again and make the node PRIMARY. Thanks! – tvb Jan 04 '17 at 11:33
  • 7
    ```not authorized on local to execute command { delete: \"system.replset\", deletes: [ { q: { _id: \"rs0\" }, limit: 0.0 } ], ordered: true }``` on doing the remove. What role is needed to be able to delete from ```system.replset```? – Adam Parkin Jan 27 '17 at 22:01
  • in MongoDB 4.0 it seems you can't remove the `replset.*` collections in the `local` db. I had to drop the `local` db – Gianfranco P. Jun 28 '18 at 14:53
17

Just remove a host from replica set (rs.remove('host:port')), relaunch it without replSet parameter and it's standalone again.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 4
    If you don't want it to hold its info you can also delete the `local` db – Sammaye Jun 04 '13 at 09:32
  • 1
    Not working: ``` bippo:PRIMARY> rs.remove('rudi.dev:27017'); { "errmsg" : "exception: can't find self in new replset config", "code" : 13433, "ok" : 0 } bippo:PRIMARY> rs.config() { "_id" : "bippo", "version" : 1, "members" : [ { "_id" : 0, "host" : "rudi.dev:27017" } ] } ``` – Rudi Wijaya May 05 '14 at 08:14
  • Write a complete doc for convert ReplicaSet to single server [tecnicume](http://tecnicume.blogspot.it/2016/02/mongodb-migrazione-da-replicaset.html). – Furetto Feb 03 '16 at 08:17
  • 2
    About the comment suggesting the deletion of the `local` database: You never know what future DB versions will insert into this DB, and the comment may be readable years after when it actually made sense and was applicable, but this could change over time, so that deleting the `local` db could result in a damaged server. Best is to backup the suggestion with a link to the Documentation which claims that it's OK to do that in order to archive one's goals. – Daniel F Sep 11 '18 at 16:14
8

On an Ubuntu Machine

  1. Stop your mongo server
  2. open /etc/mongod.conf
  3. Comment the replication and replSetName line
#replication:
   #replSetName: rs0
  1. Start your mongo server and go to mongo shell
  2. drop local database

use local

db.dropDatabase()
  1. Restart mongo
Raj Nandan Sharma
  • 3,694
  • 3
  • 32
  • 42
3

The MongoDB Documentation suggests the following to perform maintenance on a replica set member, which brings the the replica set member into standalone mode for further operations. With little modification it can be made standalone:

  1. If node in concern is the only node in a shard, drain the chunks to other shards as per MongoDB documentation here, or else the sharded database will break, i.e.
    • Make sure balancer is enabled by connecting to mongos and run sh.startBalancer(timeout, interval)
    • For the shard in concern, go to admin database and db.adminCommand( { removeShard: "mongodb0" } )
    • Check draining status by repeating above removeShard command, wait for draining to complete
  2. If node in concern is primary, do rs.stepDown(300)
  3. Stop the node by running db.shutdownServer()
  4. Change the yaml config by:
    • commenting out replication.replSetName (--replSetName in command line)
    • commenting out sharding.clusterRole for shard or config server (--shardsvc and --configsvr in command line)
    • commenting out net.port, then change it to a different port (--port in command line)
  5. Start the mongod instance
  6. If change is permanent, go to other mongod instance and run rs.remove("host:port")

After this, the node in concern should be up and running in standalone mode.

Charles Lin
  • 624
  • 1
  • 5
  • 5
0

Follow below steps :

  1. Go to mongo shell on Secondary servers

  2. Stop the secondary servers by using below command : use admin db.shutdownServer()

  3. Go to Linux shell- on secondary servers and type below command :

sudo service mongod stop

Starting the MongoDB replication -

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

    sudo service mongod start Starting the MongoDB replication -

  • Go to primary and type below commands to start the replication :

a] rs.initiate()

b] rs.add("Secondar -1:port no")

c] rs.add("Secondary-2:port no")

d] rs.add({ "_id" : 3, "host" : "Hidden_member:port no", "priority" : 0, "hidden" : true })

e] rs.status()

  • 1
    AFAIK your primary will NOT become a standalon server. It will become the primary member of a replica set that contains only a single member. – nagylzs Jan 29 '19 at 07:51