MongoDB replication set deployment and basic management

Source: Internet
Author: User
Tags instance method mongodb

MongoDB replica set deployment and basic management MongoDB replication Set Overview

A replica set (Replica sets) is an additional copy of the data that is the process of synchronizing data across multiple servers, providing redundancy and increasing the availability of data, which can be used to recover hardware failures and outage services through a replica set.

The replication set consists of the following advantages:

    • Make your data more secure
    • High data Availability (24x7)
    • Disaster recovery
    • Maintenance without downtime (e.g. backup, index rebuild, failover)
    • Read scaling (extra copy read)
    • Replica set how transparent the replication set works for Applications

      A copy set of MongoDB requires at least two nodes. One of the nodes is the primary node (Primary), which handles the client's request, and the rest is the slave node (serondary), which is responsible for replicating the data on the master node.

MongoDB each node common collocation method is: a master one from or a master many from. The master node records all hits to Oplog, and the nodes periodically polls the master node for these operations, and then performs these operations on their own copies of the data. This ensures that the data from the node is consistent with the primary node. As shown in the following:

The client writes data to the primary node, writes the data to the primary node, and the master node interacts with the data from the node to ensure consistency of data. If one of the nodes fails, the other nodes will immediately take over the business without any downtime.

MongoDB replica set Deployment configuration multiple instances

In the previous blog has explained the MongoDB open multi-instance method, here is not much to repeat, we use the same method to create four MongoDB instances. Before starting four instances, modify each instance's configuration file, configure the Replset parameter value to be the same value, this value as the name of the replica set, the following actions:

[[email protected] ~]# vim /usr/local/mongodb/bin/mongodb1.conf port=27017dbpath=/data/mongodb1logpath=/data/logs/mongodb/mongodb1.loglogappend=truefork=truemaxConns=5000replSet=kgcrs                     #配置复制集的名称

In the other three instances of the configuration file in the last line, plus the same sentence code on the line. Open four instance processes.

[[email protected] ~]# export PATH=$PATH:/usr/local/mongodb/bin/#由于之前重启过,重新设置环境变量[[email protected] ~]# mongod -f /usr/local/mongodb/bin/mongodb1.conf#开启端口号为27017的实例进程2018-07-17T10:33:29.835+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none‘about to fork child process, waiting until server is ready for connections.forked process: 3751child process started successfully, parent exiting
Initialize the configuration and start the replication set

After starting 4 MongoDB instances, here's how to configure and start a MongoDB replication set. This first configures a replica set with 3 nodes (which is then added to the last instance), and primary represents the primary node. Secondary represents the Slave node

[[email protected] bin]# mongod -f /usr/local/mongodb/bin/mongodb2.conf --smallfilesabout to fork child process, waiting until server is ready for connections.forked process: 20207child process started successfully, parent exiting[[email protected] bin]# mongod -f /usr/local/mongodb/bin/mongodb3.conf --smallfilesabout to fork child process, waiting until server is ready for connections.forked process: 20230child process started successfully, parent exiting[[email protected] bin]# mongod -f /usr/local/mongodb/bin/mongodb4.conf --smallfilesabout to fork child process, waiting until server is ready for connections.forked process: 20253child process started successfully, parent exiting

You can see that four instances have been successfully started, and the corresponding port numbers are already open. The port number is then entered into an instance of port number 27017.

[[email protected] bin]# mongo   #默认就是端口号为27017的实例> cfg={"_id":"kgcrs","members":[{"_id":0,"host":"192.168.100.201:27017"},{"_id":1,"host":"192.168.100.201:27018"},{"_id":2,"host":"192.168.100.201:27019"}]}#这句代码的意思就是向kgcrs的复制集中添加三个成员> rs.initiate(cfg){ "ok" : 1 }#对复制集进行初始化启动复制集,启动复制集后,可以通过rs.status()查看复制集的完整信息。




Adding and removing nodes

After you configure the start replica set, you can also easily add and remove nodes by using the Rs.add () and Rs.remove () commands.

kgcrs:PRIMARY> rs.add("192.168.100.201:27020"){ "ok" : 1 }#可以看到27020端口的实例添加成功kgcrs:PRIMARY> rs.remove("192.168.100.201:27019"){ "ok" : 1 }#可以看到27019端口的实例被删除了
MongoDB Replica set Switchover

1, analog fault automatic switching

The KILL command lets you stop the current node of the replica set, and then view the primary node automatically switching to the other node, and you can see that the current instance of Port 27017 is the primary node.

[[email protected] bin]# netstat -ntap| grep mongotcp        0      0 0.0.0.0:27019           0.0.0.0:*               LISTEN      20230/mongod        tcp        0      0 0.0.0.0:27020           0.0.0.0:*               LISTEN      20253/mongod        tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      20055/mongod        tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      20207/mongod [[email protected] bin]# kill -9 20055 #kill掉27017端口的进程kgcrs:SECONDARY> rs.status()#可以看到现在的主节点是端口为27019的实例


2. Manually switch between master and slave

First of all to enter into the master node of the instance, only the master node has permission to the master-slave node switch.

[[email protected] bin]# mongo --port 27019kgcrs:PRIMARY> rs.freeze(30)   #暂停30s不参加选举{ "ok" : 1 }kgcrs:PRIMARY> rs.stepDown(60,30)  #告诉主节点交出主节点位置,然后维持从节点状态不少于60s,同时等待30s以使主节点和从节点日志同步,再次查看状态,发现主节点已经切换到另外一个实例中。2018-07-18T15:52:53.254+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27019 (127.0.0.1) failed2018-07-18T15:52:53.256+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27019 (127.0.0.1) okkgcrs:SECONDARY> rs.status()

The principle of copying the electoral principle of copy set

Replication is based on the operation Log Oplog, which is equivalent to the binary log in MySQL and records only the changes that have occurred. Replication is the process of synchronizing the Oplog logs of host points and applying them to other slave nodes

The principle of election

The node types are divided into standard (host) nodes, passive (passive) nodes, and quorum (abriter) nodes.

(1) Only standard nodes may be elected as active (primary) nodes by the right to vote. The passive node has a complete copy, cannot become an active node, and has the right to vote. The quorum node does not replicate data and cannot become an active node, only the right to vote.

(2) The difference between the standard node and the passive node: The higher priority value is the standard node, the lower one becomes the passive node.

(3) The election rule is that the number of votes is high, priority is the value of 0 to 1000, which is equivalent to an additional 0 to 1000 votes. Election result: The number of votes is high, if the number of votes is the same, the new data winner.

To configure the priority of a replication set

Reconfigure the MongoDB replica set of 4 nodes, set two standard nodes, yo passive node and one quorum node. This setting is to be configured on the master node.

kgcrs:PRIMARY> > cfg={"_id":"kgcrs","members":[{"_id":0,"host":"192.168.58.131:27017","priority":100},{"_id":1,"host":"192.168.58.131:27018","priority":100},{"_id":2,"host":"192.168.58.131:27019","priority":0},{"_id":3,"host":"192.168.58.131:27020","arbiterOnly":true}]}#这句代码分别设置了四个实例的属性,优先级kgcrs:PRIMARY> rs.reconfig(cfg){ "ok":1 }

You can see an instance of Port 27018 now, which is now the master node.

Simulating primary node failure

If the primary node fails, another standard node will be elected as the new master node.

[[email protected] ~]# mongod -f /usr/local/mongodb/bin/mongodb2.conf --shutdown2018-07-22T09:20:02.706+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none‘killing process with pid: 9639kgcrs:PRIMARY> rs.isMaster()


You can see that the instance of Port 27017 has become the primary node.

Simulate all standard node failures

If all standard nodes fail, neither the passive node nor the quorum node can be the primary node.

[[email protected] ~]# mongod -f /usr/local/mongodb/bin/mongodb1.conf --shutdown2018-07-22T09:24:08.290+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none‘killing process with pid: 9740kgcrs:SECONDARY> rs.isMaster()

MongoDB replication set deployment and basic management

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.