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

40,900

Solution 1

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

Solution 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

Solution 3

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

Solution 4

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.

Share:
40,900
Amol M Kulkarni
Author by

Amol M Kulkarni

Fell in ♥ with Codes... Especially JS ;-) Currently Working on broader web platform (targeting all possible devices and browsers)You'll find me contributing, authoring many projects across the internet. I also ♥ helping others in solving technical problem & I believe this is a way to stay up-to-date and give something back to the community from where I have learnt and still learning :) Worked on: ADO.NET, AJAX, ASP.NET, C, C#, C++, Corel draw, CouchDB, CouchBase, Crystal Reports, CSS, DHTML, Dreamweaver, EJS, Express Framework Node.js, Go-lang, HTML, Infragistics, Jade, Java, Java Applets, JavaScript, jQuery, JSP, Membase, Memcache, Microsoft SQL Server, MongoDB with Python and Node.js, MongoDB(DBA), MongoDB (4.2-Basics), MongoDB New Features and Tools in 4.2, MongoDB Performance Tuning, MS-DOS, Netbeans, No SQL, Node.js, Oracle, OAuth, PageMaker, PhotoShop, Servlets, Socket.io, SwishMax, Tally, VBA, VBScript, Visual Basic, Visual InterDev, Windows Mobile C#, XHTML, XML, XQuery, XSLT, Python, PHP, Kafka, Storm, 0MQ (ZMQ), Redis, Hadoop Favorite Quotation: "Only He Who Can See The Invisible Can Do The Impossible." profile for Amol M Kulkarni on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/2185406.png

Updated on August 15, 2020

Comments

  • Amol M Kulkarni
    Amol M Kulkarni over 3 years

    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?