Wednesday, September 26, 2012

Mongodb localhost Replica set configuration db.runCommand("replSetInitiate") try querying local.system.replset to see current configuration

Bad error message, once the replica set is initialized the config contents are stored persisted on disk. If the replica set is started the first time you can call replSetInitiate.

After the first time, if a replica set has been previously initiated then you have to fetch the existing configuration, modify it then call rs.reconfigure(arg) where arg is the configuration.

Set a variable to hold the existing configuration into variable conf using:
var conf=rs.conf()

This is Javascript, safer to declare your variables with var to set the scope to your execution environment else these variables are global.

then modfiy the conf variable to what you need. Print out conf
conf and look to see if anything is incorrect. If the variable doesnt appear then it doesnt exist and you have to initialize it.

To test if the replica set is initialized, run rs.conf(). If it returns null, it has not been initialized. Make sure you log into the

[dc@localhost ~]$ mongo localhost:10001
MongoDB shell version: 2.2.0
connecting to: localhost:10001/test
> use admin
switched to db admin
> conf = rs.conf()
null

This means there is no primary and secondary elected and no contact between the replica set members.

The instructions for initializing a replica set are not listed under replica sets: http://docs.mongodb.org/manual/reference/replica-configuration/#example-document

but under converting replica sets to replicated sharded cluster!

http://cookbook.mongodb.org/operations/convert-replica-set-to-replicated-shard-cluster/


> use admin
switched to db admin
> db.runCommand({"replSetInitiate" : {"_id" : "rs1", "members" : [{"_id" : 1, "host" : "localhost:10001"}, {"_id" : 2, "host" : "localhost:10002"}, {"_id" : 3, "host" : "localhost:10003"}]}})
{
"info" : "Config now saved locally.  Should come online in about a minute.",
"ok" : 1
}
>

After a minute, verify using the web interface at ports localhost:11001, 11002, 11003 the replica sets are started. 

      15:46:22 [conn3] replSet replSetInitiate config object parses ok, 3 members specified
           15:46:22 [conn3] replSet replSetInitiate all members seem up
           15:46:23 [conn3] replSet info saving a newer config version to local.system.replset
           15:46:23 [conn3] replSet saveConfigLocally done
           15:46:23 [conn3] replSet replSetInitiate config now saved locally.  Should come online in about a minute.
           15:46:25 [rsStart] replSet I am localhost:10001
           15:46:25 [rsHealthPoll] replSet member localhost:10003 is up
           15:46:25 [rsHealthPoll] replSet member localhost:10002 is up
           15:46:25 [rsStart] replSet STARTUP2
           15:46:26 [rsSync] replSet SECONDARY
           15:46:27 [rsHealthPoll] replSet member localhost:10003 is now in state STARTUP2
           15:46:27 [rsHealthPoll] replSet member localhost:10002 is now in state STARTUP2
           15:46:27 [rsMgr] not electing self, localhost:10003 would veto
           15:46:27 .
           15:46:33 [rsMgr] replSet info electSelf 1
           15:46:33 [rsMgr] replSet PRIMARY
           15:46:35 [rsHealthPoll] replSet member localhost:10002 is now in state RECOVERING
           15:46:35 [rsHealthPoll] replSet member localhost:10003 is now in state RECOVERING
           15:46:45 [rsHealthPoll] replSet member localhost:10003 is now in state SECONDARY
           15:46:45 [rsHealthPoll] replSet member localhost:10002 is now in state SECONDARY


The above listing shows localhost:10001 has been elected the primary and can communicate to the other 2 nodes and we have a quorum or an odd number of members to perform a majority vote. 


Verify using rs.conf() we have replica set information stored.

> rs.conf()
{
 "_id" : "rs1",
 "version" : 1,
 "members" : [
  {
   "_id" : 1,
   "host" : "localhost:10001"
  },
  {
   "_id" : 2,
   "host" : "localhost:10002"
  },
  {
   "_id" : 3,
   "host" : "localhost:10003"
  }
 ]
}
rs1:PRIMARY> 


This is a minimal setting for the replica set where we don't have parameters for priority set. You have to add these. 


Once the replica set is initialized you will see the mongo prompt change from > to rs1:PRIMARY>






No comments:

Post a Comment