MongoDB Learning Note--master/slave Master-slave copy

Source: Internet
Author: User

Master/slave master-slave replication

master-slave replication MongoDB a common way, if you want to achieve a master-slave replication should have at least two MongoDB instance, one is responsible for the client request as the primary node, and the other is responsible for mapping data from the primary node, providing data backup, client reading, etc., and recommending a master multi-slave mode as a slave node

MongoDB Master-slave replication implementation:

    • The operation of the master node is recorded as stored in system database local oplog. $main
    • slave node Periodically get oplog
    • oplog Using a fixed set, as the operation gradually increases, the new document will gradually overwrite the old document

MongoDB use note points or drawbacks:

    • Each slave node must know its corresponding primary node address
    • Allow only the primary node for data update operations
    • Primary node outage service not available
    • 3.2 The version has since been removed

Mongodb the master-slave replication configuration is detailed:

  • Master : The default is false , when set to true , the current instance is configured as the primary node.
  • slave : The default is false , when set to true , the current instance is configured as a slave node.
  • Source : The default is null, which is used from the node to specify the source of replication from the node (the master node's ip+ port) in the following format:
  • only : Default is NULL, which is used to replicate all databases on the default replication master node from the node, by setting this key to specify the name of the database to be replicated
  • Slavedelay : Sets the delay time for synchronizing the main library from the library, which is used from the setting, which defaults to 0 , per second.
  • Autoresync: The default isfalse, which is used from the settings. Whether to automatically resynchronize. Set totrueif the backward master exceedsTenseconds, the automatic resynchronization is enforced. Ifoplogsizeis too small, there may be a problem with this setting. IfOPLOGThe size is not enough to store the difference between the change state of the master and the change in state, in which case forcing resynchronization is unnecessary. When you setAutoresyncoption is set tofalse,Tenminutes will never be greater than1Automatic resynchronization of the secondary.

Master-Slave Configuration example (one-from-one)

Master Node master.conf

Dbpath=d:\mongodb\zhucong\master\data

Logpath=d:\mongodb\zhucong\master\logs\mongodb.log

port=27017

bind_ip=127.0.0.1

Master=true

From node configuration

Dbpath=d:\mongodb\zhucong\slave\data

Logpath=d:\mongodb\zhucong\slave\logs\mongodb.log

port=27018

bind_ip=127.0.0.1

Master=true

Slave=true

source=127.0.0.1:27017

start with two individual MongoDB Example

Mongod--config D:\mongodb\zhucong\master\master.conf

Mongod--config D:\mongodb\zhucong\slave\slave.conf

Use Robomongo the client connects to each of the two MongoDB Example

Create a database on the main library masterslavetest and create some test data

Use Masterslavetest

for (i = 100000; i <; i++) {

Db.users.insert ({

"I": I,

"UserName": "User" + I,

"Age": Math.floor (Math.random () * 120),

"Created": New Date (),

Total:Math.floor (Math.random () * +) * I

})

}

Execute the following command on two instances to see consistent data on two databases, when performing CRUD Operations on the primary database From library data still consistent with the main library

Db.users.find ({})

Two instructions

db.printreplicationinfo ():// View information for the master node

db.printslavereplicationinfo ():// View information from a node

MongoDB Learning Note--master/slave Master-slave copy

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.