MongoDBReplica-set this article uses three MongoDBserver to set up replica-set. First, prepare three ubuntuvms with MongoDBinstance installed. Then, each vm's etchosts contains the ip address and Host Name of the other two VMS. This ensures that each vm can be connected to the other two VMS through the host name.
Set MongoDB Replica-set this article uses three MongoDB servers to set up replica-set. First, prepare three UBuntu VMS with MongoDB instance installed. Then, the/etc/hosts of each vm contains the ip address and Host Name of the other two VMS. This ensures that each vm can be connected to the other two VMS through the host name.
MongoDB Replica-set settings
This article uses three MongoDB servers to set up replica-set.
First, prepare three UBuntu VMS with MongoDB instance installed.
Then, the/etc/hosts of each vm contains the ip address and Host Name of the other two VMS.
This ensures that each vm can connect to the other two VMS through the host name.
My VMS are: vm1, vm3, and vm4. the IP addresses are 192.168.1.51, 192.168.1.52, and 192.168.1.53 respectively.
Log on to all VMS, edit the/etc/mongodb. conf file, uncomment, and set replSet.
[Plain]
ReplSet = rs1 // It must be set to a name.
Add
[Plain]
Fork = true
Restart mongodb
[Plain]
Service mongodb restart
Run the following command:
Log on to vm3, log on to mongodb, execute rs. initiate to create a replicaset, and check the configuration with config.
[Plain]
> Rs. initiate ()
{
"Info2": "no configuration explicitly specified -- making one ",
"Me": "vm3: 27017 ",
"Info": "Config now saved locally. shocould come online in about a minute .",
"OK": 1
}
> Rs. config ()
{
"_ Id": "rs1 ",
"Version": 1,
"Members ":[
{
"_ Id": 0,
"Host": "vm3: 27017"
}
]
}
Note: Only one of the three VMS can be selected to run the rs. initiate command. If you are not careful about running the command in other VMS. You need to clear it using the following method:
[Plain]
Use local
Db. dropDatabase ()
Exit
Service mongodb restart
Then execute the rs. add command on mongodb of vm3:
[Plain]
Root @ vm3 :~ # Mongo
MongoDB shell version: 2.4.3
Connecting to: test
Rs1: PRIMARY> rs. add ("vm1 ")
{
"Errmsg": "exception: set name does not match the set name host vm1: 27017 expects ",
"Code": 13145,
"OK": 0
}
Rs1: PRIMARY> rs. add ("vm4 ")
{
"Errmsg": "exception: set name does not match the set name host vm4: 27017 expects ",
"Code": 13145,
"OK": 0
}
The reason for the error is strange. After checking for half a day, is it because/etc/hosts has not been restarted just now?
Restart all three VMS and try again!
[Plain]
Root @ vm3 :~ # Mongo
MongoDB shell version: 2.4.3
Connecting to: test
Rs1: PRIMARY> rs. add ("vm4 ")
{"OK": 1}
Rs1: PRIMARY> rs. add ("vm1 ")
{"OK": 1}
Succeeded. Not easy! Let's look at the configuration again:
[Plain]
Rs1: PRIMARY> rs. config ()
{
"_ Id": "rs1 ",
"Version": 3,
"Members ":[
{
"_ Id": 0,
"Host": "vm3: 27017"
},
{
"_ Id": 1,
"Host": "vm4: 27017"
},
{
"_ Id": 2,
"Host": "vm1: 27017"
}
]
}
To see the status:
[Plain]
S1: PRIMARY> rs. status ()
{
"Set": "rs1 ",
"Date": ISODate ("2013-05-04T12: 36: 28Z "),
"MyState": 1,
"Members ":[
{
"_ Id": 0,
"Name": "vm3: 27017 ",
"Health": 1,
"State": 1,
"StateStr": "PRIMARY ",
"Uptime": 380,
"Optime ":{
"T": 1367670744,
"I": 1
},
"OptimeDate": ISODate ("2013-05-04T12: 32: 24Z "),
"Self": true
},
{
"_ Id": 1,
"Name": "vm4: 27017 ",
"Health": 1,
"State": 2,
"StateStr": "SECONDARY ",
"Uptime": 248,
"Optime ":{
"T": 1367670744,
"I": 1
},
"OptimeDate": ISODate ("2013-05-04T12: 32: 24Z "),
"LastHeartbeat": ISODate ("2013-05-04T12: 36: 28Z "),
"LastHeartbeatRecv": ISODate ("1970-01-01T00: 00: 00Z "),
"PingMs": 0,
"SyncingTo": "vm3: 27017"
},
{
"_ Id": 2,
"Name": "vm1: 27017 ",
"Health": 1,
"State": 2,
"StateStr": "SECONDARY ",
"Uptime": 244,
"Optime ":{
"T": 1367670744,
"I": 1
},
"OptimeDate": ISODate ("2013-05-04T12: 32: 24Z "),
"LastHeartbeat": ISODate ("2013-05-04T12: 36: 26Z "),
"LastHeartbeatRecv": ISODate ("2013-05-04T12: 36: 28Z "),
"PingMs": 1,
"SyncingTo": "vm3: 27017"
}
],
"OK": 1
}
Vm3 has been elected as primary.