MongoDB copy election principle and replica set management

Source: Internet
Author: User

How the MongoDB copy set is elected principle of MONGODB replication

MongoDB replication is based on the operation Log Oplog, which is equivalent to the binary log in MySQL, recording only records that have changed. Replication is the process of synchronizing and applying the Oplog log of the master node to other slave nodes.

The principle of MongoDB election

MongoDB nodes are divided into three types, namely standard node (host), passive node (passive), and quorum node (arbiter)

    • Only standard nodes can be elected as active nodes (master nodes) and have the right to vote. The passive node has a complete copy and cannot be an active node with the right to vote. The quorum node does not replicate data and cannot become an active node, only the right to vote. It is plain to say that only a standard node can be elected as the primary node, even if the standard nodes in a replication set are down, the passive node and the quorum node will not become the master node. A follow-up example demonstrates validation.
    • The difference between the standard node and the passive node: The priority value is the standard node, the lower is the passive node
    • The election rule is a high number of votes to win, priority is the value of 0~1000, equivalent to an additional 0~1000 votes. Election result: The number of votes is high, if the number of votes is the same, the new data winner.

The MongoDB replica set is shown in the Inter-node election

Link to the previous article
Yum installs MongoDB and database management
Configuring a MongoDB Replication set

Sample validation Copy set election principle Create 4 instances create a new instance of the path file and the DBPath file directory and set permissions for the log file
mkdir -p /data/mongodb/logs//  path目录,因为在安装完成之后就会有一个实例,所以这里我们只需要创建三个实例即可mkdir -p /data/mongodb/mongo{2,3,4}           //创建dbpath目录touch /data/mongodb/logs/mongod{2,3,4}.log    //创建日志文件chmod 777 /data/mongodb/logs/*.log            //为日志文件赋予权限
Modifying a configuration file (/etc/mongod.conf)
path:dbPath:  port:  bindIp:replication:  replSetName:      //修改配置文件中这五个项目的值

Shutting down firewalls and selinux firewalls
systemctl disable firewalld.servicesystemctl stop firewalld.servicesetenforce 0
Launch instance
mongod -f /etc/mongod.conf
Configuring a replica set creates a replication set of four nodes, set priority when created

This replica set consists of four nodes, including two standard nodes, one passive node and one quorum node.

mongo //=进入数据库chen={"_id":"chenrs","members":[{"_id":0,"host":"172.16.10.29:27017","priority":100},{"_id":1,"host":"172.16.10.29:27018","priority":100},{"_id":2,"host":"172.16.10.29:27019","priority":0},{"_id":3,"host":"172.16.10.29:27020","arbiterOnly":true}]}

Initializing a replication set
rs.initiate(chen)
View the identities of each node
rs.isMaster()

View OPLPOG Logs

At this time to make some additions and deletions to the database operation, note that in the Oplog log does not record the query statement, only the statement to make changes to the database

use stady                                      //使用stady库db.abc.insert({"id":1,"name":"zhangsan"})      //向表abc中写入一条数据db.abc.insert({"id":2,"name":"lisi"})          //再次插入一条数据db.abc.find()                                  //查看表中内容db.abc.update({"id":2},{$set:{"name":"jack"}}) //修改表数据db.abc.remove({"id":1}                         //删除表数据
View content in Oplog
use localshow collections    //可以看到其中有一个oplog.rs文件db.oplog.rs.find()  //由于内容过多,在这里就不展示,查看之后就会发现,没有记录查询的记录,只有记录对数据库做更改的操作
Simulating primary node failure shutting down the master node
Kill-9 + process number//close primary node mongod-f/etc/mongod4.conf--shutdown//Close Master node MONGO--port 27018chenrs:prim Ary> rs.status ()//At this point the node has become the primary node {"Set": "Chenrs", "date": Isodate ("2018-07-16t05:30:04.152z"), "MyState ": 1,                 Omit some of the content "members": ["_id": 0, "name": "172.16.10.29:27017", "Health": 0, Health value is 0, inactive "state": 8, "Statestr": "(not Reachable/healthy)", ...            Omit part of the content "_id": 1, "name": "172.16.10.29:27018", "Health": 1, "state": 1, "Statestr": "PRIMARY",//At this time 1 main node ...             Omit part of the content "_id": 2, "name": "172.16.10.29:27019", "Health": 1, "state": 2, "Statestr": "Secondary", ...            Omit part of the content "_id": 3, "name": "172.16.10.29:27020", "Health": 1, "state": 7,       "Statestr": "Arbiter", ··· Omit part of the content]
Simulate another primary node failure
  mongod-f/etc/mongod2.conf--shutdownmongo--port 27019chenrs:secondary> rs.status ()//still in SECONDARY state {            "Set": "Chenrs", "date": Isodate ("2018-07-16t05:40:09.740z"), "members": [{"_id": 0, ' Name ': ' 172.16.10.29:27017 ', ' health ': 0, ' state ': 8, ' statestr ': ' (not REAC Hable/healthy) ", ...            Omit part of the content "_id": 1, "name": "172.16.10.29:27018", "Health": 0, "state": 8, "Statestr": "(not Reachable/healthy)", ...            Omit part of the content "_id": 2, "name": "172.16.10.29:27019", "Health": 1, "state": 2, "Statestr": "secondary",//even if two standard nodes are in an outage state, the passive node and the quorum node do not become the primary node ...            Omit part of the content "_id": 3, "name": "172.16.10.29:27020", "Health": 1, "state": 7, "Statestr": "Arbiter",] Omit partial content  
MongoDB Replica Set Management configuration allows data to be read from nodes

The slave node of the default MongoDB replica set cannot read data, but can use Chitian to allow data to be read from the node

rs.slaveOk()    //允许从节点能够读取数据
Viewing replication Set status information
rs.help rs.printReplicationInfo()        //查看oplog日志文件的大小及时间范围rs.printSlaveReplicationInfo()   //查询节点及节点复制的时间

resizing oplog log files

If the node is part of a replica set, at this point you want to modify the size of the oplog is not allowed, so to move the node out of the replica set you want to modify the default size of the log file. The first thing to do is to shut down the node's service and then exit the replication set

Shut down the node service (which belongs to an offline upgrade)
use admin            //在复制集的从节点上做db.shutdownServer()  //关闭服务,此时再想登陆该节点则会失败
Node exits replication set

Log off the value of replication and modify the port value to start it as a single instance

vim /etc/mongod2.conf#replication:#  replSetName: chenrsport: 27028mongod -f /etc/mongod2.conf   //启动实例,此时该实例不属于复制集
Full backup Oplog Log
mongodump --port 27028 --db local --collection ‘oplog.rs‘   //在linux界面操作
Deleting a oplog file in a node
> use local> db.oplog.rs.drop()
Rebuilding the Oplog Log
db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )
Shut down the node service again
To join a node to a replica set

Callback parameters, plus one parameter to specify the log file size

vim /etc/mongod2.confreplication:  replSetName: chenrs  oplogSizeMB: 2048    //单位为兆mongod -f /etc/mongod2.conf      //启动实例mongo -port 27018                //进入实例

View Oplog Size
rs.printReplicationInfo()

Changes to the log file size are only valid for that node, and the other nodes are still the default values

Deploy authentication replication Create an administrative user
use admindb.createUser({"user":"root","pwd":"123123","roles":["root"]})

Configuring key validation

To enable other nodes to synchronize with the master node, create a key file to enable other nodes to synchronize

    • Create a validation file
      # cd /usr/bin/# echo "chenrs key"> chenrskey1# echo "chenrs key"> chenrskey2# echo "chenrs key"> chenrskey3# echo "chenrs key"> chenrskey4    //密钥内容自定义,但是要保证内容的一致性# chmod 600 chenrskey{1..4}        //设置文件权限,不设置在接下来的启动中会报错
    • Modify the configuration file, turn on the security authentication function of MongoDB (four configuration files should be changed, note the difference of content)
      vim /etc/mongod.confsecurity:keyFile: /usr/bin/chenrskey1     //每个节点的验证文件不同,要根据不同的节点修改clusterAuthMode: keyFile        //认证类型,密钥文件认证

    • Restart Service
      mongod -f /etc/mongod.conf --shutdownmongod -f /etc/mongod.conf              /其他几台的重启方式都相同,重复操作即可
      Authentication Login (Verify master First, then verify from)

      When you log into the system directly using the login command, you are not able to view the data using show DBS, and you need to use the authentication

      mongo --port 27018use admindb.auth("root","123123")

MongoDB copy election principle and replica set 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.