MongoDB Replica Set Construction

Usually, clustering is required to prevent single point of failure applications. However, in addition to preventing single point of failure in the database, we also need to do database backup, read-write separation, fault transfer and so on. MongoDB's Replica Set meets these requirements.

Replication integrator

Members of Replica Set are a collection of instances with the same data content as mongod, including the following three types of roles:

  1. Primary
    It is the only node in Replica Set that receives a write request and records the write instructions on the oplog. The replica node synchronizes the data of the primary node by replicating the write instructions of oplog. Secondary. A Replica Set has and only has Primary nodes. When Primar hangs, other Secondary or Arbiter nodes will re-elect a primary node. The application's default read request is also sent to the Primary node for processing.

  2. Secondary node
    By replicating the instructions in the master node oplog and keeping the same data set as the master node, the master node elects when the master node hangs up.

  3. Arbiter
    It does not store practical application data and only votes to select the primary node, but it will not be elected as the primary node.

Replication Set Building (3 members)

  1. Description of Node Directory Structure


    Node directory structure


    Among them, instance, instance_1 and instance_2 represent three nodes respectively. Each node has the same directory structure, including data directory, log directory, JavaScript file directory js, start-stop script mongod-server.sh, node configuration file mongod.conf, etc.

  2. Node configuration file mongod.conf

    # syslog configuration
    logpath=/mnt/mongodb/instance/logs/mongod.log #Log directory
    logappend=true
    timeStampFormat=iso8601-utc
    # Storage storage configuration
    dbpath=/mnt/mongodb/instance/data/db #Data Storage Directory
    directoryperdb=true
    # Process Management Process Management Configuration
    fork=true
    pidfilepath=/mnt/mongodb/instance/var/run/mongod.pid # Process PID File Save Directory
    # net network configuration
    bind_ip=127.0.0.1
    port=27017 
    # security configuration
    #auth=true
    # Replication set
    replSet=test-set    # Duplicate set name

    Where logpath, dbpath, pidfilepath need to be modified to the corresponding directory according to the node (instance in the path is changed to instance_1 or instance_2). In addition, the port of each node needs to be modified. In my configuration, the ports of instance, instance_1 and instance_2 are 27017, 27018 and 27019 respectively.

  3. Start three nodes
    The mongod-server.sh service startup and shutdown script mentioned above is needed for startup.( This script was written by me to manage the start and stop of the mongod process.)
    Execute commands in instance, instance_1, and instance_2 directories. / mongod-server.sh start

    [root@iZu1qhttxe5Z instance]# ls
    data  dump  js  logs  mongod.conf  mongod-server.sh  var
    [root@iZu1qhttxe5Z instance]# ./mongod-server.sh start
    about to fork child process, waiting until server is ready for connections.
    forked process: 680
    child process started successfully, parent exiting
    Started [680]
    [root@iZu1qhttxe5Z instance]#

    At this time, three nodes have been started, and then the replication set needs to be initialized.

  4. Initialize replication sets
    Select any node and enter the js directory:

    [root@iZu1qhttxe5Z js]$ ls
    initiate_rs.js  user.js
    [root@iZu1qhttxe5Z js]# cat initiate_rs.js 
    rs.initiate()
    rs.add("iZu1qhttxe5Z:27017")
    rs.add("iZu1qhttxe5Z:27019")
    rs.conf()
    [root@iZu1qhttxe5Z js]#

    The initiate_rs.js file here applies to the JS file that initializes the replication set. In fact, these operations can be operated in the mongo shell, but in order to facilitate operation and maintenance management, this operation is extracted into the JS file.( How to put the mongo shell command into the js file and punch it here).

    To perform initialization, select the node of port 27018.

    [root@iZu1qhttxe5Z instance_1]# mongo --port 27018 ./js/initiate_rs.js 
    MongoDB shell version v3.4.5
    connecting to: mongodb://127.0.0.1:27018/
    MongoDB server version: 3.4.5
    [root@iZu1qhttxe5Z instance_1]#

    Connect to one of the nodes to see if initialization is successful

    test-set:SECONDARY> rs.status()
    {
     "set" : "test-set",
     "date" : ISODate("2017-06-28T02:57:33.424Z"),
     ......
     },
     "members" : [
         {
             "_id" : 0,
             "name" : "10.117.225.127:27018",
             ...........
             "stateStr" : "SECONDARY",
             ...........
         },
         {
             "_id" : 1,
             "name" : "iZu1qhttxe5Z:27017",
             .............
             "stateStr" : "PRIMARY",
             ..............
         },
         {
             "_id" : 2,
             "name" : "iZu1qhttxe5Z:27019",
             .........
             "stateStr" : "SECONDARY",
             .........
         }
     ],
     "ok" : 1
    }
    test-set:SECONDARY>

    You can see that the replication set has been initialized successfully.

  5. Connect the Primary node and write data.

    test-set:PRIMARY> use test
    switched to db test
    test-set:PRIMARY> show collections
    test-set:PRIMARY> db.test.insert({"name":"knight"});
    WriteResult({ "nInserted" : 1 })
    test-set:PRIMARY> show collections
    test
    test-set:PRIMARY> db.test.find()
    { "_id" : ObjectId("59531cd6000e29eae22a2f83"), "name" : "knight" }
    test-set:PRIMARY>

    Successful insertion of data to verify whether SECONDARY synchronizes data

    # 27018 Nodes
    [root@iZu1qhttxe5Z js]# mongo --port 27018
    MongoDB shell version v3.4.5
    connecting to: mongodb://127.0.0.1:27018/
    MongoDB server version: 3.4.5
    ....
    test-set:SECONDARY>
    test-set:SECONDARY> rs.slaveOk()
    test-set:SECONDARY> use test
    switched to db test
    test-set:SECONDARY> show collections
    test
    test-set:SECONDARY> db.test.find()
    { "_id" : ObjectId("59531cd6000e29eae22a2f83"), "name" : "knight" }
    test-set:SECONDARY> 
    [root@iZu1qhttxe5Z js]#
    [root@iZu1qhttxe5Z js]#
    [root@iZu1qhttxe5Z js]#
    # 27019 Nodes
    [root@iZu1qhttxe5Z js]# mongo --port 27019
    MongoDB shell version v3.4.5
    connecting to: mongodb://127.0.0.1:27019/
    MongoDB server version: 3.4.5
    ....
    test-set:SECONDARY> rs.slaveOk()
    test-set:SECONDARY> use test
    switched to db test
    test-set:SECONDARY> db.test.find()
    { "_id" : ObjectId("59531cd6000e29eae22a2f83"), "name" : "knight" }
    test-set:SECONDARY>
  6. By default, Secondary cannot read and write, and needs to execute rs. slaveOk () (or db.getMongo().setSlaveOk()).
    test-set:SECONDARY> use test
    switched to db test
    test-set:SECONDARY> db.test.find()
    Error: error: {
     "ok" : 0,
     "errmsg" : "not master and slaveOk=false",
     "code" : 13435,
     "codeName" : "NotMasterNoSlaveOk"
    }
    test-set:SECONDARY>

References:

MongoDB executes js files
MongoDB Start-Stop script

Keywords: MongoDB shell Database Javascript

Added by redrage on Mon, 17 Jun 2019 22:05:16 +0300