MongoDB replica-set settings

Source: Internet
Author: User

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.

Replset = RS1 // It must be set to a name.

Add

fork=true

Restart MongoDB

service mongodb restart

Run the following command:

SSH to vm3, log on to MongoDB, execute Rs. Initiate to create a replicaset, and check the configuration with config.

> rs.initiate(){"info2" : "no configuration explicitly specified -- making one","me" : "vm3:27017","info" : "Config now saved locally.  Should 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:

use localdb.dropDatabase()exitservice mongodb restart

Then execute the Rs. Add command on MongoDB of vm3:

root@vm3:~# mongoMongoDB shell version: 2.4.3connecting to: testrs1: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!

root@vm3:~# mongoMongoDB shell version: 2.4.3connecting to: testrs1:PRIMARY> rs.add("vm4"){ "ok" : 1 }rs1:PRIMARY> rs.add("vm1"){ "ok" : 1 }

Succeeded. Not easy! Let's look at the configuration again:

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:

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.