MongoDB Replication Set Management optimization

Source: Internet
Author: User
Tags mongodb ticket

This article describes the basic configuration and management of a MongoDB replica set, including configuration from which nodes can read data, view replica set status, change Oplog size, configure a replicated set with authentication

    • The election 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 and applying the Oplog log of the master node to other slave nodes.

    • The principle of election

The types of nodes are divided into standard (host) nodes, passive (passive) nodes, and quorum (arbiter) nodes.

    • Standard nodes may be elected as active (primary) nodes, with 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.
    • The difference between a standard node and a passive node is that the priority value is higher than the standard node, and the lower is the passive node.
    • Election rules are high-ticket winners, priority is the value of 0~1000, equivalent to an additional 0~1000 votes. Election result: The high-ticket winner. If the number of votes is the same: the new data wins.

    • To configure the priority of a replication set
    • Create a 4-instance tutorial first
    • Set up 2 standard nodes, one passive node, and one quorum node.

      > cfg={"_id":"kgcrs","members":[{"_id":0,"host":"192.168.86.128:27017","priority":100},{"_id":1,"host":"192.168.86.128:27018","priority":100},{"_id":2,"host":"192.168.86.128:27019","priority":0},{"_id":3,"host":"192.168.86.128:27020","arbiterOnly":true}]}

    > rs.initiate(cfg)    //初始化配置    > rs.isMaster()    //查看复制集的状态

    • Simulating primary node failure

      # mongod -f /etc/mongod.conf --shutdown   //主节点服务关掉# mongo --port 27018> rs.isMaster()   //查看节点的身份状态  主节点已经换到27018

    • simulate all standard node failures

      # mongod -f /etc/mongod.conf --shutdown   # mongod -f /etc/mongod2.conf --shutdown    # mongo --port 27019> rs.isMaster()   //查看节点身份状态 可以看到主节点没有了(当所有标准节点故障,被动节点也不能成为主节点)

    • Allow data to be read from a node
    • The default MongoDB replica set is basic configuration and management, and you can use the Rs.slaveok () command to allow data to be read from the node.

      # mongo --port 27018> rs.slaveOk()        //允许默认从节点读取数据> show dbs

    • Viewing replication status information

    • To view the Oplog metadata information for master:

      > rs.printReplicationInfo()
    • To view the synchronization status of Slave:

      > rs.printSlaveReplicationInfo()
    • To view the master and slave configuration information:

      > rs.conf() //或db.system.replset.find()

    • Change the Oplog size
    • Oplog Introduction:

Oplog:operations log shorthand, stored in a special database (local), Oplog is stored in the Oplog. In $main collection, the collection is a fixed set, and the new operation automatically replaces the old one. To ensure that the oplog does not exceed the preset size, where each document represents an action performed on the primary node, Oplog contains all the actions that have been modified for the data (the query operation is not logged), and by default, the Oplog size consumes the free disk space of 64-bit instance 5%.
MONGO the process of copying: The master node applies the business operation modifications to the database, then records these operations into Oplog, copies the Oplog from the node, and then applies the modifications. PS: These operations are asynchronous. If the operation from the node has been very far from the main node, Oplog log in from the node has not finished, Oplog may have rolled round, from the node to keep up with the synchronization, replication will stop, from the node need to do a full synchronization, in order to avoid this situation, try to ensure that the main node oplog large enough, The ability to store operational records for quite a long time.

    > use local    >  db.oplog.rs.find()             //查看.oplog    > db.oplog.rs.stats()             //查看.oplog内容    > rs.printReplicationInfo()    //查询oplog的大小及保存的操作记录持续的时长

    • This is only a single-instance 27018 modification, other instances are referenced below
    • Close Service

       # mongo --port 27018 > use admin > db.shutdownServer()      //关闭服务
  • Logoff replication: Related startup parameters, and modify port port number 27028

  • Backs up all Oplog records for the current node

    # mongodump --port 27028 --db local --collection ‘oplog.rs‘# mongo --port 27028> use local> db.oplog.rs.drop()    //删除原来的oplog> db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )    //创建新的> use admin> db.shutdownServer()


  • Turn on replication: Related startup parameters, and modify port port number 27018

    Mongod-f/etc/mongod2.confmongo//Enter the master node

    Rs.stepdown ()//Yield Master node location

  • Deploying certified Replication
  • Creating the root user on the master node

    kgcrs:PRIMARY> use adminkgcrs:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]})
  • Generate a key file for 4 instances

     # cd /usr/bin/ # echo "kgcrs key"> kgcrskey1 # echo "kgcrs key"> kgcrskey2 # echo "kgcrs key"> kgcrskey3 # echo "kgcrs key"> kgcrskey4 # chmod 600 kgcrskey{1..4} # vim /etc/mongod.conf  (mongod2.conf /mongod3.conf/mongod4.conf 都要改)security:   keyFile: /usr/bin/kgcrskey1     //(分别为 kgcrskey2、kgcrskey3、kgcrskey4)   clusterAuthMode: keyFile
  • 4 instances Restart in sequence

    # mongod -f /etc/mongod.conf --shutdown# mongod -f /etc/mongod.conf
  • Go to master node verification

    > show dbs   #无法查看数据库> rs.status()   #无法查看复制集

    > use admin    #身份登录验证> db.auth("root","123")> rs.status()  #可以查看数据库> show dbs    #可以查看复制集

MongoDB Replication Set Management optimization

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.