MongoDB Simple Replication Configuration

Source: Internet
Author: User
Tags mongo shell

MongoDB's replication configuration is simpler than MySQL and feels smarter.

The configuration is very simple, let's briefly introduce the environment:

Primary One

Secondary One

Arbiter One

Three machines, respectively, are linked together by a 10.10.1.0 subnet.

On each machine's mongo.conf configuration file, add a configuration as follows:

Replset=rs0
Can not be named Rs0, can also name other, anyway, every machine repset the same OK.


Log in to MongoDB with the MONGO shell, and then create a CFG bson format variable, as follows:

cfg={"_id": "Rs0", "members": [{"_id": 0, "host": "10.10.1.55:27017"}, {"_id": 1, "host": "10.10.1.56:27017" } ]}

10.10.1.55 is primary machine, 1.56 is secondary, there is no need to add arbiter first.

Continue to enter the following name (as long as you enter on primary)

Rs.initiate (CFG) rs.status ()

After the above two commands you will find a machine is primary, one is secondary.


Now enter the following command to create the arbiter:

Rs.addarb ("10.10.1.57")

Re-enter Rs.statsu (), you should have the following:

Rs0:primary> Rs.status ()
{
"Set": "Rs0",
"Date": Isodate ("2015-10-12t17:01:31z"),
"MyState": 1,
"Members": [
{
"_id": 0,
"Name": "10.10.1.55:27017",
"Health": 1,
"State": 1,
"Statestr": "PRIMARY",
"Uptime": 66,
"Optime": Timestamp (1444636144, 1),
"Optimedate": Isodate ("2015-10-12t07:49:04z"),
"Electiontime": Timestamp (1444669275, 1),
"Electiondate": Isodate ("2015-10-12t17:01:15z"),
"Self": true
},
{
"_id": 1,
"Name": "10.10.1.56:27017",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 21,
"Optime": Timestamp (1444636144, 1),
"Optimedate": Isodate ("2015-10-12t07:49:04z"),
"Lastheartbeat": Isodate ("2015-10-12t17:01:30z"),
"Lastheartbeatrecv": Isodate ("2015-10-12t17:01:30z"),
"Pingms": 105,
"Lastheartbeatmessage": "Syncing to:10.10.1.55:27017",
"Syncingto": "10.10.1.55:27017"
},
{
"_id": 2,
"Name": "10.10.1.57:27017",
"Health": 1,
"State": 7,
"Statestr": "Arbiter",
"Uptime": 2,
"Lastheartbeat": Isodate ("2015-10-12t17:01:29z"),
"Lastheartbeatrecv": Isodate ("2015-10-12t17:01:30z"),
"Pingms": 1002
}
],
"OK": 1
}

In this way MongoDB replication is configured to complete, and then see if replication can be achieved.

On the primary, enter:

Rs0:primary> use students;
Switched to DB students
Rs0:primary> Db.scores.insert ({"Stuid": 1, "subject": "Math", "Score": 99})
Writeresult ({"ninserted": 1})

rs0:primary> Use Local
Switched to DB Local

Rs0:primary> Db.oplog.rs.find ()
{"TS": Timestamp (1444636024, 1), "H": Numberlong (0), "V": 2, "OP": "N", "ns": "", "O": {"MSG": "Initiating Set"} }
{"TS": Timestamp (1444636144, 1), "H": Numberlong (" -5201159559565017071"), "V": 2, "OP": "N", "ns": "", "O": {"MSG" : "Reconfig Set", "Version": 2}}
{"TS": Timestamp (1444669489, 1), "H": Numberlong (" -3625785754623372300"), "V": 2, "OP": "I", "ns": "Students.scores" , "O": {"_id": ObjectId ("561be83187dabed86388bff7"), "Stuid": 1, "subject": "Math", "Score": 99}}

The last one indicates that the data you just inserted is secondary by oplog, and you can log in secondary to see if you have the database and the data:

Rs.slaveok ();

Use students

Db.scores.find ()

This will show just that data.


Now simulate the case where the primary machine hangs up and enter it on the system shell:

Ps-ef|grep MONGO

Kill-9 Mongodpid

Kill the MongoDB process


Enter Rs.status () on the original secondary and you will find secondary become primary. Restarting the hung primary will be the new secondary. You can enter: Rs.setpdown () on the new primary server to re-elect the original primary.

If secondary hangs up, will it be able to complete the replication automatically after rebooting?

Now kill the secondary process with kill, and then insert several records on the primary.

Rs0:primary> for (var i=2;i<1000;i++) Db.scores.insert ({"Stuid": I, "subject": "Math", "Score": 99})

Rs0:primary> Db.scores.find (). Count ()
999

A total of 999 lines. Now reboot the secondary database and enter:

Rs.slaveok ()

Rs0:secondary> Db.scores.find (). Count ()
999

Just like primary.

This article is from the "Technical Blog" blog, please be sure to keep this source http://raytech.blog.51cto.com/7602157/1702227

MongoDB Simple Replication Configuration

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.