Deploy a MongoDB replication set (master-slave replication, read/write separation, high availability)

Source: Internet
Author: User
Tags chmod mkdir mongodb file permissions


MongoDB Replica Set

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

How MongoDB Replica sets work
  1. A copy set of MongoDB requires at least two nodes. One is the master node (Primary), which handles the client request and the rest is the slave node (secondary), which replicates the data on the master node.
  2. MongoDB each node common collocation method is: A master one from, a master many from. The master node records all operations on it into Oplog, obtains these operations from the node periodically polling the master node, and then performs these operations on its own copy of the data, ensuring that the data from the node is consistent with the primary node.
  3. The client writes data to the primary node, reads the data from the node, and the master node interacts with the data from the node to ensure data consistency. If one of the nodes fails, the other nodes will immediately connect to the business without downtime.
Features and benefits of MongoDB replication set
    • Features of the replica set:

      • n Nodes of a cluster
      • Any node can be used as the master node
      • All write operations are on the primary node
      • Auto Fail-Over
      • Automatic recovery
    • Benefits of replication set:
      • 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)
      • The replica set is transparent to the application
MongoDB Replica set deployment


We can create multiple instances on a single server for replication sets
MongoDB installation and creation of multi-instance details in the Post CentOS 7 installation MongoDB (latest version 4.0)


Configuring Replication Sets
  1. Create 4 instances of MongoDB
    # Create an instance data directory
    mkdir -p / data / mongodb / mongodb {1,2,3,4}
    
    # Create an instance's log directory
    mkdir -p / data / logs
    
    # Create an instance log file
    touch /data/logs/mongodb{1,2,3,4}.log
    
      # Grant log file permissions
      chmod 777 /data/logs/mongodb*.log
    
      #Create instance configuration file storage location
      mkdir -p / data / conf
    


  2. Create a configuration file, turn on the copy set feature
    Here Bo Master stole a little lazy, the configuration file in the previously created/data/conf/directory, created after the startup script is the relative path of the experimental machine
    # vim /data/conf/mongodb1.conf // simple configuration is as follows,
         dbpath = / data / mongodb / mongodb1
         logpath = / data / logs / mongodb1.log
         port = 27017
         replSet = testrc #Configuration parameter value is testrc
         logappend = true
         fork = true
         maxConns = 5000
         storageEngine = mmapv1
    

    Required note Three other instances of the data file in the configuration file location and log files and ports to change the

  3. scripting makes it easy to control 4 instances (using relative paths, depending on the actual modification)
    [[email protected] conf]# cd /usr/local/mongodb/bin
    [[email protected] bin]# vim mongodb
     
    #!/bin/bash
    INSTANCE=$1
    ACTION=$2
    case "$ACTION" in
    ‘start‘)
    /usr/local/mongodb/bin/mongod -f /data/conf/"$INSTANCE".conf;;
    ‘stop‘)
    /usr/local/mongodb/bin/mongod -f /data/conf/"$INSTANCE".conf --shutdown;;
    ‘restart‘)
    /usr/local/mongodb/bin/mongod -f /data/conf/"$INSTANCE".conf --shutdown
    /usr/local/mongodb/bin/mongod -f /data/conf/"$INSTANCE".conf;;
    esac
     
    [[email protected] bin]# chmod +x mongodb
    [[email protected] bin]# ./mongodb mongodb1 restart
    [[email protected] bin]# ./mongodb mongodb2 restart
    [[email protected] bin]# ./mongodb mongodb3 restart
    [[email protected] bin]# ./mongodb mongodb4 restart
    [[email protected] bin]# netstat -ntap | grep mongod


  4. Configuring a replication set of three nodes

    4.1 Viewing the status information for a replica set
    mongo # (default port: 27017)
    rs.status () 



    4.2 Define the CFG initialization parameters (add three here, the rest of the Experiment Add node function)

    > cfg={"_id":"testrc","members":[{"_id":0,"host":"192.168.125.119:27017"},{"_id":1,"host":"192.168.125.119:27018"},{"_id":2,"host":"192.168.125.119:27019"}]}



    4.3 Start the replication set function, ensure that the slave node has no data when initializing the configuration, keep the master node data synchronized

    > rs.initiate(cfg)


  5. Adding nodes
    testrc:PRIMARY>  rs.add("192.168.125.119:27020")


  6. Delete a node
    testrc:PRIMARY>  rs.remove("192.168.125.119:27020")


Deploy a MongoDB replication set (master-slave replication, read/write separation, high availability)


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.