MongoDB tip: MongoDB 3.4, creating replica set using config file
In my previous post, MongoDB tip: MongoDB 3.4, adding replica set members, issue/resolution, we created the replica set using rs.add(), in this post, I am going to use the config file option to achieve the same result.
First, let's spin up 3 mongod instances. This can be done in single servers or multiple servers, doesn't really matter, as long as for multiple servers, they can resolve the IP/hostname and communicate with one another.
I edited the /etc/hosts to make sure the hostname can be resolved
To validate, same ping from one of the servers to the other two by, assuming from demo3
On all servers, start the mongod process as
On one of the node, go into mongo shell and configure the configure file as such, then rs.initiate()
Validate the replica set status by rs.status()
First, let's spin up 3 mongod instances. This can be done in single servers or multiple servers, doesn't really matter, as long as for multiple servers, they can resolve the IP/hostname and communicate with one another.
I edited the /etc/hosts to make sure the hostname can be resolved
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain10.155.208.169 demo2.net demo210.155.228.75 demo3.net demo310.155.208.207 demo1.net demo1
ping demo1 ping demo2
On all servers, start the mongod process as
# assign ownership of /data/demo to user mongod chown -R mongod. /data/demo # this starts mongod process as user mongod sudo -u mongod mongod --port 27001 --replSet confDemo --dbpath /data/demo/data --logpath /data/demo/log/demo.log --logappend --oplogSize 50 --smallfiles --fork
On one of the node, go into mongo shell and configure the configure file as such, then rs.initiate()
> cfg = {_id : "confDemo",members : [{ _id:0, host:"demo1:27001"},{ _id:1, host:"demo2:27001"},{ _id:2, host:"demo3:27001"}]}> rs.initiate(cfg){ "ok" : 1 }
Validate the replica set status by rs.status()
MongoDB Enterprise confDemo:PRIMARY> rs.status(){"set" : "confDemo","date" : ISODate("2017-08-04T18:25:18.660Z"),"myState" : 1,"term" : NumberLong(1),"heartbeatIntervalMillis" : NumberLong(2000),"optimes" : {"lastCommittedOpTime" : {"ts" : Timestamp(1501871113, 1),"t" : NumberLong(1)},"appliedOpTime" : {"ts" : Timestamp(1501871113, 1),"t" : NumberLong(1)},"durableOpTime" : {"ts" : Timestamp(1501871113, 1),"t" : NumberLong(1)}},"members" : [{"_id" : 0,"name" : "msdlva-dsnavl04:27001","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 127,"optime" : {"ts" : Timestamp(1501871113, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1501871113, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2017-08-04T18:25:13Z"),"optimeDurableDate" : ISODate("2017-08-04T18:25:13Z"),"lastHeartbeat" : ISODate("2017-08-04T18:25:18.453Z"),"lastHeartbeatRecv" : ISODate("2017-08-04T18:25:17.085Z"),"pingMs" : NumberLong(0),"syncingTo" : "msdlvd-dsnavl02:27001","configVersion" : 1},{"_id" : 1,"name" : "msdlva-dsnavl04:27001","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 127,"optime" : {"ts" : Timestamp(1501871113, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1501871113, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2017-08-04T18:25:13Z"),"optimeDurableDate" : ISODate("2017-08-04T18:25:13Z"),"lastHeartbeat" : ISODate("2017-08-04T18:25:18.453Z"),"lastHeartbeatRecv" : ISODate("2017-08-04T18:25:17.085Z"),"pingMs" : NumberLong(0),"syncingTo" : "msdlvd-dsnavl02:27001","configVersion" : 1},{"_id" : 2,"name" : "msdlvd-dsnavl02:27001","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 885,"optime" : {"ts" : Timestamp(1501871113, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2017-08-04T18:25:13Z"),"infoMessage" : "could not find member to sync from","electionTime" : Timestamp(1501871002, 1),"electionDate" : ISODate("2017-08-04T18:23:22Z"),"configVersion" : 1,"self" : true}],"ok" : 1}
Comments
Post a Comment