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

Source: Internet
Author: User
Tags failover mongodb server

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 copy (replica set)-If the primary server is hung, a slave server will be elected as 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

You only need to add the-master parameter when one server is started, and the other server with the-slave and-source parameters to achieve synchronization.
This method is deprecated in the latest version of MongoDB.

Start simulation (direct play, no user authentication):
1: Open 2 terminals (2 MongoDB databases installed)
*--you can simulate on the same computer again, but put DBPath LogPath, the corresponding directory is built on the line
mongodb/Create a file (only need to open 2 terminal links on the line, do not install multiple MongoDB database)
Bin (MongoDB comes with files) data1 data2 dblog1 dblog2--1 is the main service with files 2 is files 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 can be done again from the server (such as large data, a backup is a few 10 minutes significantly increase the pressure of the primary server):
./mongodump--port 20002-d test backup test Database

* * This can be changed when the main server, read the link from the server



II: Replica sets copy (replica set)
MongoDB developed the replica set in version 1.6, mainly to increase the failure auto-switch and automatically repair member nodes, between each db
Data is exactly the same, the most significant difference is that the replica set does not have a fixed master node, it is the entire cluster elected to get a master node, when
Its not working is highly recommended for changing other nodes. (When the master hangs up, it automatically chooses one as the primary server)

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

2): Import 2 keys under the key directory (Linux command this, 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 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 automatically meet, now set up the next meeting room configuration
./mongo--port 20001------> (20002, which machine is OK)

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 primary 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 primary server (the higher the priority number, the higher the 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 the server (sync from the server is added and deleted data):
./mongo--port 20001
Show tables; -If the error indicates that you do not have the authority, after you can knock down the following command
Rs.slaveok (); --The replica set master-slave inside, from 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:
To more than 2 MongoDB server oh;
The replica set is better than the traditional master-salve, where it can fail the automatic transfer
If we stop a member of the replication set, the remaining members will automatically 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.