[Mongo] mongodb replica set

Time:2022-1-7

………………………………………………………………………………….
Environment: centos7
Ip:
Master node: 192.168.225.128
Slave node 192.168.225.129
Slave node & arbitration node: 192.168.225.130
Mongo version: 3.4
………………………………………………………………………………….

1、 Principle

Mongodb replica set is a master-slave cluster with automatic fault recovery function, which is composed of one primary node and one or more secondary nodes. Mmm architecture similar to MySQL.
 
Data synchronization process in replica set: the primary node writes data. The secondary node obtains replication information by reading the primary oplog, starts data replication, and writes replication information to its own oplog. If an operation fails, the backup node stops copying data from the current data source. If a backup node fails for some reason, it will automatically start synchronization from the last operation of the oplog after restart. After synchronization, the information will be written to its own oplog. Since the replication operation is to copy the data first and then write to the oplog, it is possible that two copies of the same operation will be synchronized, but mongodb has considered this problem at the beginning of design, Executing the same oplog operation multiple times has the same effect as executing it once.

Simply put, after the primary node completes data operations, the secondary node will take a series of actions to ensure data synchronization:

  1. Check the oplog. Of your local library The RS set finds the most recent timestamp.
  2. Check the primary node local library oplog RS set to find records greater than this timestamp.
  3. Insert the found records into your own oplog RS set and perform these operations.

Like master-slave synchronization, replica set synchronization is an asynchronous synchronization process. The difference is that replica set has an automatic failover function. The principle is as follows: the slave side obtains the log from the primary side, and then performs various operations recorded in the log in full order (the log does not record query operations). This log is oplog in the local database RS table, which is relatively large on 64 bit machines by default, accounting for 5% of the disk size, oplog The size of RS can be set in the startup parameters: — oplogsize 1000, in M.
      be careful:In the replica set environment, if all the secondary are down, only the primary is left. Finally, the primary node will become a secondary node and cannot provide services.

2、 Environment construction

1. Install mongodb (128 / 129 / 130)

Port 37017

2. Start 3 mongos

$ mongod -f /etc/mongo37017.conf --replSet replicaset 
$ mongod -f /etc/mongo37017.conf --replSet replicaset
$ mongod -f /etc/mongo37017.conf --replSet replicaset
explain:
$ mongod --replSet replicaset --dbpath /mongo/data/ --port 37017 --oplogSize 1024
--Replset sets the replica set name, where application is the name of the replica set, and the nodes must be the same,
--Dbpath specifies the database storage path,
--Port specifies the listening port,
--Oplogsize specifies the size of cache space before data synchronization, and temporarily 1g.
Path, port. I've set it up in the profile

3. Enter Mongo shell (128 / 129 / 130)

# mongo --host 192.168.225.128 --port 37017
# mongo --host 192.168.225.129 --port 37017
# mongo --host 192.168.225.130 --port 37017

4. Configure replica set and add members (128)

> conf=
{
    "_id" : "replicaset",
    "members" : [
        { "_id" : 0,  "host" : "192.168.225.128:37017"  },
        { "_id" : 1,  "host" : "192.168.225.129:37017"  },
        { "_id" : 2,  "host" : "192.168.225.130:37017"  }
    ]
}
Initialize replica set
> rs.initiate(conf)
{ "ok" : 1 }
replicaset:OTHER>
explain:
Set 1 master 1 slave 1 arbitration
> conf=
{
    "_id" : "replicaset",
    "members" : [
        { "_id" : 0,  "host" : "192.168.225.128:37017"  },
        { "_id" : 1,  "host" : "192.168.225.129:37017"  },
        { "_id" : 2,  "host" : "192.168.225.130:37017",  "arbiterOnly": true  }
    ]
}

After all members are configured, replica set will automatically elect a primary node and two secondary nodes. Update on the primary node to synchronize to the secondary node.
You can also execute rs.ismaster() to confirm the master
After execution, you will see that the prompt of the shell has changed to replicaset: primary >

5. View cluster status

replicaset:PRIMARY> rs.status();

6. By now, the entire replica set has been built

7. View the configuration table of the current replica set

 replicaset:PRIMARY> rs.conf()

8. Write operation test

128:
replicaset:PRIMARY> use szy
replicaset:PRIMARY> db.createCollection("mycoll")
replicaset:PRIMARY> db.mycoll.insert({name:'lcl',datetime:'2017-10-23'})
replicaset:PRIMARY> db.mycoll.find()
129:
replicaset:SECONDARY> db.mycoll.find()
Error: error: {"ok" : 0,"errmsg" : "not master and slaveOk=false","code" : 13435,"codeName" : "NotMasterNoSlaveOk" }
replicaset:SECONDARY> db.getMongo().setSlaveOk();
{ "_id" : ObjectId("59edb815a7d3f969241d0fa9"), "name" : "lcl", "datetime" : "2017-10-23" }

9. Failover

1) Close Mongo at 128,
2) Rs.status() on 129 checks the cluster status and finds that the statestr of 128 changes to (not reachable / Health); 129 becomes primary; 130 becomes secondary
3) Restart Mongo:

mongod -f /etc/mongo37017.conf –replSet replicaset

Will automatically become secondary