How to convert a MongoDB replica set to a stand al

2019-02-02 22:06发布

问题:

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?

回答1:

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



回答2:

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



回答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.



回答4:

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


回答5:

1) Go to mongo shell on Secondary servers

2) Stop the secondary servers by using below command:

   use admin
   db.shutdownServer()

After executing below command. Your primary will automatically become stand alone server.

For more information, please check the below link: http://www.tutespace.com/2016/03/stopping-and-starting-mongodb.html