chtefi
8/21/2015 - 11:40 PM

MongoDB - Create a Replica Set

MongoDB - Create a Replica Set

$ cd dbs
$ mkdir p s1 s2
$ mongod --port 27017 --dbpath p --replSet rs0 --smallfiles --oplogSize 128
$ mongod --port 27018 --dbpath s1 --replSet rs0 --smallfiles --oplogSize 128
$ mongod --port 27019 --dbpath s2 --replSet rs0 --smallfiles --oplogSize 128

$ mongo --port 27017
> rs.status()
> rs.initiate()
> rs.add('hostname:27018')
> rs.add('hostname:27019' /*, true for an arbiter only */)

Or we can declare a config directly in rs.initiate(cfg).

rs.conf() to get the current config. It's basically the result of db.system.replset.find()

rs.reconf() to reconfigure. You won't be able to update db.system.replset. rs.stepDown() to change to a secondary rs.freeze(10) to make a node ineligible to become primary during 10s. (if you're going to do some maintenance on it for instance)

rs0:PRIMARY> rs.status()                                                                  
{                                                                                         
        "set" : "rs0",                                                                    
        "date" : ISODate("2015-08-21T23:39:45.832Z"),                                     
        "myState" : 1,                                                                    
        "members" : [                                                                     
                {                                                                         
                        "_id" : 0,                                                        
                        "name" : "Rlyeh:27017",                                           
                        "health" : 1,                                                     
                        "state" : 1,                                                      
                        "stateStr" : "PRIMARY",                                           
                        "uptime" : 84,                                                    
                        "optime" : Timestamp(1440198709, 2),                              
                        "optimeDate" : ISODate("2015-08-21T23:11:49Z"),                   
                        "electionTime" : Timestamp(1440200306, 1),                        
                        "electionDate" : ISODate("2015-08-21T23:38:26Z"),                 
                        "configVersion" : 3,                                              
                        "self" : true                                                     
                },                                                                        
                {                                                                         
                        "_id" : 1,                                                        
                        "name" : "Rlyeh:27018",                                           
                        "health" : 1,                                                     
                        "state" : 2,                                                      
                        "stateStr" : "SECONDARY",                                         
                        "uptime" : 78,                                                    
                        "optime" : Timestamp(1440198709, 2),                              
                        "optimeDate" : ISODate("2015-08-21T23:11:49Z"),                   
                        "lastHeartbeat" : ISODate("2015-08-21T23:39:44.917Z"),            
                        "lastHeartbeatRecv" : ISODate("2015-08-21T23:39:45.768Z"),        
                        "pingMs" : 0,                                                     
                        "configVersion" : 3                                               
                },                                                                        
                {                                                                         
                        "_id" : 2,                                                        
                        "name" : "Rlyeh:27019",                                           
                        "health" : 1,                                                     
                        "state" : 7,                                                      
                        "stateStr" : "ARBITER",                                           
                        "uptime" : 79,                                                    
                        "lastHeartbeat" : ISODate("2015-08-21T23:39:44.418Z"),            
                        "lastHeartbeatRecv" : ISODate("2015-08-21T23:39:44.085Z"),        
                        "pingMs" : 0,                                                     
                        "configVersion" : 3                                               
                }                                                                         
        ],                                                                                
        "ok" : 1                                                                          
}

myState: 1 means Primary. self: true member you are talking with, you see the status from its point of view.

  • the other members will have a lastHearbeat