Skip to content

Instantly share code, notes, and snippets.

@davideicardi
Last active March 7, 2018 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davideicardi/238ce0c9f2c5b34ab19fc7174ba2c53d to your computer and use it in GitHub Desktop.
Save davideicardi/238ce0c9f2c5b34ab19fc7174ba2c53d to your computer and use it in GitHub Desktop.
Development mongodb cluster tutorial

Development mongodb cluster tutorial

Tutorial on how to create a development mongodb cluster. Based on: https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/

Start mongo1:

mongod --config d:\data\mongodb\mongod-cluster1.cfg

Start mongo2:

mongod --config d:\data\mongodb\mongod-cluster2.cfg

Initiate the cluster:

  • connect to mongo1, inside the shell type:
rsconf = {
  _id: "rs-test",
  members: [
    {
     _id: 0,
     host: "localhost:28001"
    },
    {
     _id: 1,
     host: "localhost:28002"
    }
   ]
}
rs.initiate(rsconf)

Connection string:

mongodb://localhost:28001,localhost:28002?replicaset=rs-test

Prevent a member to become a primary

https://docs.mongodb.com/manual/tutorial/configure-secondary-only-replica-set-member/

cfg = rs.conf()
cfg.members[1].priority = 0
rs.reconfig(cfg)

Now you can stop the primary to simulate a failover and a secondary only access...

systemLog:
destination: file
path: d:\data\mongodb\cluster1\log\mongod.log
storage:
dbPath: d:\data\mongodb\cluster1\db
mmapv1:
smallFiles: true
replication:
oplogSizeMB: 128
replSetName: rs-test
enableMajorityReadConcern: true
net:
bindIp: 127.0.0.1
port: 28001
systemLog:
destination: file
path: d:\data\mongodb\cluster2\log\mongod.log
storage:
dbPath: d:\data\mongodb\cluster2\db
mmapv1:
smallFiles: true
replication:
oplogSizeMB: 128
replSetName: rs-test
enableMajorityReadConcern: true
net:
bindIp: 127.0.0.1
port: 28002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment