Window configuration MongoDB cluster (replica set)

Source: Internet
Author: User
Tags mongoclient mongodb mongodb server

Parameter explanation:

DBPath: Data Storage Directory

LogPath: Log Storage path

Pidfilepath: Process files to help close services

Logappend: Logging in Append (boolean value)

Replset: The name of the replica set, with the same name for each replica set

Port number of the Port:mongodb

Maximum size of the Oplogsize:mongodb operation log file, in megabytes (MB), default to 5% of the remaining space on the hard disk

Noprealloc: No pre-allocated storage

Fork: Run the process later (Linux use)

Directoryperdb: Set up a folder for each database according to the database name

Health indicates whether the node is normal in the replica set, 0 is unhealthy, and 1 is normal

State represents the identity of the node, 0 represents a non-primary node, and 1 represents the primary node

Statestr is used to describe the identity of a node, primary represents the primary node, and secondary represents a secondary node

Name is the IP and port information for the replica set node

Priority: Replica set node precedence, the range of this value is 0--100, the greater the value, the higher the priority, the default value is 1, assuming the value is 0, then cannot become primay

Arbiteronly: Setting the quorum node

First, let's build a replica set (the replica set structure is 1 master nodes, one from node to one quorum node)

The first step: We start three different MongoDB instances on the 1001, 1002 and 10,033 ports on this machine;

Mongod--port 1001--dbpath f:/mongos/mongodb1/data--logpath f:/mongos/mongodb1/log/mongodb.log--pidfilepath F:/mong Os/mongodb1/mongodb1.pid--replset Test--logappend--directoryperdb
Mongod--port 1002--dbpath f:/mongos/mongodb2/data--logpath f:/mongos/mongodb2/log/mongodb.log--pidfilepath F:/ Mongos/mongodb2/mongodb2.pid--replset Test--logappend--directoryperdb
Mongod--port 1003--dbpath f:/mongos/mongodb3/data--logpath f:/mongos/mongodb3/log/mongodb.log--pidfilepath F:/ Mongos/mongodb3/mongodb3.pid--replset Test--logappend--directoryperdb

Step two: Log on to the 1001 instance to write instructions to combine three different MongoDB instances together to form a complete replica set;

CD F:\mongos\mongodb1\bin

MONGO 127.0. 0.1:1001

Use admin

CONFIG_TEST={_ID: "Test", members:[
{_id:0,host: "127.0.0.1:1001", Priority:1},
{_id:1,host: "127.0.0.1:1002", Priority:1},
{_id:2,host: "127.0.0.1:1003", Arbiteronly:True},
]};
Here, members can contain multiple values, which are listed here are the three MongoDB instances that were just started, and the copy set was named Test by the _id field.

Step three: Initialize the replica set by executing the following command.

Rs.initiate (config_test);

This uses the above configuration to initialize the MongoDB replica set.

Rs.status ()

To view the status of a replica set

Here we build a copy set of three MongoDB instances named Test.

The replica sets are now set up, so can this replica set solve two of the problems we have in the master and slave modes above?

Let's start with the first question, we'll turn off the 1001-Port MongoDB Server, and then we'll use the Rs.status () command to look under as follows:

From the return package information, you can see that when the 1001 port is closed, the node is unreachable in the state of the replica set node, and the primary node being re-selected is the MongoDB instance that is started on port 1002, the election process is such that when the primary node is hung, the other nodes can initiate an election behavior, As long as a node gets more than half the number of copies of the replica set node in the election process and no node votes against it, the node can become the primary node. (see starting position for parameter comments) after the MongoDB instance on port 1001 is hung, 1002 can be switched automatically for the new master node.

As for the second problem, that is, the master node is responsible for all the read and write operations causing the main node pressure is large, then how to solve this problem in the replica set? Under normal circumstances, we access the replica set in Java as follows:

 Public classTestmongodbreplset { Public Static voidMain (string[] args) {Try{List<ServerAddress> addresses =NewArraylist<serveraddress>(); ServerAddress Address1=NewServerAddress ("127.0.0.1", 1001); ServerAddress Address2=NewServerAddress ("127.0.0.1", 1002); ServerAddress ADDRESS3=NewServerAddress ("127.0.0.1", 1003);                Addresses.add (ADDRESS1);               Addresses.add (ADDRESS2);               Addresses.add (ADDRESS3); Mongoclient Client=Newmongoclient (addresses); DB DB= Client.getdb ("TestDB"); Dbcollection Coll= Db.getcollection ("TestDB"); //InsertBasicdbobject Object =NewBasicdbobject (); Object.append ("UserID", "001");               Coll.insert (object); Dbcursor dbcursor=Coll.find ();  while(Dbcursor.hasnext ()) {DBObject DBObject=Dbcursor.next (); System.               Out.println (Dbobject.tostring ()); }           } Catch(Exception e) {e.printstacktrace (); }       }   } 

However, it is not possible to read and write pressure dispersion in the replica set, in fact, at the code level, we can set the second access to the replica set when the data only from the secondary node. The replica set reads and writes the detach structure as shown:

In order to implement read-write separation on the replica set, we need to implement the following two steps:

(1) Set the Setslaveok on the replica node;

(2) At the code level, set the read data from the replica node during the read operation as follows:

public class Testmongodbreplset {public static void main (string[] args) {try {List<ser                veraddress> addresses = new arraylist<serveraddress> ();               ServerAddress Address1 = new ServerAddress ("127.0.0.1", 1001);               ServerAddress address2 = new ServerAddress ("127.0.0.1", 1002);               ServerAddress ADDRESS3 = new ServerAddress ("127.0.0.1", 1003);                Addresses.add (ADDRESS1);               Addresses.add (ADDRESS2);               Addresses.add (ADDRESS3);               Mongoclient client = new Mongoclient (addresses);               DB db = Client.getdb ("test");                            Dbcollection coll = db.getcollection ("test");                Basicdbobject object = new Basicdbobject ();               Object.append ("userid", "001");                Readpreference preference = Readpreference.secondary ();                DBObject DBObject = Coll.findone (object, null, preference); System. Out. PrintlN (dbobject);            } catch (Exception e) {e.printstacktrace (); }       }   }

There are several other parameters that can be used in addition to secondary reading parameters, and their meanings are as follows:

Primary: The default parameter, only read from the primary node;
Primarypreferred: Most of the data is read from the primary node, and the data is read from the secondary node only when the primary node is unavailable.
Secondary: Only read from the secondary node, the problem is that the data of the secondary node is "old" than the primary node data.
Secondarypreferred: read from the secondary node, and read data from the master node when the secondary node is unavailable;
Nearest: Reads data from the node with the lowest network latency, whether it is the primary node or the secondary node.

Window configuration MongoDB cluster (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.