Mongo: Demo 1 Replica Set

by Mark Nielsen
Copyright October 2021


This is not intended to be a complete document on how to do TLS/SSL in mongo. Rather, links and a script is provided. We assume Mongo 5.0 on Ubuntu. An explanation of the steps of the script might be given. This is just a demo.
  1. Links
  2. Replica SEt

Links



Replica Set

This example sets up a simple replica set. We do some other things for fun like failovers (changing the PRIMARY), changing the weight for elections. and setting the replica back to the original PRIMARY.

function printPrimary { 
  count=`mongo --quiet --port 3001 mongo/functions.js| wc -l`
  while [ $count -lt 1 ]
  do
    echo "No primary yet: $count"
    sleep 2
    count=`mongo --quiet --port 3001 mongo/functions.js| wc -l`
  done
  port=`mongo --quiet --port 3001 mongo/functions.js | cut -d ':' -f2 `
  echo "port is on $port"
}

  # If restarting, kill the mogo processes and then do 
rm -rf  mongo/d1 mongo/d2 mongo/d3 mongo/logs
  # Setup 3 directories:
mkdir -p mongo/d1 mongo/d2 mongo/d3 mongo/logs
  # If not AWS,	  ignore.  
sudo  -s -- bash -c   '  echo "127.0.0.1 localhost ">> /etc/hosts '
  # Start once mongo instance
mongod --dbpath mongo/d1 --port 3001 --oplogSize 100 --wiredTigerCacheSizeGB 0.25 --replSet r --bind_ip 127.0.0.1 > mongo/logs/1.log 2>&1  & 
sleep 2
  # In  the mongo shell
mongo --port 3001 --eval "rs.initiate( { _id: 'r', version: 1, members: [ {_id :0, host: 'localhost:3001' } ] } )"

  # Start and add two other replica sets
mongod --dbpath mongo/d2 --port 3002 --oplogSize 100 --wiredTigerCacheSizeGB 0.25 --replSet r --bind_ip 127.0.0.1 > mongo/logs/2.log 2>&1  &
mongod --dbpath mongo/d3 --port 3003 --oplogSize 100 --wiredTigerCacheSizeGB 0.25 --replSet r --bind_ip 127.0.0.1 > mongo/logs/3.log 2>&1  &
sleep 2
mongo --port 3001 --eval "rs.add('localhost:3002')"
sleep 2
mongo --port 3001 --eval "rs.add('localhost:3003')"
sleep 2
  # Look at the status
mongo --port 3001 --eval "rs.status()"

echo "Let us sleep a really long time, 30 seconds"
sleep 30

/home/mark/temp# mongo --port 3001 --eval "rs.status()" | grep -i state

  # Lets setup the functions. 
echo "
cfg = rs.conf();
cfg.members[0].priority = 3;
cfg.members[1].priority = 2;
cfg.members[2].priority = 1;
rs.reconfig(cfg);
" >> mongo/reconfig.js

echo '
function pcheck() {
  s = rs.status()
  s.members.forEach( 
    function(myDoc) { 
      if (myDoc.stateStr=="PRIMARY") 
        { print( myDoc.stateStr + " " + myDoc.name ) }
      }
    );
}
pcheck()
' > mongo/functions.js

  # Lets print out the hosts in the replica set
mongo --port 3001 --eval "rs.status()" | grep name
  # Remove a host
mongo --port 3001 --eval "rs.remove('localhost:3003')"
sleep 1
mongo --port 3001 --eval "rs.status()" | grep name

  # Add host back in
mongo --port 3001 --eval "rs.add('localhost:3003')"
sleep 2
  # Add print out who is primary and secondary
mongo --port 3001 --eval "rs.status()" | egrep 'name|stateStr'

  # Redo the prorities, but in practice its no promise for stepdowns.
cat mongo/reconfig.js | mongo --port 3001 
mongo --port 3001 --eval "rs.conf()" | egrep 'host|priority'

mongo --port 3001 --eval "rs.stepDown({secondaryCatchUpPeriodSecs: 10 })"
sleep 5
printPrimary
mongo --port 3001 --eval "rs.status()" | egrep 'name|stateStr'

  # Not let's force it back to the first one --- this is bad in production. 
mongo --quiet  --port 3001 --eval "rs.remove('localhost:3003')"
sleep 5
printPrimary
mongo --quiet --port 3001 --eval "rs.remove('localhost:3002')"
sleep 5
printPrimary
mongo --quiet --port 3001 --eval "rs.status()" | egrep 'name|stateStr'

   # Now add them back in
mongo --quiet --port 3001 --eval "rs.add('localhost:3002')"
sleep 2
mongo --quiet --port 3001 --eval "rs.add('localhost:3003')"
sleep 2
  # Look at the status
mongo --quiet --port 3001 --eval "rs.status()" | egrep 'name|stateStr'


  echo "
  cfg = rs.conf();
  cfg.members[0].priority = 3;
  cfg.members[1].priority = 2;
  cfg.members[2].priority = 1;
  rs.reconfig(cfg);
  " > mongo/reconfig.js
  
cat mongo/reconfig.js | mongo --port 3001 --quiet
cat mongo/reconfig.js | mongo --port 3002 --quiet
cat mongo/reconfig.js | mongo --port 3003 --quiet