MongoDB cluster building (master-slave replication, replica and) (5), mongodb master-slave

Source: Internet
Author: User
Tags failover mongodb add mongodb query mongodb sharding mongodb version

MongoDB cluster building (master-slave replication, replica and) (5), mongodb master-slave
Vi. Architecture Management

Mongodb master-slave clusters are divided into two types:
1: master-Slave replication (master-Slave) -- The Slave server will not automatically change to the master server. You need to set the Slave server.
2: replica Sets (replica set) -- if the master server fails, an slave server is elected as the master server.

I. Master-slave Replication
Mongodb supports failover and redundancy through asynchronous replication on multiple machines.
Only one in multiple machines is used for write operations. This situation guarantees data consistency for mongodb.
Machines that assume the primary role can distribute read operations to slave.

One master, one slave, one master, and multiple slaves

You only need to add the-master parameter when a server is started, and add the-slave and-source parameters to the other server to implement synchronization.
This method is not recommended for the latest mongodb version.
Start the simulation (start the simulation directly without user verification ):
1: open two terminals (install two mongodb databases)
* -- You can simulate it on the same computer, but just create the corresponding directory of dbpath logpath.
Create a file under mongodb/(you only need to open two terminal links and do not need to install multiple mongodb databases)
Bin (mongodb built-in file) data1 data2 dblog1 dblog2 -- 1 is the file used by the master service; 2 is the file used by the slave server.
2: Master (set a master server)
. /Mongod -- master -- dbpath =/usr/local/mongodb/data1/-- logpath =/usr/local/mongodb/dblog1 -- fork -- port 20001 (port enables a corresponding port)

3: From -- source to "source", link the server and Port
./Mongod -- slave -- source 20001 -- dbpath =/usr/local/mongodb/data2/-- logpath =/usr/local/mongodb/dblog2 -- fork -- port 20002

From:./mongo -- port 20002

4: MASTER:./mongo -- port 20001
Db. c1.insert ({name: "user1 "});
Db. c1.insert ({name: "user2 "});
5: The c1 set created by the master has arrived.
Db. c1.find ();
Db. c1.insert ({name: "user3"}); -- the slave server cannot be inserted. The "not master" error is returned"

To reduce the pressure on the master server, you can back up the backup or anything from the server (for example, if the data is large, a backup will increase the pressure on the master server by dozens of minutes ):
./Mongodump -- port 20002-d test backup test Database

** In this way, the master server can be operated during addition, deletion, and modification, and the link to the slave server during read.

Ii. Replica Sets replication (Replica set)
Mongodb 1.6 has developed the replica set, which mainly includes fault automatic failover and Automatic Repair of member nodes.
The most significant difference between data consistency is that the replica set does not have a fixed master node, which is a master node elected by the entire cluster.
It is strongly recommended to change other nodes. (After the master node fails, one of the master servers is automatically selected as the master server)

Deploy Replica Sets:
1): Create the corresponding file mongodb/(create the key, data, and log folders) -- the key is the group member who saves the same Meeting, used for election of the master server
Bin key (create key file: touch key1, touch key2) data (create folder data1, data2) log (create folder dblog1, dblog2)

2): import the two keys in the key Directory (for linux commands, You can manually create them in Windows)
Echo "123456"> key1
Echo "123456"> key2

Chmod 600 * -- remember: These two keys must be set to 600 (under the key file)

3): After preparation, start the game (enable 2 mongodb services)
Rs1: defines a group. The group number is rs1.
. /Mongod -- replSet rs1 -- keyFile =/usr/local/mongodb/key/key1 -- port 20001 -- dbpath =/usr/local/mongodb/data/data1/-- logpath =/local /mongodb/log/dblog1 -- fork

When Yes, start 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 is started.

4): two members will have a meeting automatically. Now, the configuration of the meeting room is created.
./Mongo -- port 20001 ------> (20002 is fine, which machine can be used)
Configure to create a replica set (group list ):
Config_rs1 = {
_ Id: "rs1 ",
Members :['
{_ Id: 0, host: "localhost: 20001", priority: 1}, -- indicates the priority. The higher the number, the higher the priority.
{_ Id: 1, host: "localhost: 20002", priority: 2 },
} -- Press enter. You can directly press enter to view config_rs1.

Initialize replica set configuration (group list ):
Rs. initiate (config_rs1); -- press enter and press Enter. You will find that the ">" symbol of mongodb is changed to "SECONDARY>" (slave) or "PRIMARY>" (master)

Why do they become symbols? (Mongodb's ">" symbol to "SECONDARY>" (slave) or "PRIMARY>" (master )):
When you initialize the replica set, they will elect localhost: 20002 as the master server after the meeting (the higher the priority number, the higher the priority ),
Therefore, localhost: 20002 is elected as the master server "PRIMARY>"

5): Data Testing
./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 on to the slave server (synchronize the added or deleted data from the slave server ):
./Mongo -- port 20001
Show tables; -- if an error is reported, you do not have the permission.
Rs. slaveOk (); -- In the replica set Master/Slave, you can click this command to confirm that your identity is from the server.
Show tables; --
Db. c1.find (); -- the master data can be read.
Db. c1.insert ({name: "user2"}); -- the master and slave nodes of the replica set are the same. data cannot be inserted from the replica set because it is not the master server.

More than two mongodb servers are required;
The replica set is better than the traditional master-salve in that it can perform automatic failover of faults.
If we stop a member in the replica set, the remaining Members will automatically elect a new member to act as the master database.

Who has an apsaradb for mongodb advanced video tutorial?

Are you learning this software?

Who has learned the MongoDB video tutorial?

I recommend a document called "in-depth development of MongoDB applications (basic, Development Guide, system management, cluster and system architecture)" with 22 lessons, it focuses on explaining the common features and advanced features of MongoDB, and comprehensively and deeply analyzes MongoDB from the perspective of actual development. For details, contact me at 1511065175.

MongoDB basics:

Lecture 1: nosql and MongoDB (background of the rise of nosql, Introduction to various nosql databases, and features of MongoDB)
Section 2: MongoDB installation and configuration (MongoDB installation and use, basic system management skills, and web Console usage)
Lecture 3: MongoDB shell (introduces the usage and commands of MongoDB shell, Backup recovery, data import and export)
Lecture 4: Concepts of MongoDB documents, collections, and databases (Introduction to documents, collections, databases, and other basic concepts, database file storage methods, and command Rules)
Lecture 5: Introduction to Mongodb data types (details on MongoDB supported data types)
MongoDB Development Guide:
Lecture 6: MongoDB add, delete, and modify documents (describes commands for adding, deleting, and modifying documents in MongoDB, insertion principle, batch modification, and modifier usage)
Lecture 7: MongoDB query syntax 1 (describes in detail the powerful query functions of MongoDB, and queries by operators such as $ in, $ or, $ ne, $ lt, and $ gt)
Lecture 8: MongoDB query syntax 2 (describes in detail the powerful query functions of MongoDB, such as regular expression query, array query, and embedded document query)
Lecture 9: MongoDB query syntax 3 (detailed description of MongoDB where queries, cursor operations, paging queries, code examples, and cursor details)
Lecture 10: MongoDB index (detailed description of MongoDB index principles, management, index query and analysis tools, and mandatory index usage)
11th Lecture: MongoDB aggregation statistics (describes the MongoDB aggregation statistics function)
Lecture 12th: MongoDB advanced guide-how commands work (describes how database commands work)
Lecture 13th: MongoDB advanced guide-fixed set and GridFS (introducing fixed set and GridFS principles and applications)
Lecture 14th: apsaradb for MongoDB advanced guide-server scripts (Introduction to server scripts dbeval and javascript storage)
MongoDB System Management:
Lecture 15th: Advanced MongoDB System Management Skills 1 (System Monitoring)
Lecture 16th: apsaradb for MongoDB advanced system management skills 2 (Database Security, backup and recovery, and data restoration)
MongoDB cluster and system architecture:
Lecture 17th: MongoDB replication function (describes in detail how to create, manage, and maintain MongoDB master-slave replication)
Lecture 18th: MongoDB replica set function (describes in detail how to create, manage, and maintain MongoDB replica sets)
19th Lecture: MongoDB sharding function (details on MongoDB sharding creation, management, and maintenance)
20th: MongoDB insider (in-depth analysis of MongoDB System Architecture and Data File structure principles)
MongoDB application case:
21st Lecture: Development Based on MongoDB General Account Management System 1
22nd Lecture: Development Based on MongoDB General Account Management System 2

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: 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.