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

回眸只為那壹抹淺笑 提交于 2019-11-30 02:55:25

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

amvalente

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

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!