Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to switch replica sets in MongoDB

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

MongoDB how to switch copy set, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

MongoDB replica set switching method

1. Rs.stepDown ([stepdownSecs, catchUpSecs]) stepdown as primary (disconnects)

The steps are as follows:

[root@127-0-0-1 conf] # mongo-- port 2777

Shard1:PRIMARY > show dbs

Admin 0.000GB

Config 0.000GB

Local 0.001GB

Yoon 0.000GB

Shard1:PRIMARY > rs.help ()

Rs.status () {replSetGetStatus: 1} checks repl set status

Rs.initiate () {replSetInitiate: null} initiates set with default settings

Rs.initiate (cfg) {replSetInitiate: cfg} initiates set with configuration cfg

Rs.conf () get the current configuration object from local.system.replset

Rs.reconfig (cfg) updates the configuration of a running replica set with cfg (disconnects)

Rs.add (hostportstr) add a new member to the set with default attributes (disconnects)

Rs.add (membercfgobj) add a new member to the set with extra attributes (disconnects)

Rs.addArb (hostportstr) add a new member which is arbiterOnly:true (disconnects)

Rs.stepDown ([stepdownSecs, catchUpSecs]) stepdown as primary (disconnects)

Rs.syncFrom (hostportstr) make a secondary sync from the given member

Rs.freeze (secs) make a node ineligible to become primary for the time specified

Rs.remove (hostportstr) remove a host from the replica set (disconnects)

Rs.slaveOk () allow queries on secondary nodes

Rs.printReplicationInfo () check oplog size and time range

Rs.printSlaveReplicationInfo () check replica set members and replication lag

Db.isMaster () check who is primary

Reconfiguration helpers disconnect from the database so the shell will display

An error, even if the command succeeds.

The command shard1:PRIMARY > rs.stepDown () # degrades primary to a Secondary node

The command shard1:PRIMARY > rs.stepDown (30) # degrades primary to Secondary node and maintains 30s. If no new primary is elected during this period, this node will rejoin for election.

2. By setting priority

a. In order to ensure the consistency of data, the writing service of the application must be turned off first.

b. Raise the priority of Secondary nodes to be upgraded to primary nodes

Do the following:

View current configuration information:

Shard1:PRIMARY > rs.conf ()

{

"_ id": "shard1"

"version": 4

"protocolVersion": NumberLong (1)

"writeConcernMajorityJournalDefault": true

"members": [

{

"_ id": 0

"host": "127.0.0.1 purl 2777"

"arbiterOnly": false

"buildIndexes": true

"hidden": false

"priority": 3

"tags": {

}

"slaveDelay": NumberLong (0)

"votes": 1

}

{

"_ id": 1

"host": "127.0.0.1 purl 3777"

"arbiterOnly": false

"buildIndexes": true

"hidden": false

"priority": 1

"tags": {

}

"slaveDelay": NumberLong (0)

"votes": 1

}

{

"_ id": 2

"host": "127.0.0.1 purl 4777"

"arbiterOnly": false

"buildIndexes": true

"hidden": false

"priority": 1

"tags": {

}

"slaveDelay": NumberLong (0)

"votes": 1

}

]

"settings": {

"chainingAllowed": true

"heartbeatIntervalMillis": 2000

"heartbeatTimeoutSecs": 10

"electionTimeoutMillis": 10000

"catchUpTimeoutMillis":-1

"catchUpTakeoverDelayMillis": 30000

"getLastErrorModes": {

}

"getLastErrorDefaults": {

"w": 1

"wtimeout": 0

}

"replicaSetId": ObjectId ("5f27de76c8d22581cdeb2f2e")

}

}

Shard1:PRIMARY > config=rs.conf () # View the current configuration and store it in the config variable

Shard1:PRIMARY > config.members [2] .priority = 3 # modify the config variable to set the member priority of the third group of ports 4777 to 3

Shard1:PRIMARY > rs.reconfig (config) # configuration takes effect

Shard1:PRIMARY > db.isMaster ()

{

"hosts": [

"127.0.0.1purl 2777"

"127.0.0.1purl 3777"

"127.0.0.1 purl 4777"

]

"setName": "shard1"

"setVersion": 7

"ismaster": true

"secondary": false

"primary": "127.0.0.1 purl 4777"

"me": "127.0.0.1 purl 4777"

"electionId": ObjectId ("7fffffff0000000000000004")

"lastWrite": {

"opTime": {

Ts: Timestamp (1596599298, 1)

"t": NumberLong (4)

}

LastWriteDate: ISODate ("2020-08-05T03:48:18Z")

"majorityOpTime": {

Ts: Timestamp (1596599298, 1)

"t": NumberLong (4)

}

MajorityWriteDate: ISODate ("2020-08-05T03:48:18Z")

}..

...

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report