当前位置:网站首页>Mongodb replication (replica set) summary

Mongodb replication (replica set) summary

2022-07-07 13:12:00 cui_ yonghua

The basic chapter ( Can solve the problem of 80% The problem of ):

  1. MongoDB Overview 、 Application scenarios 、 Download mode 、 Connection mode and development history, etc

  2. MongoDB data type 、 Key concepts and shell Commonly used instructions

  3. MongoDB Various additions to documents 、 to update 、 Delete operation summary

  4. MongoDB Summary of various query operations

  5. MongoDB Summarize the various operations of the column

  6. MongoDB Summary of index operations in

Advanced :

  1. MongoDB Summary of aggregation operations

  2. MongoDB Import and export of 、 Backup recovery summary

  3. MongoDB Summary of user management

  4. MongoDB Copy ( Replica set ) summary

  5. MongoDB Slice summary

  6. MongoDB meet spark( Integration )

  7. MongoDB Internal storage principle

Other :

  1. python3 operation MongoDB Various cases of

  2. MongoDB Command summary

One . MongoDB Copy ( Replica set )

MongoDB Replica set (Replica Set) It is a master-slave cluster with automatic fault recovery function , There is a master node and one or more slave nodes . Replica set has no fixed master node , When the primary node fails , The whole cluster will elect a master node to provide services for the system to ensure the high availability of the system .

MongoDB Replication is the process of synchronizing data on multiple servers .

Replication provides redundant backup of data , And store copies of data on multiple servers , Improved data availability , And can guarantee the security of data .

Replication also allows you to recover data from hardware failures and service outages .

Official website reference :MongoDB Replica set

Two . Why copy ?

Secure data
High availability of data (24* 7 )
disaster recovery
No need to shut down for maintenance ( Backup , Rebuild index , Compress )
Read zoom ( Extra copy reads )
Replica sets are transparent to applications

3、 ... and . MongoDB The principle of replication

mongodb Replication of requires at least two nodes . One of them is the master node , Responsible for handling client requests , The rest are from nodes , Responsible for copying the data on the primary node .

mongodb The common collocation mode of each node is : A master from 、 One master, many followers .

All operations recorded on the master node oplog, Periodically polling the master from the node for these operations , Then perform these operations on your own copy of the data , So as to ensure that the data of the slave node is consistent with that of the master node .

MongoDB Copy the structure diagram as follows :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-UAcjfDeV-1657022462558)(evernotecid://B1CD39FE-B044-413D-A086-0649DB3F0070/appyinxiangcom/26430792/ENResource/p1224)]
In the structure chart above , The client reads data from the master node , When the client writes data to the master node , Data interaction between master node and slave node ensures data consistency .

Replica set features :

  • N Cluster of nodes
  • Any node can be the master node
  • All writes are on the primary node
  • Automatic failover
  • Automatic recovery

Four . Set up a replica set

Use the same MongoDB To do it MongoDB The master-slave experiment , The operation steps are as follows :

1、 Close running MongoDB The server .

2、 By designation --replSet Option to start mongoDB. The basic syntax is as follows :

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"

5、 ... and . Case study

4.1 Start each member of the replica set with the appropriate options

sudo rm -rf /MongoDB/node1 /MongoDB/node2 /MongoDB/node3

sudo mkdir -p /MongoDB/node1 /MongoDB/node2 /MongoDB/node3

sudo mongod —bind_ip 192.168.17.129 —port 27020 —dbpath "/MongoDB/node1" —replSet rs0

sudo mongod —bind_ip 192.168.17.129 —port 27021 —dbpath "/MongoDB/node2" —replSet rs0

sudo mongod —bind_ip 192.168.17.129 —port 27022 —dbpath "/MongoDB/node3" —replSet rs0

You can also specify the replica set name in the configuration file . start-up mongod Use profile , File specified with configuration options

4.2 mongo shell Connect replica set

mongo -port 27020 --host 192.168.17.129

4.3 initialization initiate Replica set

utilize rs.initiate() A default replica set configuration will be initialized on a member of the replica set .

4.4 Verify the initial replica set configuration

Use rs.conf() Show replica set configuration objects :

rs0:OTHER> rs.conf()
{
    
    "_id" : "rs0",
    "version" : 1,
    "protocolVersion" : NumberLong(1),
    "members" : [
        {
    
            "_id" : 0,
            "host" : "192.168.17.129:27020",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
    
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        }
    ],
    "settings" : {
    
        "chainingAllowed" : true,
        "heartbeatIntervalMillis" : 2000,
        "heartbeatTimeoutSecs" : 10,
        "electionTimeoutMillis" : 10000,
        "getLastErrorModes" : {
    
        },
        "getLastErrorDefaults" : {
    
            "w" : 1,
            "wtimeout" : 0
        },
        "replicaSetId" : ObjectId("579b3500299da8059cc5fb99")
    }
}
rs0:PRIMARY>

4.5 Check the status of the replica set rs.status()

rs0:PRIMARY> rs.status()
{
    
    "set" : "rs0",
    "date" : ISODate("2021-07-29T11:13:58.433Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "members" : [
        {
    
            "_id" : 0,
            "name" : "192.168.17.129:27020",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1200,
            "optime" : {
    
                "ts" : Timestamp(1469789441, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2021-07-29T11:50:41Z"),
            "electionTime" : Timestamp(1469789440, 2),
            "electionDate" : ISODate("2021-07-29T11:50:40Z"),
            "configVersion" : 1,
            "self" : true
        }
    ],
    "ok" : 1
}
rs0:PRIMARY>

4.6 Add the remaining members to the replica set

You must connect to the replica set primary Master node , Can be used rs.add() Add remaining members .

rs.add() In some cases , Trigger an election . If connected to the master node primary Become a slave node secondary, Connection required Mongo shell To the master node primary Continue to add new replica set members
utilize rs.status() Identify replica set master primary

4.7 Delete copy

rs.remove("192.168.17.129:27021")

The above example will start a named rs0 Of MongoDB example , Its port number is 27017.

After startup, open the command prompt box and connect mongoDB service .

stay Mongo The client uses the command rs.initiate() To start a new replica set .

We can use rs.conf() To view the configuration of the replica set , View replica set status using rs.status() command

原网站

版权声明
本文为[cui_ yonghua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071117314469.html