mongodb--master-slave copy & replica set

Source: Internet
Author: User

As I said earlier, the MongoDB database supports data replication between servers. Support for replication between master-slave mode and server. The goal of course is to achieve load balancing and failure recovery. There are two techniques built into the MONGO: master-slave copy, replica set. The configuration of these two modes is briefly described below.


First, master-slave replication

Master-slave replication is a simple database synchronous backup of the cluster technology. The model is as follows:


  


Need to note:

In the database cluster to know who is the primary server, the primary server only one.

From the server to know its own data source, that is, its own home server is who.

--master is used to clarify the primary server,--slave, and-source to control from the server.

Example

The configuration is very simple, similar to the Redis master-slave replication We talked about earlier, the idea is as follows:

We now start with two MongoDB db instances, one as master one as slave, then the data from master and then see if we can get the data out of slave. The steps are as follows:

   Step One:

Do the server and client boot bat files for master (with 8888 ports) and slave (with 7777 ports) respectively:

  

  


Master's file contents are as follows:

8888.conf:


  


8888mongodbstartserver.bat:


  


8888mongodb.bat:


  


the contents of the slave file are as follows:

7777.conf:


  


7777mongodbstartserver.bat:


  


7777mongodb.bat:


  


Step Two:

Double-click Master's Startup Bat file (8888mongodbstartserver.bat):


  


Double-click the slave startup Bat file (7777mongodbstartserver.bat):


  


Step Three:

Double-click the master client startup bat file (8888mongodb.bat) and create a new database with the data inserted:


  


Step Four:

Double-click the slave client startup bat file (7777mongodb.bat) and query for the existence of the newly created database, collection and two data inserted in master:


  


This means that master-slave replication is configured successfully. It is also important to note that slave database instances do not allow us to insert, modify data, we can only operate the master database server, for example, we insert a piece of data in slave, will be error:


  


the structure diagram for the instance is as follows:    


second, replica set

Similar to the effect that master-slave replication can achieve. Master in the master-slave replication model is equivalent to the "active" node in the replica set; slave is equivalent to a "backup" node, such as:


  


1, indicating that A is active, B, C is used for backup; Figure 2 shows that when a fails, the cluster according to the weight algorithm to elect B is an active database node; Figure 3 shows that when a recovery fails, it automatically becomes a backup node.

Unlike master-slave replication, there is only one master in master-slave replication, and when the master node is hung, all slave nodes cannot automatically become master and continue to serve. At this point all slave will not be able to continue backing up data until it is connected to master again because it cannot connect to master.

While all nodes in the replica set can be "active" nodes (initially randomly launched), when the current active node is hung up, the cluster will immediately select a node from the other intact backup nodes as the "active node", and the entire replica cluster will not stop the backup service.

Configuration

Configuration is similar to the configuration of master-slave replication, here is no longer as detailed step by step introduction, directly give the approximate configuration:

Prepare three sets of MongoDB boot configurations: (listening on different ports)

Three boot configurations (a/b/c.conf) are as follows:


  


Start the three server instances of MONGO and initialize the replica set:


  


Then, start the client instance of MONGO, and you can see which active node and which are the backup nodes after startup:


  

  

You can then do a "master-slave Copy" instance to verify that we are configured successfully.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

mongodb--master-slave copy & replica set

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.