MongoDB Cluster Setup (master-slave copy, copy, and) (v)

Source: Internet
Author: User
Tags create directory failover

VI: Architecture Management


MongoDB's master-slave cluster is divided into two types:
1:master-slave Replication (master-Slave)--from the server does not actively become the primary server, need to set the line
2:replica sets replication (replica set)-assuming that the primary server is hung up, an election from the server to the primary server

One: Master-slave replication
MongoDB supports failover and implementation redundancy across multiple machines through asynchronous replication.
Only one of the unified moments in a multi-machine is for write operations. It is for this reason that MongoDB provides the assurance of data consistency.
Machines that play the role of primary can distribute read operations to slave.



One Master one from one master more from the way

It is only necessary to add-master to one server startup, and the other server plus-slave and-source, you can synchronize.


This method is deprecated for MongoDB's latest version number.

Start simulation (direct play, no real user authentication):
1: Open 2 terminals (2 MongoDB databases installed)
*--you can simulate on the same computer again. But put the DBPath LogPath, the corresponding folder can be built
Create a file under mongodb/(only need to open 2 terminal link, do not install multiple MongoDB database)
Bin (MongoDB comes with the file) data1 data2 dblog1 dblog2--1 is the main service with the file 2 is the file from the server
2: Master (set up a primary server)
./mongod--master--dbpath=/usr/local/mongodb/data1/--logpath=/usr/local/mongodb/dblog1--fork--port 20001 (Port Open A corresponding port)

3: "Source" from--source, link that server and port
./mongod--slave--source 127.0.0.100:20001--dbpath=/usr/local/mongodb/data2/--logpath=/usr/local/mongodb/dblog2- -fork--port 20002

From:./mongo--port 20002

4: main:./mongo--port 20001
Db.c1.insert ({name: "user1"});
Db.c1.insert ({name: "User2"});
5: From: Will find the master created C1 collection has come
Db.c1.find ();
Db.c1.insert ({name: "User3"}); --In the server is not plugged in, will error "not master"

In order to reduce the pressure backup of the primary server and so on from the server (for example, the data is large, a backup is a few 10 minutes significantly increase the pressure of the primary server):
./mongodump--port 20002-d test backup test Database

* * This allows you to manipulate the master server when adding or deleting. Read the link from the server



II: Replica sets copy (replica set)
MongoDB developed the replica set in the 1.6 version number, mainly to add the fault itself to actively switch and self-repair member nodes. Between each DB
The data is completely consistent. The most striking difference is that. The replica set does not have a fixed master node, it is a master node that is elected by the entire cluster, when
It is strongly recommended to change other nodes that do not work.

(When the master hangs up, he will take the initiative to select one as the primary server)

Deploy replica sets:
1): Create the corresponding file mongodb/(establish key, data, log directory)--key is a member of the same meeting, for the election of the primary server
Bin key (Build key file: Touch Key1, Touch key2) data (under Create directory data1, data2) log (under Create directory Dblog1, DBLOG2)

2): Import 2 keys under the key folder (this is the Linux command.) Win under the words can be set up manually)
echo "123456" >key1
echo "123456" >key2

CHMOD 600 *--remember: These 2 keys must be set to (key file operation)

3): Ready to complete. Start Play (open 2 MongoDB service)
RS1: The definition of a group, group Howl Rs1, meeting
./mongod--replset rs1--keyfile=/usr/local/mongodb/key/key1--port 20001--dbpath=/usr/local/mongodb/data/data1/-- Logpath=/local/mongodb/log/dblog1--fork

Can after starting one:
./mongod--replset rs1--keyfile=/usr/local/mongodb/key/key2--port 20002--dbpath=/usr/local/mongodb/data/data2/-- LOGPATH=/LOCAL/MONGODB/LOG/DBLOG2--fork


* The replica set has been started.

4): 2 members will meet on their own initiative, now set up the next meeting room configuration
./mongo--port 20001------> (20002 is OK, which machine can)

To configure a replica set (group list):
config_rs1={
_id: "Rs1",
Members:['
{_id:0,host: "localhost:20001", priority:1},--Indicates priority, the higher the number priority, the greater the election of the host server
{_id:1,host: "localhost:20002", Priority:2},
‘]
}--Enter, config_rs1 can directly enter to view

Initialize replica set configuration (group checklist):
Rs.initiate (CONFIG_RS1); --Enter in the carriage return, you will find the ">" Symbol of MongoDB become "secondary>" (from) or "primary>" (Master)

Why do they change symbols? (MongoDB's ">" Symbol becomes "secondary>" (from) or "primary>" (Master)):
Think that when you initialize the replica set, they will be in the meeting after the localhost:20002 election as the main server (the priority number of the higher precedence),
So localhost:20002 was elected as the main server "Primary>"

5): Data test
./mongo--port 20002 (Master)
Db.c1.insert ({name: "user1"});
Db.c1.insert ({name: "User2"});
Db.c1.find ();
Rs.status (); --View the status of the replica set

Log in from server (sync from server to delete data):
./mongo--port 20001
Show tables; --assuming that you have no authority, you can then knock down the following commands.
Rs.slaveok (); --The replica set master-slave inside, from to want to have the master's Read permission to knock down the command, to confirm that your identity is from the server
Show tables; --It works.
Db.c1.find (); -The master's data can be read
Db.c1.insert ({name: "User2"}); --The master and slave of the replica set can no longer insert data from it, because it is not the primary server


Failover:
More than 2 units of Mongodbserver Oh.
The replica set is better than the traditional master-salve, where it can fail its own active transfer
Suppose we stop a member in the replication set. Then the rest of the members will voluntarily elect a new member to be the primary database




MongoDB Cluster Setup (master-slave copy, copy, and) (v)

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.