Get the App
SLTechnology News&Howtos  ›  Database  › 

The mongodb replica set adds a slave node step using the consistent snapshot method

Shulou Source: shulou.com Published: 2022-06-01 05:25:59 09月19日 Update

Environment description

Primary node 192.168.0.1purl 27002

Two slave nodes

192.168.0.2:27002

192.168.0.3:27002

Goal: add the third slave node 192.168.0.3 with a consistent snapshot

Brief description of the steps:

1) perform a consistent snapshot backup on the primary node

2) restore the consistent snapshot from the node, restore only part of the data, and do not restore the oplog for the time being

3) initialize the oplog.rs collection and restore the oplog record

4) initialize the other two collections db.replset.election,db.system.replset of the local database

5) modify the database configuration and restart the database (the instance does not enable the authentication mode and the configuration of the replication set before this step)

6) use the rs.add ("HOST_NAME:PORT") command to add slave nodes to the cluster

7)。 Use rs.status () to observe the synchronization status and verify the integrity and consistency of the data

one。 Back up data on the master node or the other two slave nodes:

Mongodump-uroot-ptest-- host 192.168.0.2-- authenticationDatabase=admin-- port=27002-- oplog-o / data/mongo/backup

two。 Scp the backup file to 192.168.0.3 and restore it:

Scp-r / data/mongo/backup mongo@192.168.0.3/data/mongo

three。 The third node starts as a single instance:

Note: the following replica set parameters need to be commented out

# auth = true

# replSet = test27002

# replSet = repl_mongo

# keyFile = / data/mongo/27002/replSet.key

# su-mongo

$mongod-f / data/mongo/27002/conf/mongodb.conf

Consistent snapshot restore on 192.168.0.3:

$mongorestore-oplogReplay-port=27002 / data/mongo/backup

Create oplog.rs set merge initialization size:

> use local

> db.createCollection ("oplog.rs", {"capped": true, "size": 100000000})

Restore the data from the oplog.rs collection of consistent backups to 192.168.0.3:

$mongorestore-d local-c oplog.rs-- port=27002 / data/mongo/backup/oplog.bson

You need to query the data of the primary node replset.election collection and store the data to the 192.168.0.3 node

Actions on the primary DB:

$mongo 192.168.0.1:27002/admin-uroot-ptest

Test27002:PRIMARY > use local

Switched to db local

Test27002:PRIMARY > db.replset.election.find ()

{"_ id": ObjectId ("5d64912a1978c9b194cf7cc5"), "term": NumberLong (2), "candidateIndex": NumberLong (2)}

The data contents of the replset.election collection on the primary DB are saved on the 192.168.0.3 node:

Use local

Db.replset.election.save ({"_ id": ObjectId ("5d64912a1978c9b194cf7cc5"), "term": NumberLong (2), "candidateIndex": NumberLong (2)})

Close the third slave node and start mongodb as a replica set:

> use admin

Switched to db admin

> db.shutdownServer ()

Server should be down...

2019-09-01T18:10:57.337+0800 I NETWORK [js] trying reconnect to 127.0.0.1 purl 27002 failed

2019-09-01T18:10:57.337+0800 I NETWORK [js] reconnect 127.0.0.1 purl 27002 failed failed

>

>

Modify the configuration of the third slave node and remove the comments:

Auth = true

ReplSet = test27002

KeyFile = / data/mongo/27002/replSet.key

Start mongodb with a copy of the set

$mongod-f / data/mongo/27002/conf/mongodb.conf

The primary node performs the add node operation:

Mongo 192.168.0.1:27002/admin-uroot-ptest

> rs.add ("172.31.30.82 rig 27001")

Write data on the primary node:

Use test

For (var iTuno nisu test)

> db.test.count ()

Note: the above steps are similar to MySQL's method of adding a slave node using mysqldump or xtrabackup backup.

Tags: Node data consistency three backup method snapshot copy two database configuration step instance comment method content parameter command comment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Huawei macOS Redmi